How it works
Rememeber that HFSM is just a FSM that can use other FSM objects as states, hence hierarchy in the name - we compose networks in other networks.
We have a hierarchy of states that looks like this:
- Patrol
- Find Next Waypoint
- Walk Towards Waypoint
- Combat
- Walk towards player
- Attack player
Each network layer doesn't know anything about other networks, they are fully encapsulated. We start at first network layer which has Patrol and Combat states and start with Patrol state active, it gets activated and we start executing Patrol network which has Find Next Waypoint state (we start here) and Walk Towards Waypoint state. We have found waypoint so we switch to Walk Towards Waypoint state. Notice that while we are executing Patrol network we are still executing root network too, which means at any time we can get player in range condition succeed and we switch at root level to Combat network, then there it starts at Walk Towards Player, it reaches the player, switches to Attack Player, makes player dead and root network switches back to Patrol network, its Find Next Waypoint gets activated it cycles between two states of Patrol super-state.
The key information is:
In HFSM, FSM networks are equivalent to states so as long as network is active, it will process its states, and if we get networks tree many levels deep, all nodes (FMS networks) that are along the way from the root to the leaf, are active and they can make decisions.
This allows us to modularize our previously made FSM even more, making us able to group states into reusable "categories" instead of copy-paste bits here and there - think of it as nested prefabs for behaviors.