I have been working with nested FSMs lately and I often find that I could really use a node that where run when the FSM stops. For doing various cleanup etc. and I figured that running a Concurrent node in the FSM and putting a custom action with the cleanup code in the OnStop override should work. And it kind of does too, the only problem is that the OnStop method also gets called every time i ENTER the FSM.
This is one custom action that I have been using:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[Category("Mecanim")]
publicclassExitMecState:ActionTask
{
publicAnimator Animator;
protectedoverridevoidOnExecute(){}
protectedoverridevoidOnUpdate()
{
EndAction(false);
}
protectedoverridevoidOnStop(){
Animator.SetTrigger("Return");
EndAction(true);
}
}
Got any advice on what might be causing the OnStop to get called when I enter the FSM or any ideas on how to achieve the same in a better way?
Note: I have also tried it without overriding OnExecute and OnUpdate with the same result.
Using the Concurrent node to run some functionality OnStop is definetely a correct way of doing it.
OnStop is called whenever the action is stopped due to any reason. So the reason why OnStop is called twice in your case, is because you call EndAction again within OnStop. So it’s called once when you call EndAction and another time when the nested FSM exits.
To achieve what you want to do, you can simply do this:
1
2
3
4
5
6
7
8
publicclassCleanUp:ActionTask{
protectedoverridevoidOnStop(){
Debug.Log("Stoped");
}
}
Adding this on a Concurent node and when the nested FSM is exited, “Stoped” will be loged.
Let me know if this works for you.
Thank you for all the great help you are giving, it really is appreciated. And a reply given on a sunday can not be late 🙂
As you can see in the example I have sent you by email, whitch is very close to what I am doing in my project.
The Cleanup action is invoked when the nested FSM is entered as well as when it’s exited.
Entering one nested FSM and the switching to another calls the Cleanup a total of three times when it should only have been called once ( when i exited the first nested FSM ).
Hope this clarifies the issue i have a bit better.
Thanks for sending me the reproduction project. So I’ve found the issue, but not the solution yet. The problem is due to the order that OnGUI is called and how events are received. I am searching for solution and will post it here as soon as possible.
Here is a temporary solution:
Please open up FSM.cs and at line #86 in method EnterState right after the (newState == null) check please add the following: