Improving FSMs: hierarchical finite-state machines
Finite-state machines can be improved in terms of having different layers or hierarchies. The principles are the same, but states are able to have their own internal finite-state machine, making them more flexible and scalable.
Getting ready
This recipe is based on top of the previous recipe, so it is important that we grasp and understand how the finite-state machine recipe works.
How to do it...
We will create a state that is capable of holding internal states, in order to develop multi-level hierarchical state machines:
- Create the
StateHighLevel
class deriving fromState
:using UnityEngine; using System.Collections; using System.Collections.Generic; public class StateHighLevel : State { }
- Add the new member variables to control the internal states:
public List<State> states; public State stateInitial; protected State stateCurrent;
- Override the initialization function:
public override void OnEnable() { if (stateCurrent == null) ...