1. State Types

Animancer's state machines are Generic so before you can make a StateMachine<TState> you need to define your base state type (to use as the TState parameter). The only requirement for a class to be used as a state in this system is to implement the IState interface, which means that states can be anything you want such as:

  • MonoBehaviour components attached to a scene object or prefab.
  • ScriptableObject assets in the project.
  • Or just regular C# classes.

Example

A base state class can be as simple as this:

public abstract class State : IState
{
    public virtual bool CanEnterState => true;

    public virtual bool CanExitState => true;

    public virtual void OnEnterState() { }

    public virtual void OnExitState() { }
}

Existing Types

Rather than implementing the entire interface from scratch every time, there are several base classes that can make your implementation simpler:

Class Description
State The State class used in the above Example is included in the system so you can simply Inherit from it so that you only need to override the members you actually want to use.
DelegateState Implements IState, but rather than defining its own logic it simply has a Delegate for each of the members of that interface so you can assign them when creating the state.
StateBehaviour

Inherits from MonoBehaviour and implements IState to act as the base class for states that are attached as components on GameObjects. Its OnEnterState and OnExitState methods enable and disable itself respectively, allowing you to use the regular MonoBehaviour Messages like OnEnable and OnDisable to implement your state transition logic, as well as other messages like Update or FixedUpdate depending on the needs of each individual state.

All of the State Machines samples use State Behaviours.