Inspiaaa / UnityHFSM

A simple yet powerful class-based hierarchical finite state machine for Unity
MIT License
1.14k stars 125 forks source link

[Question] Shortcut for AddTriggerTransition with custom typed states #55

Open ariok opened 1 month ago

ariok commented 1 month ago

Hello, I have initialized my fsm with 2 custom types (AIStateand AIStateEvent) instead of string type.

fsm = new StateMachine<AIState, AIStateEvent>();

I see that there are various useful shortcuts, but I can't find one to create a transition directly between custom types in AddTriggerTransition. So, I have to use:

fsm.AddTriggerTransition(AIStateEvent.PLAYER_DETECTED, new Transition<AIState>(AIState.IDLE, AIState.CHASE));

Am I correct in assuming there isn’t a shortcut like this?

fsm.AddTriggerTransition(AIStateEvent.PLAYER_DETECTED, AIState.IDLE, AIState.CHASE));

Of course, I could implement my own shortcut, but I’d prefer to use what the library provides rather than adding extra layers. :)

Something like this should work as extension:

public static void AddTriggerTransition<E, S>(this StateMachine<S, E> stateMachine, E trigger, S from, S to)
{
    stateMachine.AddTriggerTransition(trigger, new Transition<S>(from, to));
}