Windows menu doesn't work, variable goes crazy

E

epYa

Guest
Hey,
I'm fairly new to GML and I tried to do a starting menu.
So the menu works from the navigation, you can go up and down with the arrow keys.
The variable location tells me, which button I'm currently at and it also paints the text red, that works great!
However if I want to check if location is 3 and the enter button is pressed, it works even if location isn't 3.

I tried to draw location and somehow all the numbers seem to overlap. What's wrong with my code?

This is how the Step-Event of my code looks like:
Code:
if(keyboard_check_pressed(vk_down) && (location != 3)) {
    location += 1;
}else if(keyboard_check_pressed(vk_down) && (location == 3)){
    location = 1;
}
else if(keyboard_check_pressed(vk_up) && (location != 1)) {
    location -= 1;
} else if(keyboard_check_pressed(vk_up) && (location==1)) {
    location = 3;
}



if(location == 1){
    colorbuttonc = c_maroon;
    colorbuttonn = c_white;
    colorbuttonq = c_white;}
else if(location == 2){
    colorbuttonc = c_white;
    colorbuttonn = c_maroon;
    colorbuttonq = c_white;}
else if(location == 3){
    colorbuttonc = c_white;
    colorbuttonn = c_white;
    colorbuttonq = c_maroon;}
    
if(keyboard_check_pressed(vk_enter)) {
    switch( location ){
            case 1: // Resume

            break;
            case 2: // newgame
            break;
            case 3: // Exit
                game_end();
            break;
        }
}
Draw Gui event:
Code:
draw_text_colour(350,450,"<Continue>",colorbuttonc,colorbuttonc,colorbuttonc,colorbuttonc,image_alpha);
draw_text_colour(350,500,"<New Game>",colorbuttonn,colorbuttonn,colorbuttonn,colorbuttonn,image_alpha);
draw_text_colour(350,550,"<Quit>",colorbuttonq,colorbuttonq,colorbuttonq,colorbuttonq,image_alpha);
draw_text_colour(350,600,location,colorbuttonq,colorbuttonq,colorbuttonq,colorbuttonq,image_alpha);
Thanks in advance!
 
While there are different ways I'd go about this, I don't see anything inherently broken. Are you saying that the game exits even when "Continue" is selected?
 
E

epYa

Guest
Yes, exactly.
For example when I press down and up it should be on "continue" and the location should be 1, however location seems to be 1,2 and 3 at the same time.
 

TsukaYuriko

☄️
Forum Staff
Moderator
So we're dealing with Schrödinger's location here? :p

That's a tricky one. I don't see anything that could cause this in the code in the opening post. Is this the only thing in the room and is there only one instance of it? Does the game still end if you comment out the line that's supposedly causing it and then do what you did to cause it before?
 
E

epYa

Guest
Wow Schrödingers location is pretty accurate haha. However you were right,
I had an "<press any key to continue>" object, which created my menu object, but it didn't stop creating new ones.. Such a stupid mistake and wasted 2 hours of my life..
Thank you very much!

Also is there any smooth ways to make sure that a if statement is only used once? I solved it with adding an extra temporary variable which I set to false, but it seems like a 💩💩💩💩ty solution.
 
N

NoFontNL

Guest
If I get it right, you mean you want to only create one menu object. Then I'd go for if(!instance_exists(obMenu)){instance_create(x,y,obMenu)}
 
Top