Inspiaaa / UnityHFSM

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

Feature request: onEnter and onExit on non-root StateMachines #21

Closed rafaeldolfe closed 1 year ago

rafaeldolfe commented 1 year ago

Hey! This repo is awesome. Helped massively for complex state machines in my UX flows. However, I've found myself repeatedly wishing that non-root state machines could trigger onEnter and onExit before they transition to the startState. Maybe they could even run their own onLogic.

Thoughts on this? I might volunteer to implement this if it sounds useful. :)

Inspiaaa commented 1 year ago

Hi @rafaeldolfe, I am not entirely sure that I understand the question, but let me try to answer all the same.

Once the root state machine transitions to a nested state machine, it will call its OnEnter method which in turn will call its active state's OnEnter method. This is the standard behaviour. If on the other hand you mean that you would like to add custom logic that is executed once the nested state machine enters / exits / has its OnLogic call in addition to its active state's events, then you can use the HybridStateMachine class. It is a StateMachine that also behaves like a normal State in the sense that you can define custom functions to be called OnEnter / OnLogic / OnExit. The custom functions are called after its active state's methods. The release branch already has a version of the code that makes the HybridStateMachine class more versatile, allowing you to also run custom logic before and after the active state's events. In addition you can also define custom actions that are executed after the sub-states custom actions. Please see the Changelog.md and the TestHybridStateMachine.cs files for usage examples.

If none of these answers help, please let me know.

rafaeldolfe commented 1 year ago

Oh I must have missed the HybridStateMachine then. That's what I am looking for.