How to toggle between 2 (OR MORE) states using the same button?

Got a funky-colored Earth orbiting the Sun, let’s call its starting orbit “0”. I’m thinking to have the Earth jump to a wider orbit, call it “1”, when (in this case) the left mouse btn is clicked. Click again to change the orbit radius back to “0”. May want more orbit “lanes” for it in the future.

The issue: The Earth doesn’t appear to move out of its orbit 0!

There are 2 ‘orbits’ the Earth can be in, set in a object number variable that can be 0 or 1.
(I’ve considered changing it to a boolean, and using a toggle action instead of events below, but want to try figuring this out so I could do any number or orbits not just 2.)

I have the “EllipseMovement” extension installed.

See screenshots….

earth orbit logic loop 3-preview

Subevent 1 tests if the Earth’s variable orbit=0, then moves the Earth outward, then changes the orbit=1.
Subevent 2 tests for orbit=1, if true it moves Earth inward, then changes the orbit=0.

I think Subevent 2 happens right away though since that last event just set orbit=1!

Is there a way to only run the Subevent 1 code if its true, then skip Subevent 2 and jump out to the root level of the parent event?

Also I still don’t fully get order of execution. When to use subevents or not, or to use the “or” “and” or “not” conditions.

This is probably a very common logic issue…? But I’m still new in this arena.
I want to say I’ve seen this covered in one of the GDevelop videos I’ve been binge watching on YouTube, but of course where, and did I imagine it?

HEEEELP! :face_exhaling:

If you want to use the same button for toggling, like opening and closing a menu, you need to use a variable that checks if the button is being pressed.

Kinda like…
Condintion

  1. “a” key is pressed
  2. Scene boolean “buttonpressed” = False

Action

  1. Change scene boolean “buttonpressed” “set to” “True”
  2. Do what you want to do…

then have a reset button event like…
Condition

  1. “a” key is pressed (Invert condition to be “is not pressed”)
  2. Scene boolean “buttonpressed” = True

Action

  1. Change scene boolean “buttonpressed” “set to” “False”

And your done :slight_smile:

1 Like

Press left mouse button

When to sub event or not you ask?
You see i want all these 3 actions to happen when i press LMB
YET i only want change scene variable SET TO blablabla run always when LMB is pressed
And other 2 i only want to get activated ONCE and then not again even if LMB is still pressed

So in this case actions in sub event will get activated ONCE when LMB is pressed but not again until i release LMB and press it again

Imagine it as ONE TIME use coupon for discount

And so
mod (Variable, Number)
Means
Loop this variable in this number
So if you put there 3
Then it it will loop from 0 to 2 so 3 numbers 0 1 2
Cause it always starts with 0
So if you put there 9 it will go 0 1 2 3 4 5 6 7 8 < 9 numbers since it started from 0

And now in sub event you add +1 to that variable
Where in 2nd action in subevent you change x pos of torch to
Position of player center on X axis + 7 and you multiply it by value of TorchDistance variable
So you dynamically adjust position multiplied by some variable
You could go with checking
If variable = 4 then change position of torch to bla bla bla
If variable = 5 then change position of torch to ble ble ble

But i wanted to show you if you that you can get even distances with one event using var as multiplier
Where you could shove in few more variables there to have not even distances
But whatever

1 Like

There are so many methods.
You can use a formula. orbit * 70
You use multiple events orbit=0, orbit =1
You could use an array orbitDist[orbit]

First off, thanks for all who commented. Been trying to get back to this but I’m not always the most focused with what time I have!

aside to ZeroX4...

Thanks @ZeroX4 for the extra tip on subevents and mod()…more to chew on. I’ve yet to encounter or look into mod() in GDevelop. I’ve heard of it in other code languages as modulo, to get the remainder when dividing two numbers. Is this different I guess?

I will say I wasn’t imagining things, I DID see some video related exactly to this topic. Funny that, it was one of the official GDevelop vids, and has corresponding tutorial in the wiki. For future readers stumbling on this, links:

https://wiki.gdevelop.io/gdevelop5/tutorials/how-to-make-togglable-states-with-variables/#32-option-2-using-a-state-variable
And corresponding video :

So with the help I got here and another server, and the official video and wiki links I just noted, I’ll say this one is…SOLVED

1 Like