playMaker

Author Topic: if ___ and ___  (Read 3045 times)

greg

  • Junior Playmaker
  • **
  • Posts: 68
if ___ and ___
« on: March 05, 2013, 12:41:31 PM »
Is there a simple way to do:

IF ____ and ____ is true, within a single state? Right now i'm having to do some convoluted things just to check two things..

(example attached)

Thanks :)

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: if ___ and ___
« Reply #1 on: March 05, 2013, 01:07:59 PM »
the way you do this is by using 2 transitions. One should be fired if the condition is not met, the other if everything returns true.

So you could first use a BoolTest action and fire the FAILED event if the bool is not true. If it is true, do nothing. Then in the next action you can do a float compare. If it's false, then fire FAILED again, if it's true, fire FINISHED.

If the first condition (the bool) is false, then it will never even check the second condition, because it already left the state via the failed event. If it is true, then the second condition decides whether the AND codnition is either true or false. Hope that made it clearer ;)
Best,
Sven

greg

  • Junior Playmaker
  • **
  • Posts: 68
Re: if ___ and ___
« Reply #2 on: March 06, 2013, 06:08:20 AM »
So the failed event loops straight back to the state, thus making it check again?
thanks

EDIT:

tried your method in a state like this:

"walking?"     "walking!"
yep            >
                 <  not anymore


with "failed" tests like you said, looping back to start of its prospective state. Unity crashes and says there is an infinite loop. Could you by any chance show me an example of this technique working, it would really help my workflow. Thanks
« Last Edit: March 06, 2013, 06:46:49 AM by Greg Sergeant »

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: if ___ and ___
« Reply #3 on: March 06, 2013, 10:35:08 AM »
If you check once a frame whether the result should be true or not then use a "next frame"action. That way it will wait until the end of the frame to start all over. This will stop any infinite loop.
Best,
Sven

greg

  • Junior Playmaker
  • **
  • Posts: 68
Re: if ___ and ___
« Reply #4 on: March 07, 2013, 11:17:49 AM »
Could you tell me what i'm doing wrong here? I've tried "every frame" checked an unchecked and i've tried changing "next frame event" to "moving".

Every time it freezes the web-player upon preview, indicating that there's likely an infinite loop or something wrong with the code.

Thanks for your time.

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: if ___ and ___
« Reply #5 on: March 07, 2013, 12:17:21 PM »
the failed event must first go to a new state with a "next frame" action in it. This action is like the wait action, but it only waits one frame.

Without that next frame action it would go like :

Compare Int = false -> send FAILED event
FAILED event goes back to the same state. Then Playmaker runs the first action of the state which is
Compare Int = false -> send FAILED event
and of course the failed event still goes to the same state.

Since this would go on like this forever, it is called an infinite loop.
Often infinite loops are what you want. You may want an object to move each frame for example. That too would be an infinite loop.

The problem here is though that your infinite loop is running infinite times per frame, not once like the good ones do.
This is because PlayMaker runs stuff as fast as possible. In this example it checks the statement as soon as the FSM is loaded, sees that it's false, and then checks it again right when the previous statement returned false. Since time in games is based on frames, and your current frame is infinitely long because it loops your state forever, the statement will never even have the chance to turn true.

Now, the next frame action fixes this. With it you first check the statement, if it returns false , it will wait until everything else is done calculating (=> until the next frame starts), and then it will try loop back to the statement and check it again. Since it's a new frame, the statement may now return true.
But even if it never turns true, this way you will at max only check it 60 times per second. And that's quite nothing for a processor.
Best,
Sven

greg

  • Junior Playmaker
  • **
  • Posts: 68
Re: if ___ and ___
« Reply #6 on: March 07, 2013, 12:25:07 PM »
Thanks for the detailed explanation!

Think i came up against this issue before - assuming that each state runs once per frame. I was creating/deleting objects, and had to add a "wait: 0.01" for it to run properly..else it was trying to delete them faster than they were being created or something.