|
|
|
|
@@ -105,7 +105,7 @@ These methods define the flow of the FSM process; because in those methods the F
|
|
|
|
|
<li>To handle <strong>Event</strong> moments, create methods starting with OnBefore or OnAfter concatenated with the Event name.</li>
|
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
<p><em>* The OnLeave and OnBefore transition methods may return false to cancel the transition.</em>*</p>
|
|
|
|
|
<p><strong>The OnLeave and OnBefore transition methods may return false, which will cancel the transition.</strong></p>
|
|
|
|
|
|
|
|
|
|
<h2>1.2) Event Triggers</h2>
|
|
|
|
|
|
|
|
|
|
@@ -128,6 +128,95 @@ These rules define when an FSM can transition from a specific state towards an o
|
|
|
|
|
|
|
|
|
|
<p>The initial state can be defined using the method <a href="##(FSM).SetStartState">FSM.SetStartState</a>(). The default start state of an FSM is "None".</p>
|
|
|
|
|
|
|
|
|
|
<h3>Example</h3>
|
|
|
|
|
|
|
|
|
|
<p>This example creates a new FsmDemo object from class FSM.
|
|
|
|
|
It will set the start state of FsmDemo to Green.
|
|
|
|
|
2 Transition Rules are created, where upon the event Switch,
|
|
|
|
|
the FsmDemo will transition from state Green to Red and vise versa.</p>
|
|
|
|
|
|
|
|
|
|
<pre><code> local FsmDemo = FSM:New() -- #FsmDemo
|
|
|
|
|
FsmDemo:SetStartState( "Green" )
|
|
|
|
|
FsmDemo:AddTransition( "Green", "Switch", "Red" )
|
|
|
|
|
FsmDemo:AddTransition( "Red", "Switch", "Green" )
|
|
|
|
|
</code></pre>
|
|
|
|
|
|
|
|
|
|
<p>In the above example, the FsmDemo could flare every 5 seconds a Green or a Red flare into the air.
|
|
|
|
|
The next code implements this through the event handling method <strong>OnAfterSwitch</strong>.</p>
|
|
|
|
|
|
|
|
|
|
<pre><code> function FsmDemo:OnAfterSwitch( From, Event, To, FsmUnit )
|
|
|
|
|
self:E( { From, Event, To, FsmUnit } )
|
|
|
|
|
|
|
|
|
|
if From == "Green" then
|
|
|
|
|
FsmUnit:Flare(FLARECOLOR.Green)
|
|
|
|
|
else
|
|
|
|
|
if From == "Red" then
|
|
|
|
|
FsmUnit:Flare(FLARECOLOR.Red)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
FsmDemo:__Switch( 5, FsmUnit ) -- Trigger the next Switch event to happen in 5 seconds.
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
FsmDemo:__Switch( 5, FsmUnit ) -- Trigger the first Switch event to happen in 5 seconds.
|
|
|
|
|
</code></pre>
|
|
|
|
|
|
|
|
|
|
<p>The OnAfterSwitch implements a loop. The last line of the code fragment triggers the Switch Event within 5 seconds.
|
|
|
|
|
Upon the event execution (after 5 seconds), the OnAfterSwitch method is called of FsmDemo (cfr. the double point notation!!! ":").
|
|
|
|
|
The OnAfterSwitch method receives from the FSM the 3 transition parameter details ( From, Event, To ),
|
|
|
|
|
and one additional parameter that was given when the event was triggered, which is in this case the Unit that is used within OnSwitchAfter.</p>
|
|
|
|
|
|
|
|
|
|
<pre><code> function FsmDemo:OnAfterSwitch( From, Event, To, FsmUnit )
|
|
|
|
|
</code></pre>
|
|
|
|
|
|
|
|
|
|
<p>For debugging reasons the received parameters are traced within the DCS.log.</p>
|
|
|
|
|
|
|
|
|
|
<pre><code> self:E( { From, Event, To, FsmUnit } )
|
|
|
|
|
</code></pre>
|
|
|
|
|
|
|
|
|
|
<p>The method will check if the From state received is either "Green" or "Red" and will flare the respective color from the FsmUnit.</p>
|
|
|
|
|
|
|
|
|
|
<pre><code> if From == "Green" then
|
|
|
|
|
FsmUnit:Flare(FLARECOLOR.Green)
|
|
|
|
|
else
|
|
|
|
|
if From == "Red" then
|
|
|
|
|
FsmUnit:Flare(FLARECOLOR.Red)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
</code></pre>
|
|
|
|
|
|
|
|
|
|
<p>It is important that the Switch event is again triggered, otherwise, the FsmDemo would stop working after having the first Event being handled.</p>
|
|
|
|
|
|
|
|
|
|
<pre><code> FsmDemo:__Switch( 5, FsmUnit ) -- Trigger the next Switch event to happen in 5 seconds.
|
|
|
|
|
</code></pre>
|
|
|
|
|
|
|
|
|
|
<p>This example is fully implemented in the following MOOSE test mission:</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<p>Note that transition rules can be declared with a few variations:</p>
|
|
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
|
<li>The From states can be a table of strings, indicating that the transition rule will be valid if the current state of the FSM will be one of the given From states.</li>
|
|
|
|
|
<li>The From state can be a "*", indicating that the transition rule will always be valid, regardless of the current state of the FSM.</li>
|
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
<p>This transition will create a new FsmDemo object from class FSM.
|
|
|
|
|
It will set the start state of FsmDemo to Green.
|
|
|
|
|
A new event is added in addition to the above example.
|
|
|
|
|
The new event Stop will cancel the Switching process.
|
|
|
|
|
So, the transtion for event Stop can be executed if the current state of the FSM is either "Red" or "Green".</p>
|
|
|
|
|
|
|
|
|
|
<pre><code> local FsmDemo = FSM:New() -- #FsmDemo
|
|
|
|
|
FsmDemo:SetStartState( "Green" )
|
|
|
|
|
FsmDemo:AddTransition( "Green", "Switch", "Red" )
|
|
|
|
|
FsmDemo:AddTransition( "Red", "Switch", "Green" )
|
|
|
|
|
FsmDemo:AddTransition( { "Red", "Green" }, "Stop", "Stopped" )
|
|
|
|
|
</code></pre>
|
|
|
|
|
|
|
|
|
|
<p>The transition for event Stop can also be simplified, as any current state of the FSM is valid.</p>
|
|
|
|
|
|
|
|
|
|
<pre><code> FsmDemo:AddTransition( "*", "Stop", "Stopped" )
|
|
|
|
|
</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>1.4) FSM Process Rules</h2>
|
|
|
|
|
|
|
|
|
|
<p>The FSM can implement sub-processes that will execute and return multiple possible states. <br/>
|
|
|
|
|
|