Common mistakes (discussion)

A common mistake I made when I was a beginner in GDevelop is how to correctly toggle a boolean variable between true and false for case-specific selections.

The challenge arises when attempting to ensure that only one specific block executes based on the current state of the boolean variable. If not managed properly, modifying the boolean variable inside conditional statements can lead to unintended consequences, such as triggering multiple blocks sequentially when only one should execute per cycle.


Expected behavior:
When ‘selector’ is true, specific actions are triggered, and then ‘selector’ should switch to false. Conversely, when ‘selector’ is false, different actions are triggered, and ‘selector’ should toggle back to true. This may ensure that actions alternate based on the current state of ‘selector’ during each cycle (frame).

To try to achieve this behavior (and in lack of a dedicated case-selection block/module for this), usually a beginner will obviously jump to this approach:

simple_selector_issue

Obviously for non-beginners, the problem with this approach is the “False Case” will execute immediately after the “True Case”, since ‘selector’ changing to ‘false’ in the “True Case” immediately triggers it.


This get even worse when the expected behavior is to execute a specific case based on the value of a non-boolean variable, and the executed actions requires to set the stage for the next case changing the ‘selector’ value.

multiple_selector_issue

While this may seems a logical setup to achieve this kind of behavior in the eyes of a beginner user, this setup will only trigger a chain reaction that will execute all cases at once.


Of course, you can resolve these issues by introducing an additional boolean variable to track whether a case has been executed in the current cycle or not:

simple_selector_solved

multiple_selector_solved

But this is not obvious to a beginner user, the forum is crowded with questions about this same issue, and this would be easily solved with the inclusion of a case-selection block (like “Select Case” or “Choice” blocks).


̵I̵’̵m̵ ̵a̵w̵a̵r̵e̵ ̵a̵ ̵b̵o̵o̵l̵e̵a̵n̵ ̵a̵l̵t̵e̵r̵n̵a̵t̵i̵n̵g̵ ̵a̵c̵t̵i̵o̵n̵ ̵w̵a̵s̵ ̵i̵n̵t̵r̵o̵d̵u̵c̵e̵d̵ ̵a̵t̵ ̵s̵o̵m̵e̵ ̵p̵o̵i̵n̵t̵ ̵t̵o̵ ̵s̵o̵m̵e̵w̵h̵a̵t̵ ̵s̵o̵l̵v̵e̵ ̵t̵h̵i̵s̵ ̵i̵s̵s̵u̵e̵ ̵i̵n̵ ̵b̵i̵n̵a̵r̵y̵ ̵c̵a̵s̵e̵s̵ ̵(̵n̵o̵t̵ ̵i̵n̵ ̵m̵u̵l̵t̵i̵p̵l̵e̵ ̵c̵h̵o̵i̵c̵e̵ ̵c̵a̵s̵e̵s̵,̵ ̵a̵n̵d̵ ̵a̵l̵t̵h̵o̵u̵g̵h̵t̵ ̵i̵t̵ ̵s̵t̵i̵l̵l̵ ̵n̵e̵e̵d̵e̵d̵ ̵a̵ ̵s̵p̵e̵c̵i̵f̵i̵c̵ ̵s̵e̵t̵u̵p̵ ̵t̵o̵ ̵w̵o̵r̵k̵)̵.̵ ̵H̵o̵w̵e̵v̵e̵r̵,̵ ̵i̵t̵ ̵s̵e̵e̵m̵s̵ ̵d̵e̵p̵r̵e̵c̵a̵t̵e̵d̵ ̵s̵i̵n̵c̵e̵ ̵t̵h̵e̵ ̵v̵a̵r̵i̵a̵b̵l̵e̵ ̵s̵y̵s̵t̵e̵m̵ ̵c̵h̵a̵n̵g̵e̵d̵.

Moreover, while it’s possible to resolve this issue with an additional check variable in the setup, I don’t understand why this process is unnecessarily difficult for beginners and time-consuming for advanced users, rather than simply including a ‘Choice’ logical block.

NOTE: please correct me if there is an intuitive, beginner-friendly, and accesible way to achieve this in GDevelop.

1 Like

Yeah, I like to call it the domino effect or chain reaction. I miss If… Then… Else like other languages have.

If you’re talking about toggle, it’s still there. It’s just been moved to the setting action.

1 Like

I see. Thanks for letting me know that the boolean toggle option was moved into the action settings.

However, this doesn’t resolve the issue of handling multiple choices.

Introducing ‘ElseIf’ or ‘Choice’ statements would make the engine automatically more intuitive for beginners and more efficient for everyone in terms of workflow.

1 Like

Yes. A Boolean toggle only helps with 2 choices.

I use your method with a changed variable. You can also wait until all of the choices are evaluated before changing the variable if it’s a simple sequence.

A=1 do something
A=2 do something
A=3 do something

If a >= 1 and a <= 3 then set a+1

There are other times when you can use a formula, a group or for each object or each child or use an object variable of a colliding object or even an array or structure to reduce it to just a couple of events.

You want to try to reduce repeating the same events when possible. Extension are another tool. Even when the extension doesn’t reduce the number of events it makes the main event sheet easier to read.

I’ve never used external events. For me, they break up the flow. It makes it harder for me to follow the execution.

1 Like