playMaker

Author Topic: How to "Reset" a Finished FSM  (Read 4991 times)

ciabaros

  • Playmaker Newbie
  • *
  • Posts: 13
    • View Profile
How to "Reset" a Finished FSM
« on: April 13, 2013, 10:27:10 AM »
Problem Setup:
  • A regular FSM (not SubFSM) is configured to NOT "Reset on Disable" in inspector (by design, I need it to sometime continue after re-enable).
  • Once it reaches a certain state, it calls "FinishFSM", and gets put into "Fsm.Finished" state and disabled.

Without enabling the "Reset on Disable" feature, I need to manually restart this FSM (via code, from another FSM). I tried PlayMakerFSM.Reset( ) and Fsm.Reset( <component> ), and both just reset the FSM structure itself (by clearing the component's data), to the default 1-state FSM.

I Tried:
  • Fsm.Init( )
  • Fsm.Reinitialize( )
  • PlayMakerFSM.Reset( ) and Fsm.Reset( * ) as mentioned above.

Of course, I'm explicitly enabling it first.. all works if the "Reset on Disable" flag is enabled on this FSM component, but I need to do the same action that this flag initiates in the backend, in my custom Action code.. Help :)

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3629
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: How to "Reset" a Finished FSM
« Reply #1 on: April 13, 2013, 11:11:10 AM »
Not at my computer to check, but try Fsm.Start();

ciabaros

  • Playmaker Newbie
  • *
  • Posts: 13
    • View Profile
Re: How to "Reset" a Finished FSM
« Reply #2 on: April 13, 2013, 11:17:27 AM »
Sorry I didn't explicitly mention, I was running Start( ) in every case. But I have since found a workaround:

It feels like some weird technicality, but it works!

It seems that the way you programatically "Reset" an FSM which does not "Reset on Disable", AND is Finished via FinishFSM ([fsmComponent].Fsm.Finished==true), is to:
  • Temporarily enable its "[fsmComponent].Fsm.RestartOnEnable" flag
  • Enable the FSM by "[fsmComponent].enabled = true"
  • Stop the FSM by "[fsmComponent].Fsm.Stop( )"
  • Start the FSM by "[fsmComponent].Fsm.Start( )"
  • Restore the previous value for the "[fsmComponent].Fsm.RestartOnEnable" flag.

That was fun. Hope this saves anyone the hours it took me to figure out by trial and error.

Edit:
Only Fsm.Stop( ) cares about the RestartOnEnable flag. So enable can happen after the flag is reverted, and hence, the entire sure-resetting mechanism is just:
  • Temporarily enable its "[fsmComponent].Fsm.RestartOnEnable" flag
  • Stop the FSM by "[fsmComponent].Fsm.Stop( )"
  • Restore the previous value for the "[fsmComponent].Fsm.RestartOnEnable" flag.
« Last Edit: April 13, 2013, 12:59:53 PM by ciabaros »