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 first 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 multilevel hierarchical state machines:
- First, let's create the
StateHighLevel
class deriving fromState
, as shown in the following code:
using UnityEngine; using System.Collections; using System.Collections.Generic; public class StateHighLevel : State { }
- Next, add the new member variables to control the internal states, as demonstrated in the following code:
public List<State> states; public State stateInitial; protected State stateCurrent;
- Then...