GML i Need help with menu

André Luiz

Member
Okay..., I’m Brazilian and I’m just typing in English thanks to the Google translator, so don’t be surprised if there’s something weird written,


continuing, I was following a tutorial to make a menu, it seemed very simple and functional, (I'll leave the link at the end), but when I finally finished the buttons didn't work, only the exit button,

CODE:

(Create Event)
GML:
menu_x = x;
menu_y = y;
button_h = 90;

//buttons
button[0] = "New Game"
button[1] = "Load Game"
button[2] = "Options"
button[3] = "Quit"
Buttons = array_length_1d(button);

menu_index = 0;
last_selected = 0;
(Step_Event)
GML:
menu_move = keyboard_check_pressed(vk_down) - keyboard_check_pressed(vk_up)

menu_index += menu_move;
if (menu_index < 0) menu_index = Buttons - 1;
if (menu_index > Buttons -1) menu_index = 0;

if (menu_index != last_selected) audio_play_sound(MenuButtons_Sound,1,false);

last_selected = menu_index;

(Draw_Event)
GML:
var i = 0;
repeat (Buttons) {
    
    draw_set_font(Fonte_menu);
    draw_set_halign(fa_center);
    draw_set_color(c_dkgray);
    
    if (menu_index == i) draw_set_color(c_white);
    
    draw_text(menu_x, menu_y + button_h * i, button[i]);
    i++;
}
(Key_press_enter)
GML:
switch (menu_index) {
    
    case 0:
        room_goto_next()
        break;
    case 3:
        game_end();
        break;
}
the link I promised:
Here
 

eimie

Member
The code seems legit at the first glance 🤔

The buttons „load game“ and „options“ can‘t work, because they are not checked in your switch statement.
„Start game“ leads to the next room. Does such a room exist?
 

Slyddar

Member
As mentioned, you are missing the cases for Load Game and Options. Is that what you mean by saying the buttons don't work?

Every menu tutorial I've seen never shows you how to add submenus, the Options menu, and also none show how to change the options and have the values save in your game. So I created a tutorial that does that. If you need those features you can check out the first one here
 

André Luiz

Member
As mentioned, you are missing the cases for Load Game and Options. Is that what you mean by saying the buttons don't work?

Every menu tutorial I've seen never shows you how to add submenus, the Options menu, and also none show how to change the options and have the values save in your game. So I created a tutorial that does that. If you need those features you can check out the first one here
I was talking about the start button, the "case [0]" and not the others, sorry for not specifying, and about your tutorials, I will see and thank you for helping me by posting them here!
 

André Luiz

Member
The code seems legit at the first glance 🤔

The buttons „load game“ and „options“ can‘t work, because they are not checked in your switch statement.
„Start game“ leads to the next room. Does such a room exist?
yes yes, I was not talking about the other buttons, but about the "start" button, I wrote the text wrongly, sorry there, and yes, there is a next room, and this room is the game room, I still don't understand why you won't, ¯ \ _ (ツ) _ / ¯
 

Slyddar

Member
Add this line to your keyboard press enter event so you can see if the value of menu_index is 0 as expected.
Code:
show_debug(menu_index);
Which will show it as a popup box, or use this to show it to the output window
Code:
show_debug_message(menu_index);
These methods are very useful for helping yourself find the problems in your code. Also running in debug mode is also useful and needed to solve problems.
 

André Luiz

Member
Add this line to your keyboard press enter event so you can see if the value of menu_index is 0 as expected.
Code:
show_debug(menu_index);
Which will show it as a popup box, or use this to show it to the output window
Code:
show_debug_message(menu_index);
These methods are very useful for helping yourself find the problems in your code. Also running in debug mode is also useful and needed to solve problems.
nothing happened :/
 

Slyddar

Member
Ok, so let's think about what you've done. We are trying to see what the value of menu_index is when you press enter. If the line runs where you've placed it, then we know it's zero, but we already know it's zero as we are in case 0. Instead place it outside of the switch statement so we can see it's value when the event runs.
 
FIY, the function array_length_1d is deprecated in version 2.3+ and should be replaced by array_length
Now, I don't think this is breaking games yet, but something to consider when following older tutorials.
 
Top