playMaker

Author Topic: Forcing State Change From C# [SOLVED]  (Read 5949 times)

FelixDraconis

  • Playmaker Newbie
  • *
  • Posts: 5
Forcing State Change From C# [SOLVED]
« on: September 29, 2011, 06:02:45 PM »
Is there a way to tell a FSM to immediately change to a state?

There's a method ChangeState() which inexplicably takes an event.  Also, ActiveState is private, though I agree setting that would be a bad idea as it probably needs to do some state initialization.

I want to control my FSM externally.  As a very basic example, if the player interfaces with a Chair object the Chair will tell the player what action to do based on the state of the Chair.  [Face away, Play sit animation, Make sitting sound, etc]

Part of the problem is that with modern small games you need to be able to recreate your state on reloading.  For instance, the Chair will remember a character is already sitting and needs to force it to resume from that state.

I could populate every state with a list of transition events, but that would be a terrible idea.  Although it does make me wonder if I can set up global transitions a state will fall back to at any time.  There doesn't seem to be functionality for that, though.

My current workaround is to make a new FSM for every action.  It seems to be the only good way to create an externally influenced FSM system.  This isn't necessarily a bad thing, but it does seem like there might be a better way.
« Last Edit: November 05, 2011, 03:02:52 PM by alexchouls »

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Forcing State Change From C#
« Reply #1 on: October 04, 2011, 06:47:24 PM »
The Playmaker FSM is designed to change state based on events, that's why ChangeState takes an event.

You can create global transitions if you want the FSM to react to an event from any state. You could use Get Previous State Name and Goto Previous State after the global transition.

Or, in your chair example, just have the object manage a string variable as it changes state. The player FSM can then query that string variable to know what action to take...

FelixDraconis

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Forcing State Change From C#
« Reply #2 on: October 11, 2011, 04:06:26 PM »
How about that.  We've had teams digging around on this for weeks now and I don't think anyone has noticed global transitions before (or thought about their implications when seeing the option).

That does look useful.  I'll try it out and spread it around here.  Thanks!

michela

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Forcing State Change From C#
« Reply #3 on: November 29, 2011, 07:05:03 AM »
The Playmaker FSM is designed to change state based on events, that's why ChangeState takes an event.

You can create global transitions if you want the FSM to react to an event from any state. You could use Get Previous State Name and Goto Previous State after the global transition.

Or, in your chair example, just have the object manage a string variable as it changes state. The player FSM can then query that string variable to know what action to take...

Alex, I can see how to record current state or previous state but not how to goto a recorded state (only the previous state).   What is the point of being able to save the current state if you can't return to it? I missing something.

Any advice appreciated.

Thanks

Michela

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Forcing State Change From C# [SOLVED]
« Reply #4 on: November 30, 2011, 04:28:51 PM »
PlayMakerFSMs are designed to be event driven, so most of the time you should be concerned with the events that you send to the FSM rather than specific state names. This lets you control the transitions between states more explicitly, which becomes invaluable as your project grows.

However, if you really need to force an FSM into a state from any other state, use a global transition to that state. You could use an event with the same name as the state.

NOTE: Send Event in the last update doesn't let you send events as strings any more (sounds like you might need this if you've recorded state names...?). This will be addressed in the next update, but in the meantime you could use the old Send Event To FSM and either delete the Obsolete flag from the script, or ignore the warnings.

You can see obsolete actions in the Action Browser by unchecking Hide Obsolete Actions in the Settings Menu (the little cog).

Does that help? Maybe try to make your FSM more event driven (instead of recording state names)... but it's hard to say for sure without knowing the problem...