FSM Design
This project implements an example of a Finite State Machine (FSM). In the code, the example implementation of this is called a Fixed State Machine because each definition of the state machine is 'fixed' and statically defined in its possible transitions and states.
Finite State Machine
The main design of these classes is managed by a set of C# Attributes to configure and manage controls for the state machine directly from the C# code.
Interfaces
- IStateMachine<E> - interface to manage a set of states and transitions.
- IAnimStateMachine<E> - extends IStateMachine<E> and supports extra features such as an attached Animator and a currently selected animation.
Implementations
implementations of the state machines.
- FixedSM - abstract implementation of IStateMachine<E> with cached transitions and events from decorators from FSMUtils.
- FixedSMBehaviour - abstract implementation of IStateMachine<E> with cached transitions and events from decorators from FSMUtils in addition to firing off events for Unity Messages and supports attributes such as OnUpdateAttribute
- FixedSMAnim - abstract extension of the FixedSM that supports updating an animation state of an Animator component of a state machine and fully implements the IAnimStateMachine<E>
Components
- State - A state for a given FSM.
- TransitionAttribute - Attribute to define and manage the transitions for a given state.
- AnimationAttribute - Attribute to select an animation state for the given Animator of a state machine
IEvent - Event for managing transitions or executing actions in state machines.
Entry and exit behaviors defined via the attributes:
- OnEnterStateAttribute - Called when state is entered
- OnExitStateAttribute - Called when the state is exited
InitialStateAttribute - Attribute that defines the initial state of a given state machine.
OnEventDoActionAttribute - Whenever an IEvent is thrown invoke a given action.
Update Attributes to be triggered on various MonoBehaviour functions including the following subset. There are other messages defined for the unity MonoBehaviour but these are the only planned ones as of now, feel free to extend the code or add your own events if you wish.
- OnUpdateAttribute - Called each frame.
- OnFixedUpdateAttribute - Called each fixed update.
- OnLateUpdateAttribute - Called at the end of each frame.
- OnGUIAttribute - Called each GUI update.
- OnEnableAttribute - Called when object is enabled.
- OnDisableAttribute - Called when object is disabled.
- OnAnimatorIKAttribute - Callback for setting up animation IK (inverse kinematics).
AnimationTransitionAttribute - Extension of TransitionAttribute that will trigger an animation upon transition and can configure settings for the CrossFade call for the animation transition between states.
TransitionAfterTimeAttribute - Extension of TransitionAttribute will trigger a transition automatically after a given period of time in the current state. Will only work for implementations of FixedSMBehaviour and its sub classes.
TransitionOnAnimationCompleteAttribute - Automatically transition after an animation has been completed. Supported by FixedSMAnim
Creating a State Machine
If you want to create your own state machine based off this library, first you need to select which abstract implementation of IStateMachine<E> you would like to use.
Next, define your set of states for the state machine as sub classes under the main class. The way transitions work for the state machine is defined by transitions via TransitionAttribute which are triggered by Events which implement IEvent . Make sure to label the initial state with an InitialStateAttribute
Whenever the RaiseEvent(IEvent) is invoked, the state machine should check any transitions defined within the state machine for its current state. If any transitions specify that type of event, the state machine will transition from the current state to the targeted state.
You can also define additional attributes to call a method whenever an event is raised using an OnEventDoActionAttribute . Additionally, if you are using the FixedSMBehaviour , you can use attributes to trigger functions with the MonoBehaviour such as OnUpdateAttribute .
Examples
Some examples of custom state machines are added in the test code under
- DemoFixedStateMachine - Example implementation of a FixedSM.
- DemoFixedStateMachineMonoBehaviour - Example implementation of a FixedSMBehaviour
DemoFixedSMAim - Example implementation of a FixedSMAnim
ExampleSMAnim - Example state machine added as a sample to the project.
In addition, the example FSM is explained in further detail in the Example FSM document.