Game Development Reference
In-Depth Information
Updating the finite state machine
Updating the state machine internally performs two different operations. The first is to ex-
ecute or continue executing a running action, and the second is to select a new state once
the action is complete. As transitions exist in a priority order, we iterate over all transitions
executing their evaluators to determine the next state for the FSM to transition to. The first
evaluator that returns true determines the state the FSM moves to.
Note
Note that there is no validation that the finite state machine actually picks an action to
transition to. If this occurs, the FSM will attempt to iterate over each transition in the next
update call in order to find a valid transition. Using an evaluator that always returns true as
the last possible transition is a best practice to prevent cases where our agent is unable to
select any action to perform.
First we'll create an EvaluateTransitions function to determine the next state our
FSM will move to once a state has finished. Afterwards we can create the FSM Update
function that manages a running action and determines when state transitions occur.
FiniteStateMachine.lua :
local function EvaluateTransitions(self, transitions)
for index = 1, #transitions do
-- Find the first transition that evaluates to true,
-- return the state the transition points to.
if (transitions[index].evaluator_(self.userData_))
then
return transitions[index].toStateName_;
end
end
end
function FiniteStateMachine.Update(self, deltaTimeInMillis)
if (self.currentState_) then
local status = self:GetCurrentStateStatus();
if (status == Action.Status.RUNNING) then
Search WWH ::




Custom Search