Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: KJIB on September 21, 2012, 12:03:19 PM

Title: How to control FSM execute order?
Post by: KJIB on September 21, 2012, 12:03:19 PM
Is there a built in way to determine FSM execute order?

Real example: A couple of us independently ran through tutorial 10 today and found that at the point where you first try to see the colour of a platform being changed it didn't work. The mechanism relies on one start FSM setting up some colours in some globals and a different FSM was meant to pick up these colours & apply them to the game object. Trouble is the FSMs got executed in a different order to that in the tutorial so the platform was set to black (RGBA=0,0,0,0 default I guess) instead of the colour we'd specified. I added a 0.1 second wait timer to the FSM that applies the colour & this fixed the problem (i.e. the other set up FSM now got to do the work it needed before the apply colour FSM continued).

This wait timer solution is not the sort of thing I ideally want us to rely on for predictability. Where'd I go wrong? Or, is there a right way to do this (preferably built in rather than me needing to add more variable/states/checks/waits etc.) and if not, any plans to add control over this important facet of FSM use?
Title: Re: How to control FSM execute order?
Post by: kiriri on September 21, 2012, 12:27:00 PM
would the " Fsm state test" action fit your needs? I use it all the time to make sure one FSM picks up where another one left of, just at the right moment. Alternatively if the other FSM hasn't been used at all, you could disable the component by default and then enable it when needed.

Both aren't really effecting your framerate (unless you use hundreds of different FSMs at a time, but then , with a solid combination of the two you should only have a few FSMs work at a time)

A third powerful solution is empty start states. Just trigger a global event on an independent state to start the fsm.(send event) . This shouldn't cost you anything at all. Once you again need to make sure that another FSM runs before that one, make another dead end, create another global event and let it wait for the other FSM to finish up and send the global event.

Well, I hope this is what you needed :)
Title: Re: How to control FSM execute order?
Post by: KJIB on September 24, 2012, 04:12:11 AM
In terms of a pure FSM, simply adding another state (to FSM 2nd in my order) & doing a send event from the FSM I want to run 1st would seem to be the answer. It also, in this case, adds a minimum overhead. I can see that this is not something that will affect most people but it just seems every so slightly clumsy (meaning that you need to complicate your diagram). More of an issue when it is a potential issue in a tutorial.

On the otherhand, having it as a problem to solve in a tutorial has been useful & was easy for me to home in on but the other people are not coders & may have had problems if I'd not already jumped through this hoop. In the interests of being helpful, I think that this issue probably should be covered in the tutorial where the problem occurs.
Title: Re: How to control FSM execute order?
Post by: dasbin on September 25, 2012, 03:23:10 PM
The game I'm working on right now uses initial Wait actions on FSM's that rely on the setup of others to complete a task. I agree it is not ideal and if starting again I would do something different, but I don't really want to redo 500+ FSM's at this point.

The problem with dealing with such a massive number of FSM's is that having each of them test the state of the setup FSM is also quite an annoying thing to have to do, so I'm not sure what the ideal solution would be.
The Wait solution has been fully reliable so far, but I am just waiting for the time when I run into a situation where everything breaks because the FSM is not executed in time on a very slow system, or when something needs to happen much more quickly than a Wait allows. I have already run into a situation where 0.1 seconds was not enough time to step through multiple states in the Setup FSM and I had to increase the Wait to 0.2 seconds.

The best solution I can think of is to broadcast an event from the Setup FSM to say "OK, I'm done" which triggers subsequent states in all other FSM's which rely on it. However, if those same FSM's must rely on multiple different Setup FSM's then things get too complicated very quickly and a Wait action is actually much simpler.
Title: Re: How to control FSM execute order?
Post by: jeanfabre on September 28, 2012, 02:48:37 AM
Hi,

 Indeed, events broadcasting is a lot safer than wait actions!

another way is to have your logic embedded in prefabs, and your manager simply instantiate them prefabs when the time is right. thus the fsm doesn't wait... it starts when the gameObject it is attached to is created.


bye,

 Jean
Title: Re: How to control FSM execute order?
Post by: jeanfabre on September 28, 2012, 02:49:45 AM
Hi,

 And another way is to use boolean flags, and have an fsm watching for the bool flag to change and fire an event when it has changed. This way one fsm when done sets a flag to true, and other fams that where watching this flag will detect that and proceed straight after.

bye,

 Jean
Title: Re: How to control FSM execute order?
Post by: dasbin on September 28, 2012, 03:01:17 AM
The problem is that if an FSM relies on the setup of a bunch of other FSM's (5 or more), broadcasting or testing bools from each becomes very tedious and complex and a Wait is actually much simpler.
Title: Re: How to control FSM execute order?
Post by: kiriri on September 28, 2012, 09:11:44 AM
The problem is that if an FSM relies on the setup of a bunch of other FSM's (5 or more), broadcasting or testing bools from each becomes very tedious and complex and a Wait is actually much simpler.
I kindly disagree, see, of course it'll take a tad longer, but it would still work out if you say wait for 5 fsms to finish you'd:
1) create 5 bools
2) have each fsm set one bool on finish, then have them each send the global event "Go" to that one FSM that's waiting
3) in that one fsm, have a state that is triggered on "Go". In it, use 5 bool test actions. Create 2 transitions for that state: 1 is FINISHED, the other is FAILED . Now set the bool test actions to return FAILED if the bool is false. Now, if all of your bools are true, the state will return FINISHED, since that is what's triggered if you run through a state without triggering an other event.

This way there really is no large setup and no performance issues whatsoever.
And let's be honest, we wouldn't be able to work any more efficient in a normal scripting environment, so why'd you think it should be better here?

EDIT:
alternatively you could use check state actions here too :) You would have to stick to the naming, but you'd save the 5 bools :D
Title: Re: How to control FSM execute order?
Post by: jeanfabre on October 01, 2012, 01:27:57 AM
Hi,

 yes, I do sometimes have Fsm with strict state names that other Fsm check, so they know that is that Fsm is on state "a given state" then it means something for them to act upon. It does save a lot of variable creation and maintenance indeed, and the complexity of the Fsm can be of a greater magnitude to decide whether it should be on that state or not.

this is one thing that could be improved in future playmaker version, that Fsm's state are also binded like variables. so that if you reference a state, if it happens to change, the reference is kept.


bye,

 Jean