State machines are a very effective way to manage game state, either on your main game play object (Game Over, Restart, Continue etc) or on individual actors and NPCs (AI behaviours, Animations, etc). The following is a simple state machine that should work well within any Unity context.
Most state machines come from the world of C# enterprise, and are wonderfully complicated or require a lot of boilerplate code. State Machines however are an incredibly useful pattern in game development, administrative overhead should never be a burden that discourages you from writing good code.
- Simple use of Enums as state definition.
- Minimal initialization - one line of code.
- Incredibly easy to add/remove states
- Uses reflection to avoid boiler plate code - only write the methods you actually need.
- Compatible with Coroutines.
- Tested on iOS and Android
An example project is included (Unity 5.0) to show the State Machine in action.
To use the state machine you need a few simple steps
using MonsterLove.StateMachine; //Remember the using statement before the class declaration
public class MyStateMachine : StateBehaviour
{
}
public enum States
{
Init,
Play,
Win,
Lose
}
Initialize<States>();
This can be done at any time, but generally you would do it on start up in your Awake function.
ChangeState(States.Init);
void Init_Enter()
{
Debug.Log("We are now ready");
}
//Coroutines are supported, simply return IEnumerator
IEnumerator Play_Enter()
{
Debug.Log("Game Starting in 3");
yield return new WaitForSeconds(1);
Debug.Log("Game Starting in 2");
yield return new WaitForSeconds(1);
Debug.Log("Game Starting in 1");
yield return new WaitForSeconds(1);
Debug.Log("Start");
}
void Play_Update()
{
Debug.Log("Game Playing");
}
void Play_Exit()
{
Debug.Log("Game Over");
}
Currently supported methods are:
Enter
Exit
FixedUpdate
Update
LateUpdate
Finally
It should be easy enough to extend the source to include other Unity Methods such as OnTriggerEnter, OnMouseDown etc
These methods can be private or public. The methods themselves are all optional, so you only need to provide the ones you actually intend on using.
Couroutines are supported on Enter and Exit, simply return IEnumerator
. This can be great way to accommodate animations.
Finally is a special method guaranteed to be called after a state has exited. This is a good place to perform any hygenie operations such as removing event listeners. Note: Finally does not support coroutines.
There is simple support for managing asynchronous state changes with long enter or exit coroutines.
ChangeState(States.MyNextState, StateTransition.Safe);
The default is StateTransition.Safe
. This will always allows the current state to finish both it's enter and exit functions before transitioning to any new states.
ChangeState(States.MyNextState, StateTransition.Overwrite);
StateMahcine.Overwrite
will cancel any current transitions, and call the next state immediately. This means any code which has yet to run in enter and exit routines will be skipped. If you need to ensure you end with a particular configuration, the finally function will always be called:
void MyCurrentState_Finally()
{
//Reset object to desired configuration
}
There are no dependencies, but if you're working with the source files, the tests rely on the UnityTestTools package which can be downloaded from the Asset Store. These are non-essential, only work in the editor, and can be deleted if you so choose.
StateMachineBehaviour was a logical name, so much so that Unity used it for the animation system in Unity 5.0. To avoid naming conflicts, StateMachineBevaiour
and StateMachineEngine
have been changed to StateBehaviour
and StateEngine
respectively. To upgrade you will need to make all your monobevaviours inherit from StateBehaviour instead, and drag StateEngine.cs the same script component used by your game object for StateMachineEngine.
This implementation uses reflection to automatically bind the state methods callbacks for each state. This saves you having to write endless boilerplate and generally makes life a lot more pleasant. But of course reflection is slow, so we try minimize this by only doing it once during the call to Initialize
.
For most objects this won't be a problem, but note that if you are spawning many objects during game play it might pay to make use of an object pool, and initialize objects on start up instead. (This is generally good practice anyway).
This is designed to target mobile, as such should be memory allocation free. Right now we are failing on that front, primarily due to IEnumerator's generating garbage. This remains a work in progress.
Due to differences in the Windows Store flavour of .Net, this is currently incompatible. More details available in this issue.
##License MIT License
This is state machine is used extensively on the Made With Monster Love title Cadence, a puzzle game about making music.
This library was heavily inspired by the state machine found at http://unitygems.com/ (available via The Internet Archive as of Nov 2014). Whilst that was my preferred way to manage state, the truth is that most of the time I ended up using a very rudimentary state machine above the Unity Gems effort. This was primarily because I kept on running into hitches with iOS. But it was also an intimidating project to use, sifting through an elaborate tutorial to find all the moving parts (which as of writing appears to be offline anyway).
Finally I found the time to take my favourite parts of the Unity Gems effort and my ghetto state machine to create something awesome. Adhering to the principle that a library should do one thing really well, what we have now I consider to be the easiest and most straight forward State Machine in existence for Unity.