On my free time I love to create game prototypes using C# in Unity. One of these prototypes is a turn-based game where I need to have multiple game states and a fine control over them.
I started working on a typical finite state machine (FSM) for controlling the game scenario and player input in different game modes, such as
First the state machine looked something like this:
Immediately I started to stress about how bloated the single state machine would become as the codebase grows in size. This is when I divided it in two state machines:
After dividing the state machine in two, I wanted to have the ability to bundle related state machines together and allow their states to change the active state machine based on what happens in the game world. For example if player encounters an enemy in
StateExplorationMoving - that state can call
I created a parent state machine class that can hold a collection of child state machines:
In the above table
PlayerStateMachine is a parent state machine for controlling child state machines.
BattleStateMachine can be considered as the states of the
After having this structure in place, I created a StateMachineController class that holds a list of parent state machines and runs their update methods.
I extracted the code from my prototype and started editing it to be more generic. You are free to use it for your own Unity projects if you want - and feel free to create a pull request if you have ideas to make it better. The project can be found from GitHub.