• 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!

Legacy GM While loop exiting too soon

C

Crossoni

Guest
For some reason, if my fire_rate is 5 and the stat chooses "fire_spd", my draw event sometimes draws text "fire_rate" instead. My code does increase the values for fire_spd, but for some reason it draws different stat text in my draw event. With my understanding the code should at the end choose "fire_spd" because my fire_rate is 5, then change leveling to false, break out of switch statement, exit while statement and draw text "fire_spd". Any help is appreciated.
Code:
fire_rate = 5;
fire_spd = 5;

//Step event
///Leveling up small stat
if (expr >= 1000*mpl) {
    lvl += 1;
    mpl += 1.25;
    while (leveling) {
        stat = choose("fire_rate","fire_spd")
        switch (stat)  {
            case "fire_rate":
            {
                if (fire_rate > 5) {
                    fire_rate -= 1;
                    leveling = false;
                    break;
                }
            }
            case "fire_spd":
            {
                if (fire_spd < 15) {
                    fire_spd += 0.25;
                    leveling = false;
                    break;
                }
            }
        }
    }
    leveling = true;
    draw_lvl_up = true;
    alarm[9] = 280;
}

//Draw event
if (draw_lvl_up) {
    draw_text(x,y-64,stat);
}
 

2Dcube

Member
Because there is no break after case "fire_rate" it continues on and executes the code under case "fire_spd".
Switch statement:
Code:
Switch(variable)
{
  case "one":
  //some code
  break;

  case "two":
  //some code
  break;
}
 
M

maratae

Guest
For some reason, if my fire_rate is 5 and the stat chooses "fire_spd", my draw event sometimes draws text "fire_rate" instead. My code does increase the values for fire_spd, but for some reason it draws different stat text in my draw event. With my understanding the code should at the end choose "fire_spd" because my fire_rate is 5, then change leveling to false, break out of switch statement, exit while statement and draw text "fire_spd". Any help is appreciated.
Code:
fire_rate = 5;
fire_spd = 5;

//Step event
///Leveling up small stat
if (expr >= 1000*mpl) {
    lvl += 1;
    mpl += 1.25;
    while (leveling) {
        stat = choose("fire_rate","fire_spd")
        switch (stat)  {
            case "fire_rate":
            {
                if (fire_rate > 5) {
                    fire_rate -= 1;
                    leveling = false;
                    break;
                }
            }
            case "fire_spd":
            {
                if (fire_spd < 15) {
                    fire_spd += 0.25;
                    leveling = false;
                    break;
                }
            }
        }
    }
    leveling = true;
    draw_lvl_up = true;
    alarm[9] = 280;
}

//Draw event
if (draw_lvl_up) {
    draw_text(x,y-64,stat);
}
Don't use a while loop unless you absolutely need it. I'm not even sure how the loop it's not just locking up your game.
Use a "if" instead, why don't you?
 
C

Crossoni

Guest
Because there is no break after case "fire_rate" it continues on and executes the code under case "fire_spd".
Switch statement:
Code:
Switch(variable)
{
  case "one":
  //some code
  break;

  case "two":
  //some code
  break;
}
I knew that there was something simple wrong with it, I was just again so blind to my own code... thank you so much!
 
C

Crossoni

Guest
Don't use a while loop unless you absolutely need it. I'm not even sure how the loop it's not just locking up your game.
Use a "if" instead, why don't you?
But I do need while loop there, since if I use if statement and it chooses "fire_rate" then it does not level up any of my stats and continues through the code. Plus this code is not complete yet, when it is it's going remove stats from the stat list if they are at "max", and if all of the stats are at max then it will change leveling to false.
 
Top