playMaker

Author Topic: How to add cleanup code/actions at "OnExit" State event?  (Read 3344 times)

Andy22

  • Junior Playmaker
  • **
  • Posts: 99
How to add cleanup code/actions at "OnExit" State event?
« on: October 17, 2013, 11:14:13 AM »
Hi,

i have 2 states and the first uses a global transition, that can happen anytime and i need to-do some cleanup that is related to the second state, but i cant find a way to execute actions if a state just "exits", so i can make sure the cleanup code is run always?

The state systems i worked before had a OnEnter/Exit callback, where u could put in your init/cleanup code, so how do u handle such situations in playmaker?

In my case i'm doing some animation/tweening actions encapsulated in the second state and i need to stop/reset/disable things if the state is "interrupted" aka. exited before all sequential actions did "normally" execute and send the "FINISHED" event.

I tried to use a second cleanup state, but there is no way to execute this cleanup state just if a other state "exits".

The only "hack" i could come with, was to move the cleanup actions, directly after the global event, but thats not the same, since now the cleanup code is at the wrong location and also cant be easily reused if i add more events?

any idea how to handle such situations?

bye Andy
« Last Edit: October 17, 2013, 11:16:11 AM by Andy22 »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How to add cleanup code/actions at "OnExit" State event?
« Reply #1 on: October 21, 2013, 03:05:14 AM »
Hi,

 Interesting case. Indeed there is not built in "On Exit" space within a state.

 By this I mean that if you "jump" from one state to another in a given fsm, then your state is exited with no further notification to your actions.

 IF your state "transits" to another state, then you can insert a state in between and do your clean up here.

 in your case, I would add a new global event referencing the moment you need to clean up, so your state doesn't jumps to the target state, but is first redirected to a clean up state, and then wiat there for the final global event to be fired.

 Does that sound like a solution for your case?

 the problem with this case is that it's not really the way Finite state machine works in theory,

As I think of it, there is another way, maintain a bool flag called "dirty", and you set to true with you enter a state, if you jump to another state, that state checked for "dirty" and if true, will go trhough a clean up state, and then follow the actually expected procedures. the clean up state mark the "dirty" flag down when proceeding.

bye,

 Jean

Andy22

  • Junior Playmaker
  • **
  • Posts: 99
Re: How to add cleanup code/actions at "OnExit" State event?
« Reply #2 on: October 23, 2013, 05:16:42 AM »
Thx for the suggestions, those also crossed my mind.
The problem is that i really don't want the cleanup code to rely on other logic/states. The state that "dirty's" the system should cleanup after itself, not rely on some proper setup external logic.
This would be like moving the destructor or OnDestroy() call out of a class.

The problem is that if i want to reuse and copy/paste the state, i now also have to maintain/copy the cleanup logic and wire all up properly.

What i ended up doing was to send a message from Playmaker to a "cleanup" script, that does all the cleanup work. This works, but also requires extra/propper setup.

Maybe add some special "onExit" container as action, where we can put in all onExit actions, that gets executed if a state is exited? A other way would be to define some labeled sections, where all actions after the label are considered (onExit) or maybe mark actions and color them? Just some wild ideas :p

I just worked with 3 other FSM systems so far and all had similar events exposed to the programmer OnEnter/OnExit/OnUpdate or BeginState/EndState and i found those hooks quite handy, since its similar on how u work with classes (ctor/dtor). The OnEnter() state is kinda simulated, since most events have a "every frame" option which would map to "OnUpdate", while if disabled would be "OnEnter".

thx Andy
« Last Edit: October 23, 2013, 05:53:08 AM by Andy22 »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How to add cleanup code/actions at "OnExit" State event?
« Reply #3 on: October 23, 2013, 07:09:42 AM »
Hi,

 Yes, It's a tricky situation, the "everyframe" system also has many limitations since sometimes we need more control about when to perform the action, on Fixed update, on late update, etc.

Bye,
 
Jean

Andy22

  • Junior Playmaker
  • **
  • Posts: 99
Re: How to add cleanup code/actions at "OnExit" State event?
« Reply #4 on: October 23, 2013, 11:27:27 AM »
Yeah just checked the FsmState class, it actually has a OnEnter/OnExit/OnUpdate, but is exposed only to the actions and not the GUI itself.
A other way to solve the execute "once" vs "every frame" would be to expose a List of actions for each internal call, so that OnEnter/OnExit/OnUpdate each has a separate List<FsmStateAction> and exposes this via 3 tabs per state to the gui, in which u put whatever action u like.
This way u can clearly see what executes when and the action itself only needs to have a "OnExecute/OnInit" call, since OnUpdate is handled by the state itself for each action. U could than also setup when the OnUpdate() is called for each action or for all, which would be transparent to the action itself.

Just some ideas :p

bye Andy
« Last Edit: October 23, 2013, 11:30:23 AM by Andy22 »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How to add cleanup code/actions at "OnExit" State event?
« Reply #5 on: October 24, 2013, 01:28:50 AM »
Hi,

 Check the quaternion set of custom actions, it features custom editor to address just this, I guess you can totally add an option to process something on state exit this way. So totally possible indeed if you use custom actions. But regular action will not work this way as is unfortunatly.

https://hutonggames.fogbugz.com/default.asp?W967

bye,

 Jean