Use generics for state identifiers and events
masterUnityHFSM supports generics to allow using types other than string for state identifiers (TStateId) and events (TEvent). Using enum or int instead of string can improve type safety (preventing typos) and improve performance by up to 50% in internal mechanics.
When building a hierarchy, every nested state machine can use its own TStateId type. However, all state machines in a given hierarchy must share the same TEvent type so that triggers can be passed down the hierarchy.
enum PlayerStates {
IDLE, MOVE, JUMP
}
enum MoveStates {
WALK, DASH
}
enum Events {
ON_DAMAGE, ON_WIN
}
// Root FSM: Uses PlayerStates for its own ID, Events for triggers
var fsm = new StateMachine<PlayerStates, Events>();
// Nested FSM: Uses PlayerStates as its parent ID, MoveStates for its own ID, and Events for triggers
var moveFsm = new StateMachine<PlayerStates, MoveStates, Events>();
// Adding states and transitions
fsm.AddState(PlayerStates.IDLE, new State<PlayerStates, Events>());
fsm.AddState(PlayerStates.MOVE, moveFsm);
moveFsm.AddState(MoveStates.WALK);
moveFsm.AddState(MoveStates.DASH);
moveFsm.AddTransition(MoveStates.WALK, MoveStates.DASH);