I have an other demand (sorry :'( )
Will it be possible to have a sort of “OnExit” when for example FSM Exit the current state, or BT change its running leap ?
It will be convenient to have it with the case where you want an action to finish, but you also want a callback when the state exit. Actually we have a OnStop callback only when the action End, but not when exiting the current running state.
Sorry for the late reply due to summer vacation and no problem! 🙂
Hmm… Right now, when a state exits, EndAction is called on all ActionTasks assigned to the state. However, if the ActionTask is already stopped, the callback OnStop is not called again (to avoid calling it multiple times). I could add something like “OnForcedStop” which will be called regardless whether or not the action is still running, which will result in it being called for example, when the FSM state exists as well.
Here are the changes I’ve made if you want to please try out and let me know if that indeed works for you the way you want:
Please open up ActionTask.cs and change lined #128-130 to be like this:
1
2
3
4
5
6
if(status!=Status.Running){
OnForcedStop();
return;
}
Then also, of course add a virtual method at the end like so:
1
2
3
4
///Called if EndAction is called while the action was not running anyways. Could be called multiple times.
It’s when fsm current state stop. Not when the action task is running and forced to stop. It will be called on all ActionTask without checking their state when the actual FSM current state stop.
So we can do something regardless of the state of the actionTask but in regard of the state of the FSM State.
And for BT, it will be when the BT change its current running “leaf”.
Scenario on our game : Launch a sound on Execute (when the state begin in fact) -> Stop the sound when the current state stop.
For the moment I have a empty running Sound Task because if I EndAction, my sound End… because I can’t detect the actual End Of State.
Hello and very sorry for late reply due to summer vacation!
Maybe the naming of the method (“OnForcedStop”) brings confusion, but I think this does what you want at least in the FSMs context. It should be called when the state exits/stops (even if the action is already stopped). Have you tried adding this to check it out?
Regarding Behaviour Trees this won’t work at all the way you suggest indeed however. This (OnForcedStop) is instead called when the action node (or tree altogether) resets. But in Behaviour Trees, the leaf is changing when the action has finished (returns Success, Failure or anything but Running), in which case the normal OnStop callback is called.
Can you please give the above change in my post a check in the context of FSMs if you haven’t already and let me know?