.. _state-machine-class: State Machine ============= Namespace --------- .. code-block:: csharp namespace Handy2DTools.Actions{} Declaration ----------- .. code-block:: csharp public class StateMachine : MonoBehaviour {} Fields ---------- .. table:: :width: 100% :widths: 25 15 60 +-----------------------------------------------+----------------------------+----------------------------------------------------------------------------------------------------------+ | ``protected string`` | ``recognizedActorName`` | The actor related to this state machine |br| 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`` | ``recognizedStates`` | Visual feedback | +-----------------------------------------------+----------------------------+----------------------------------------------------------------------------------------------------------+ | ``protected List`` | ``states`` | List of states attached to the machine's GameObject. |br| Will be loaded at ``StateMachine.SetUp()`` | +-----------------------------------------------+----------------------------+----------------------------------------------------------------------------------------------------------+ Properties ---------- .. table:: :width: 100% :widths: 35 15 50 +-----------------------------------------------------------------------+----------------------------+-----------------------------------------------------------------------------------------------+ | ``public Actor { get; }`` | ``actor`` | The state machine's actor instance. |br| 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 { get; }`` | ``AttachedStates`` | The list of attached states | +-----------------------------------------------------------------------+----------------------------+-----------------------------------------------------------------------------------------------+ | ``public State { get; }`` | ``RecognizedActor`` | The machine's actor. Either attached to |br| the machine's GameObject or a parent | +-----------------------------------------------------------------------+----------------------------+-----------------------------------------------------------------------------------------------+ Methods ---------- .. table:: :width: 100% :widths: 45 15 40 +-----------------------------------------------+----------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | ``public virtual void`` | ``SetUp()`` | Called by the :ref:`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`` :ref:`transitions ` is met, |br| 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 :ref:`Actor ` ``MonoBehaviour.Update()``. Forces machine to |br| evaluate if should change the ``currentState`` and calls |br| the ``currentState`` ``State.Tick()`` | +-----------------------------------------------+----------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | ``public virtual void`` | ``LateTick()`` | Called every :ref:`Actor ` ``MonoBehaviour.LateUpdate()``. Forces machine to |br| evaluate if should change the ``currentState`` and calls |br| the ``currentState`` ``State.LateTick()`` | +-----------------------------------------------+----------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | ``public virtual void`` | ``FixedTick()`` | Called every :ref:`Actor ` ``MonoBehaviour.FixedUpdate()``. Forces machine to |br| evaluate if should change the ``currentState`` and calls |br| the ``currentState`` ``State.FixedTick()`` | +-----------------------------------------------+----------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ Events ------ .. table:: :width: 100% :widths: 25 15 60 +-----------------------------------------------+----------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | ``public UnityEvent`` | ``StateChanged`` | Invoked every time the machine changes the ``currentState`` | +-----------------------------------------------+----------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | .. HTML Substiuions .. |br| raw:: html