UnityHFSM
UnityHFSM copied to clipboard
'needsExitTime' doesn't seem to respect sub-machine's state's "forceInstantly"?
I have a simple scenario like:
var FSM = new StateMachine(true);
FSM.AddState("Idle", new State(onEnter: Idle, onLogic: Idle));
var movementFSM = new StateMachine(true);
FSM.AddState("Movement", movementFSM);
movementFSM.AddState("Walk", new State(onLogic: Walk));
movementFSM.AddState("Dodge", new State(onLogic: Dodging, canExit: _=> dodgeAnim.Animation.IsFinished));
FSM.AddTransition(new Transition("Idle", "Movement", _ => GetMovementInput.magnitude != 0, true));
FSM.AddTransition(new Transition("Movement", "Idle", _ => GetMovementInput.magnitude == 0 , false));
movementFSM.AddTransition(new Transition("Dodge", "Walk", _ => dashAnim.Animation.IsFinished, forceInstantly: false));
movementFSM.AddTransition(new Transition("Walk", "Dodge", _ => Globals.Input.Player.Dodge.WasPerformedThisFrame(), true));
Even though the "force instantly" is false for the Movement>Idle
transition, as well as the Dodge>Walk
, and there is a canExit
condition on my Dodge
state that isn't true until the animation is actually finished, it will still instantly leave my dodge animation if the Movement>Idle
transition condition is true.
This isn't making any sense to me and seems to defeat the whole purpose of a hierarchical state machine. What am I missing here or is this a bug?
Hi @Invertex, thanks for reaching out with your question.
I don't think what you've described is a bug. I've translated my understanding of your intention into the diagram below.
stateDiagram-v2
Idle: Idle
Movement: Movement (needsExitTime)
state Movement {
Walk
Dodge: Dodge <br> (needsExitTime)
[*] --> Walk
Dodge --> Walk: Animation is finished <br> (forced)
Walk --> Dodge: Dodge input
}
[*] --> Idle
Idle --> Movement: Any input
Movement --> Idle: No input
As far as I can tell, there are two distinct things that you want to happen:
-
When inside the
Movement
state, theDodge
animation should finish before transitioning to theWalk
state. That's why theDodge
state should haveneedsExitTime
set to true. Because it needs exit time, it also needs a way to exit. As the customonLogic
function (Dodging
) probably does not call theStateCanExit()
method internally, the state transition needs to be forced from the outside, i.e. the transition needsforceInstantly
to be true. -
When the root fsm wants to transition from
Movement
toIdle
, it should not transition instantly if it's still playing the Dodge animation, i.e. theDodge
state has not exited yet. Therefore theMovement
state (machine) also needs its ownneedsExitTime
property set to true. TheMovement
state machine will then only exit, when its currently active state does not need exit time (like theWalk
state) or when a state that does need exit time (like theDodge
state) exits.
The canExit
field lets the state machine instantly transition out of a state that needs exit time. It will not be continually checked. In your case it does not make sense, as the Dodge
state's needsExitTime
property was set to false.
Without knowing the details of your code, I've tried to outline a solution in the code snippet below. Let me know if this helps.
// The root fsm does not needExitTime
StateMachine fsm = new StateMachine();
// Idle should be able to exit immediately => does not needExitTime.
fsm.AddState("Idle", new State(onLogic: Idle));
// As the movement state needs to remain in the active state as long
// as the animation needs to play, it needsExitTime.
StateMachine movementFsm = new StateMachine(needsExitTime: true);
fsm.AddState("Movement", movementFsm);
fsm.AddTransition("Idle", "Movement", _ => isAnyMovementInput);
fsm.AddTransition("Movement", "Input", _ => !isAnyMovementInput);
// The movementFsm state machine should be able to leave at any time
// if it is in the Walk state => Walk does not need exit time.
movementFsm.AddState("Walk", new State(onLogic: Walk, canExit: _ => isDodgeAnimationFinished));
// But as the Dodge animation has to finish before the state machine should ...
// a) transition to Walk, the Dodge state needsExitTime
// OR:
// b) exit and transition to Idle, the Dodge state needsExitTime AND
// movementFsm needsExitTime.
movementFsm.AddState("Dodging", new State(onLogic: Dodging, needsExitTime: true));
// forceInstantly is required because the Dodge state needsExitTime and it
// won't exit itself (by calling movementFsm.StateCanExit()). That's why a
// transition has to be forced "from outside".
movementFsm.AddTransition(new Transition(
"Dodge",
"Walk",
_ => isDodgeAnimationFinished,
forceInstantly: true
));
// forceInstantly is not required here because Walk does not need exit time
movementFsm.AddTransition(new Transition(
"Walk",
"Dodge",
_ => isPlayerPerformingDodge
));