• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED switch statement loop even with break;

GML:
if ((menuability_commited != -1) and (global.menuability == true))

    switch (menuability_commited)
    {
        case 4:
        {
         
        instance_create_layer(oPlayer.x, oPlayer.y, "Held_Tools",oFireorb);
        break;
        }

        case 3:
        {
        show_debug_message("Ooga booga"); break;
     
        }
     
        case 0:
        {
        global.menuability = false; break;
     
        }
 
    }
this is the code where i have an issue, i tried doing return 1; instead of break, before or after the statments after the case.
but it still loops.
but when i remove the break; it still loops. im not surw what im doing wrong here.
i draw the menu like this normally:
step event:
GML:
if (keyboard_check_pressed(ord("O")) and (global.menuability == false))
{
    global.menuability = true;
 
}
else
{
    if (keyboard_check_pressed(ord("O")) and (global.menuability == true))
    {
    global.menuability = false;

    }
}

draw gui event:
GML:
if (global.menuability == true)
{
    draw_sprite_ext(sBookMenu51,0,menu_x,menu_y,0.5,0.5,0,c_white,1)
ignore that it doesn't have } at the end, i just didn't show the entire code, because the draw event works fine.


but the issue here also is that, when i use the botton that activates case 0: it makes global.menuability false; permanently, unlike when i use keyboard_check_pressed when it only does it once.
I've tried googling several issues around this, and scratched my head around this for a couple of days.
 

chamaeleon

Member
switch() statements do not loop. You may be executing it multiple times (perhaps thanks to events running all the time, executing code over and over), but the statement itself does no looping at all.
 
switch() statements do not loop. You may be executing it multiple times (perhaps thanks to events running all the time, executing code over and over), but the statement itself does no looping at all.
maybe im wording it wrong then, it keeps creating instances of this object, instead of just one.
same with the debug, it just spams the text "ooga booga", non stop. so is there a way from stopping it being a permanent case?
like it's in the same room as the player atm, this is not in a different room.
so is there a way to force it to only execute codes in the cases, once?
 

FrostyCat

Redemption Seeker
If your first piece of code is in a Step event or another similar repeating event, then it will repeat every frame, and the switch statement will repeat with it as long as both of the conditions hold true. Now look at what your cases are doing:
  • 4: It creates an instance of an object. It changes neither menuability_commited nor global.menuability, so it will repeat again next step.
  • 3: Same deal as 4, but it shows a debug message instead. It changes neither menuability_commited nor global.menuability, so it will repeat again next step.
  • 0: It changes global.menuability to false, so at least that part of the condition will be false for the next step. But menuability_commited stays at 0, so when global.menuability becomes true again, you get locked onto the same choice as last time.
Of the 3 cases you used, only one has any ability to stop the condition for future steps, and even then it is not doing anything about menuability_commited.

If you want code to not repeat on future steps, not only do you have to gate it behind an if statement, you have to work towards breaking that condition within the statement block. Something like this:
GML:
if ((menuability_commited != -1) and global.menuability) {
    switch (menuability_commited) {
        ...
    }
    menuability_commited = -1;
    global.menuability = false;
}
 
If your first piece of code is in a Step event or another similar repeating event, then it will repeat every frame, and the switch statement will repeat with it as long as both of the conditions hold true. Now look at what your cases are doing:
  • 4: It creates an instance of an object. It changes neither menuability_commited nor global.menuability, so it will repeat again next step.
  • 3: Same deal as 4, but it shows a debug message instead. It changes neither menuability_commited nor global.menuability, so it will repeat again next step.
  • 0: It changes global.menuability to false, so at least that part of the condition will be false for the next step. But menuability_commited stays at 0, so when global.menuability becomes true again, you get locked onto the same choice as last time.
Of the 3 cases you used, only one has any ability to stop the condition for future steps, and even then it is not doing anything about menuability_commited.

If you want code to not repeat on future steps, not only do you have to gate it behind an if statement, you have to work towards breaking that condition within the statement block. Something like this:
GML:
if ((menuability_commited != -1) and global.menuability) {
    switch (menuability_commited) {
        ...
    }
    menuability_commited = -1;
    global.menuability = false;
}
oh that makes so much sense, for some reason i thought break would just stop it anyway. i guess that's where my lack of understanding stood in the way. Thank you all again!
 
Top