You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a state machine is nested inside another state machine, the StateMachine.StateChanged event of the parent (root) machine will not be called for any state changes inside the nested machine.
Not quite sure if this is intended behavior or not, thus labelling this as an issue for now.
If this indeed is intended behavior, I'd recommend adding a note to the StateChanged event XML comment. Furthermore, a HierarchyChanged event could be implemented to allow users to get feedback on state changes without polling.
My reasoning for this is that I don't want to keep polling the StateMachine.GetActiveHierarchyPath() method each frame, since it generates garbage with the string concatenations.
Example script that showcases the behavior:
usingUnityEngine;usingUnityHFSM;namespaceUnityHFSMTest{/// <summary>/// This example demonstrates how the <see cref="StateMachine.StateChanged"/> event does NOT get triggered when a nested state machine changes its state./// </summary>publicclassUnityHFSMStateChangedTest:MonoBehaviour{privateStateMachine_rootFsm;privatevoidAwake(){_rootFsm=newStateMachine();// ----- Root States -----// State A: Normal state that waits for one second before transitioning to the next state._rootFsm.AddState("State A",onEnter: _ =>{print("Enter state A");},onLogic: state =>{if(state.timer.Elapsed>1)state.fsm.StateCanExit();},needsExitTime:true);// State B: A state machine, that contains two states (X and Y).StateMachinestateBFsm=new(needsExitTime:true);stateBFsm.AddState("Nested X",onEnter: _ =>{print("Enter state B-X");},onLogic: state =>{if(state.timer.Elapsed>1)state.fsm.StateCanExit();},needsExitTime:true);stateBFsm.AddState("Nested Y",onEnter: _ =>{print("Enter state B-Y");},onLogic: state =>{if(state.timer.Elapsed>1)state.fsm.StateCanExit();},needsExitTime:true);// Add state B transitions. Nested Y is an exit transition.stateBFsm.AddTransition(newTransition("Nested X","Nested Y"));stateBFsm.AddExitTransition(newTransition("Nested Y",""));// Add the state machine to the root FSM._rootFsm.AddState("State B",stateBFsm);// ----- Root Transitions -----// Alternate between "state A" and "state B"._rootFsm.AddTransition(newTransition("State A","State B"));_rootFsm.AddTransition(newTransition("State B","State A"));}privatevoidOnEnable()=>_rootFsm.StateChanged+=OnStateChanged;privatevoidOnDisable()=>_rootFsm.StateChanged-=OnStateChanged;privatevoidStart()=>_rootFsm.Init();privatevoidUpdate()=>_rootFsm.OnLogic();privatevoidOnStateChanged(StateBase<string>newState){print($"StateChanged @: {newState.name}");}}}
Expected output:
"StateChanged @ stateName" Debug.Log call for each state change.
Actual output:
Debug.Log call for only "State A" and "State B" (the highest-up states in the hierarchy):
The text was updated successfully, but these errors were encountered:
If this is the intended behavior and you give me the thumbs-up, I can submit a PR to clarify the event docs and potentially implement the HierarchyChanged event.
When a state machine is nested inside another state machine, the
StateMachine.StateChanged
event of the parent (root) machine will not be called for any state changes inside the nested machine.Not quite sure if this is intended behavior or not, thus labelling this as an issue for now.
If this indeed is intended behavior, I'd recommend adding a note to the StateChanged event XML comment. Furthermore, a
HierarchyChanged
event could be implemented to allow users to get feedback on state changes without polling.My reasoning for this is that I don't want to keep polling the
StateMachine.GetActiveHierarchyPath()
method each frame, since it generates garbage with the string concatenations.Example script that showcases the behavior:
Expected output:
"StateChanged @ stateName" Debug.Log call for each state change.
Actual output:
Debug.Log call for only "State A" and "State B" (the highest-up states in the hierarchy):
The text was updated successfully, but these errors were encountered: