State Machine Help [SOLVED]

How do I…

I am trying to make 3 states for a state machine.

What is the expected result

Pressing only spacebar, you toggle through 3 player states depending on game context.

What is the actual result

Here is me just trying to toggle between the two states of notCasting and casting.

This does not work and seems to be endlessly looping. I’m using the “playerState” variable as my condition. I assumed a “Trigger Once” would prevent the states from endlessly looping but I guess I’m wrong?

I have referenced the state machine documentation as well as the Paulera post and the YT video (which only shows sequential states). I feel like I have this set up in a similar fashion, yet I’m still having issues.

For reference here is how I had it set up in Construct and it worked perfectly. I initially tried something similar in GDevelop with no success.
image

  • Do I need more conditions besides a text variable check?
  • Someone in Discord mentioned boolean buffers but I’m a bit lost on that. Is that a missing piece?
  • Do I need to be using external sheets here? I know the example used them but I thought the variable check would be doing something similar.

Hello,
i suggest you tu use a temporary variable.

It’s a workaround to substitute the “else” command.

2 Likes


This works for two states.

1 Like


Press Space and look at Jumps text

This is for going 0 1 2 > 0 1 2 > 0 1 2 …
So it is one way loop

IF you need to also be able to go 2 1 0 > 2 1 0 > 2 1 0 …
1st you need to add boolean variable and put it in same condition that have space key is pressed
Now you duplicate event
And in duplicate instead of add 1 you change it to subtract 1 in action
And now one event needs to have if that that boolean is TRUE while other event if that boolean is FALSE
And now you need to add some event to control state of that boolean
Also you need to duplicate last event you see on screenshot above where it checks in condition if variable is equal or greater to 3 and switch it to 0 if it is
You duplicate that event and simply set so that if that variable is equal or less than -1 set it to 2
Actually i think checking if it is greater than 2 and less than 0 would do the work also but whatever

And now you have 2 way loop

If you need any of above loop while also be able to at will loop only between 0 and 1 while skipping 2

You duplicate event with if it is equal or greater than 3 set it to 0
You change that to equal or greater than 2 set it to 0

If you went for 2 way loop you do the same for
Less or equal than -1 set it to 2
Instead you set it to 1

And now you create another boolean and shove it in to each event while 2 of them need to have true and other false

And now you are covered for any kind of scenario
Do not forget trigger once

1 Like

So, at a super high level:

All events are processed every frame, and trigger once resets as soon as the conditions are false. Because your top level condition becomes false with your last action, the events proceed to the next one.

If you want the states to be focused around the space key, jumpingj’s method will work. Alternatively, you can set up actual “key pressed” states. Add a condition that is a boolean variable of “SpaceReleased” = True.
As the last action of each event, set the boolean to false.

Set up another event that sets the boolean back to true if the Spacebar is pressed (inverted). Do not use the “released” condition otherwise it won’t work the first time as it’s never been pressed.

1 Like

Thanks for the help, everyone!

The fix recommended by @jumpingj and backed up by @Silver-Streak worked well here. This is what enabled me to set up the state machine that I outlined above.

Marking this as solved.

2 Likes