Problem with simple switch using varibles

It should be easy to make a simple switch with variables, but for some reason it does not work. I should be able to click on a item to open or close a layout. I shouldn’t have to use two separate buttons.

Classic, the first event runs, the variable is switched, instantly the second event is also true, it runs and the variable is switched again :sunglasses:

You need another variable, I like to call it “else”, that helps to detect if the first event was executed. Your events should looke like:

[code]Conditions: No conditions (Always)
Actions: Do = 1 to variable “else” // Reset the variable state to start a new check

Conditions: Cursor on object, Left mouse button released, Variable “varPackActivate” is = 0
Actions: Time scale = 0, Pause timer, Show layer, Do = 1 to variable “varPackActivate”
Do = 0 to variable “else” // Prevent the next event to be launched

Conditions: Cursor on object, Left mouse button released, Variable “varPackActivate” is = 1
Variable “else” is = 1 // False if the previous event was executed
Actions: Time scale = 1, Unpause timer, Hide layer, Do = 0 to variable “varPackActivate”[/code]

Of course it would look better with sub-events:

[code]Conditions: Cursor on object, Left mouse button released
Actions: Do = 1 to variable “else” // Reset the variable state to start a new check

    Conditions: Variable "varPackActivate" is = 0
    Actions: Time scale = 0, Pause timer, Show layer, Do = 1 to variable "varPackActivate"
             Do = 0 to variable "else"          // Prevent the next sub-event to be launched

    Conditions: Variable "varPackActivate" is = 1
                Variable "else" is = 1          // False if the previous sub-event was executed
    Actions: Time scale = 1, Unpause timer, Hide layer, Do = 0 to variable "varPackActivate"[/code]

Thanks. I ended up finding a other post of yours and used that. But I think I had to add a line in the scene start.
But my questions is why doesn’t my version work?
If the events are read from top to bottom action 2 should prevent condition 3 from activating action 3. Am I not wrong in thinking this is how most programming would work? I maybe I am off in my thinking here. Is this just something with GDevelop or am I just not thinking strait?
How are these conditions and events handled? Maybe this is why I am running into problems from time to time, not thinking the way this is when it handles them.
This acts as if if goes through all the conditions see what to run and not run and then does the actions for those such as condition group 1, condition group 2, condition group 3, action group 1, action group 2, action group3 instead of condition group 1, action group 1, condition group 2, action group 2, condition group 3, action group 3,.

All standard events are triggered at the same time and conditions of each event is checked from top to bottom (I believe).

Not really, the events are also trigerred from top to bottom each frame.

I’m confused then… So if it is the case and the second event is waiting for the first one, why dafly’s event doesn’t work?
I mean, the actions in both event are triggered when the mouse is released. So once the mouse is released, it trigger the actions of the first event but after, the actions in the second event should be not triggered before the mouse pressed and released again :confused:

The order of the events doesn’t seems to have any effects at least I have never noticed if it does :laughing:

The events are executed from top to bottom each frames.
So, during the frame when the mouse button is released, the condition “Mouse button is released” is true the whole frame : so both events conditions are true.

That makes sense thank you. :slight_smile:

I made a feature request here:

It is kind of ridiculous that we dont yet have an “else” and “elseif” condition in gdevelop :frowning:

2 Likes