- Home /
Animator Sub State Machine Exit Node Issue
Issue:
I want to have some code run whenever a sub-statemachine is exited, this should be easy using a sub-statemachine behavior. However, if I use the exit node on my substate machine, the entry node on the parent state machine is ignored and it jumps directly to the default state. This is an issue for me because I have important decisions coming from the entry node of the parent.
If I want the parent entry node to activate i need to use the (Up) node, but then it doesn't trigger the OnStateMachineExit method.
Potential Work Arounds:
1) I can create a new default state in the parent state machine and duplicate all my entry node logic there. The issue is that if I use a placeholder state to make the decisions, a frame is wasted processing this node. If you make the decisions on the entry node, the decisions happen instantly, this is a far cleaner and more responsive solution than having a place holder node.
2) I can put a StateMachineBehavior on the parent state machine and put the logic on the OnStateMachineEnter method. Then I can add a parameter to the animator and use that to determine whether or not to run the code when entering the parent state machine... but this is kind of a silly cludge.
Help...
Does anyone have any advice? Is this really the intended behavior? I don't understand why exiting a sub-statemachine isn't treated similar to entering the parent behavior.
At the very least, I think they should add a new event for OnStateMachineTransitionUp.... or OnStateMachineTransitionExit or something that triggers whenever the statemachine is left for any reason...
I've complained about the behaviour of State$$anonymous$$achineEnter/Exit before, to no avail. $$anonymous$$y work-around has been to create a script that calls the State$$anonymous$$achineEnter/Exit methods when the state machine is entered/exited, no matter how that happens.
It requires me to use a custom subclass on State$$anonymous$$achineBehaviour that seals the normal State$$anonymous$$achineEnter/Exit methods and exposes it's own versions, which can be used to get the expected behaviour.
If you're interested, I can make a thread on the forums and post the code. I've been meaning to clean it up anyways, so it would probably help me get that done.
I would absolutely love to know more about how you did that.
In the end, that's actually the behavior that I'm looking to create.
I added another reply to this post though, where I discovered a work around that I believe actually does mimic the behavior I was originally describing in this post.
Thanks for the response.
@baste Hey, I'm still really curious as to how you went about making that custom subclass.
What does it use to hook into the "event" of leaving the entire statemachine? Does it use something to scan each frame if it changed or something? Or does it just run OnStateExit (so it hits every state in the substatemachine) and check if it's moving to a state that's outside of the statemachine?
I'd love to know!
Oh, didn't see your reply back in December. Sorry!
I've got a script that lives next to the animator. It scans the animator's controller, and finds two pieces of information:
1: What states has my custom State$$anonymous$$achineBehaviour on it. 2: Which states are substates of which substate machines.
Then, during runtime, I check which state the machine is in. If the state changes, I call the custom OnState$$anonymous$$achineExit on all of the state machines that have been left.
I've been meaning to put the code on GitHub, I'll do that soonish.
EDIT: Github
Answer by Eluem · Dec 16, 2015 at 05:29 PM
I found a viable work around. It's still a cludge of sorts... but I THINK it achieves the exact overall behavior that I was describing.
If you create a placeholder Sub-Statemachine in the parent layer that has nothing in it except a transition straight from the entry node to the Up layer pointing to the parent statemachine, and then you create a transition from the original sub statemachine that was having issues to this statemachine, and point your states inside it to the exit node instead of the Up node, you can achieve what I wanted:
-Having the OnStateMachineExit fire -The parent entry node trigger -No dead frames (since instead of using a state to do the redirect, i'm using entry/up nodes, which don't consume a minimum of one update, they can pass through inside of an update [I believe])
However, this solution still doesn't address another problem... exiting the state machine due to an AnyState transition.
There really should be a sub state machine level method that is fired when the state machine itself is exited for any reason.
Answer by edwon · Sep 27, 2016 at 07:10 AM
Is there an "exit sub state machine" method yet? I could really use it!
Your answer
Follow this Question
Related Questions
OnStateEnter isn't updating in the correct order with normal update? 0 Answers
Sharing some but not all animator states between characters 0 Answers
Mecanim-Statemachine caches old version? (was: One Transition blocks other Transitions) 0 Answers
Transition Between Different Player States In Animator 0 Answers
Get time when the animation starts in Animator Controller 1 Answer