State Machine

Namespace

namespace Handy2DTools.Actions{}

Declaration

public class StateMachine : MonoBehaviour {}

Fields

protected string

recognizedActorName

The actor related to this state machine
just for visual feedback at the inspector

protected MachineStatus

status

The machine current status

protected string

currentStateName

A visual feedback for inspector

protected State

defaultState

The default state to be set using inspector

protected List<State>

recognizedStates

Visual feedback

protected List<State>

states

List of states attached to the machine’s GameObject.
Will be loaded at StateMachine.SetUp()

Properties

public Actor { get; }

actor

The state machine’s actor instance.
This is set in the Machine’s SetUp method.

public MachineStatus { get; }

Status

A getter for the machine’s status

public State { get; protected set; }

currentState

The machine’s current state

public State { get; }

DefaultState

A getter for defaultState

public List<State> { get; }

AttachedStates

The list of attached states

public State { get; }

RecognizedActor

The machine’s actor. Either attached to
the machine’s GameObject or a parent

Methods

public virtual void

SetUp()

Called by the Actor to initialize the machine

Arguments: Actor actor

protected virtual void

LoadStates()

Loads all states attached into the machine’s GameObject.

public virtual void

StartMachine()

If the machine is ready, sets the currentState to de default state and status to On

public virtual void

Resume()

status = On

public virtual void

Pause()

status = Pause

public virtual void

Stop()

status = Off

public virtual void

ChangeState()

Sets the currentState as the given state

Arguments: State state

protected virtual void

EvaluateNextState()

Case any condition of the currentState transitions is met,
the machine will transition into the target state

protected virtual State

ConditionMet()

Checks the StateTransition.Condition() of all State.transitions of the currentState. Returns null if no condition is met.

public virtual void

SetDefaultState()

Sets the defaultState

Arguments: T where T : State

public virtual void

Tick()

Called every Actor MonoBehaviour.Update(). Forces machine to
evaluate if should change the currentState and calls
the currentState State.Tick()

public virtual void

LateTick()

Called every Actor MonoBehaviour.LateUpdate(). Forces machine to
evaluate if should change the currentState and calls
the currentState State.LateTick()

public virtual void

FixedTick()

Called every Actor MonoBehaviour.FixedUpdate(). Forces machine to
evaluate if should change the currentState and calls
the currentState State.FixedTick()

Events

public UnityEvent<State>

StateChanged

Invoked every time the machine changes the currentState