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

GameMaker 2D array for option menu

K

Kyle Rider

Guest
Hey all, looking at changing my option menu from a 1D array to 2D.
The problem I have with it being a 1D array is controlling the menu.

I have 3 columns in my menu. being a 1D array the pressing left or right should change the rows but it doesn't function all the time and is just horrible.

I will start tinkering with how to draw a 2D array but if any of you can point me to read anything the manual really doesn't help.

Here is my current code.
Code:
for (m = 0; m < array_length_1d(menu); m += 1)
{

    if (m <= 3)
    {
        if move = m
        {
            draw_set_halign(fa_right);
            draw_set_valign(fa_top);
            draw_set_color(c_blue);
            draw_set_font(f_dialogue);
            draw_text(display_get_gui_width()/2 - 400, 136 +(64* m),string(menu[m]));
        } else
        {
            draw_set_halign(fa_right);
            draw_set_valign(fa_top);
            draw_set_color(c_white);
            draw_set_font(f_dialogue);
            draw_text(display_get_gui_width()/2 - 400, 136 +(64* m),string(menu[m]));
        }
    }
    if (m > 3) && (m <= 8)
    {
        if move = m
        {

            draw_set_halign(fa_left);
            draw_set_valign(fa_top);
            draw_set_color(c_blue);
            draw_set_font(f_dialogue);
            draw_text(display_get_gui_width()/2 - 250, 136 +(64* (m - 4)),string(menu[m]));
        } else
        {
            draw_set_halign(fa_left);
            draw_set_valign(fa_top);
            draw_set_color(c_white);
            draw_set_font(f_dialogue);
            draw_text(display_get_gui_width()/2 - 250, 136 +(64* (m - 4)),string(menu[m]));
        }
    }
    if (m >8) && (m <= 10)
    {
        if move = m
        {

            draw_set_halign(fa_left);
            draw_set_valign(fa_top);
            draw_set_color(c_blue);
            draw_set_font(f_dialogue);
            draw_text(display_get_gui_width()/2 + 340, 136 +(64* (m - 9)),string(menu[m]));
        } else
        {
            draw_set_halign(fa_left);
            draw_set_valign(fa_top);
            draw_set_color(c_white);
            draw_set_font(f_dialogue);
            draw_text(display_get_gui_width()/2 + 340, 136 +(64* (m - 9)),string(menu[m]));
        }
    }
    if (m >= 11)
    {
        if move = m
        {
            draw_set_halign(fa_center);
            draw_set_valign(fa_top);
            draw_set_color(c_blue);
            draw_set_font(f_dialogue);
            draw_text(display_get_gui_width()/2, display_get_gui_height() - 150 + (50* (m-12)),string(menu[m]));
        } else
        {
            draw_set_halign(fa_center);
            draw_set_valign(fa_top);
            draw_set_color(c_white);
            draw_set_font(f_dialogue);
            draw_text(display_get_gui_width()/2, display_get_gui_height() - 150 + (50* (m-12)),string(menu[m]));
        }
    }
}
 

NightFrost

Member
It is best to separate menu item data from the draw. For simple menus I have a pretty standard system. First, you set up each menu item in create event, for example a three-item menu (simplified, I use enums and scripts but the end result is the same):
Code:
MainMenu[0, 0] = 100; // X position of upper left corner
MainMenu[0, 1] = 100; // Y position of upper left corner
MainMenu[0, 2] = "START"; Label of the menu item
MainMenu[0, 3] = 2; // Menu item to move to when up-key is pressed (-1 = no move)
MainMenu[0, 4] = -1; // Right-key press
MainMenu[0, 5] = 1 // Down-key press
MainMenu[0, 6] = -1; // Left-key press

MainMenu[1, 0] = 100; // X position of upper left corner
MainMenu[1, 1] = 150; // Y position of upper left corner
MainMenu[1, 2] = "HIGH SCORES"; Label of the menu item
MainMenu[1, 3] = 0; // Menu item to move to when up-key is pressed (-1 = no move)
MainMenu[1, 4] = -1; // Right-key press
MainMenu[1, 5] = 2 // Down-key press
MainMenu[1, 6] = -1; // Left-key press

MainMenu[2, 0] = 100; // X position of upper left corner
MainMenu[2, 1] = 200; // Y position of upper left corner
MainMenu[2, 2] = "QUIT"; Label of the menu item
MainMenu[2, 3] = 1; // Menu item to move to when up-key is pressed (-1 = no move)
MainMenu[2, 4] = -1; // Right-key press
MainMenu[2, 5] = 0 // Down-key press
MainMenu[2, 6] = -1; // Left-key press

CurrentMenuItem = 0; // Currently menu item zero is selected
As can be seen from the key press setup, left and right are unused but up and down will cycle through the menu items.

Next you update the currently selected menu item in the step event (again simplified, I use enums and have added mouse/pad controls):
Code:
if(KeyUp){ // If your chosen up-key was pressed
    if(MainMenu[CurrentMenuItem, 3] != -1) CurrentMenuItem = MainMenu[CurrentMenuItem, 3];
}
else if(KeyRight){ // If your chosen right-key was pressed
    if(MainMenu[CurrentMenuItem, 4] != -1) CurrentMenuItem = MainMenu[CurrentMenuItem, 4];
}
// And so on for down and left... you get the idea
How you visualize the menu is up to you but basically you loop through the menu array in draw event and draw the items. In pseudocode:
Code:
for loop from zero to number of columns (menu items) in MainMenu {
    if current menu item equals CurrentMenuItem, draw a rectangle at menu item's given x and y, width and height as you please
    draw menu item's label text so it centers on the rectangle
}
Finally, the actions you handle back in the step event. A simple method would be like:
Code:
if(KeySelect){ // If your selection/action/whatever key was pressed
    if(CurrentMenuItem == 0) room_goto(rm_game);
    else if(CurrentMenuItem == 1) room_goto(rm_highscores);
    else if(CurrentMenuItem == 2) game_end();
}
You could integrate the action information into the MainMenu array too, or set up some action identifier based system, depending on how complex the menu actions need to be.

EDIT: You can set up large menus with this with multiple rows and columns and connect them as you like, as the locations and where each item connects to are distinct data.
 
Last edited:
K

Kyle Rider

Guest
I really want to use this for my newgame/loadgame screen. Thank you very much!
 
Top