• 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 Simple 2D menu array

C

colcha

Guest
Hi,

I'm a graphics artist who has decided to expand my skills to coding in game maker to make a game. So far so good but I have hit a slight wall with menu arrays and was hoping someone could give me a simple explanation for what I want.

I have a menu working with single options, but what I'd like is for each option in the menu to load a different sub menus. For example if the fist option on the main menu is called "music" then there will be "track 1", "track 2" and "track 3 to select from once "music" is selected from main menu.

Then the second option on the menu could be "video", which leads to the menu "video1" and "video 2".

Hope that's clear enough. I've seen a few tutorials on 2D arrays but seem incompatible with what I want to do or are part of something bigger. Just need to know how to set it up in its simplest form to work it out. Thanks
 
Z

Zekka

Guest
You could represent your menu like this:

Code:
menu[0][0] = "Music";

menu[0][1] = "Track 1";
menu[0][2] = "Track 2";
menu[0][3] = "Track 3";
menu[0][4] = undefined; // end of submenu marker

menu[1][0] = "Video";
menu[1][1] = "Video 1";
menu[1][2] = "Video 2";
menu[1][3] = undefined; // end of submenu marker

menu[2][0] = undefined; // end of menu marker
Your code to operate on it might look something like this.

Code:
for (var opt = 0; menu[opt][0] != undefined; opt++) {
  // do something with option
  var option = menu[opt][0];

  if (selected == i) { // selected is an instance variable saying what base menu the user is looking at
    // if selected, draw the suboptions
    for (var subopt = 1; menu[opt][subopt] != undefined; subopt++) {
      var suboption = menu[opt][subopt];
      // suboption now contains the menu text
      // you might also have a separate variable "subselected" containing the index of a suboption.
     }
  }
}
 
C

colcha

Guest
Thanks, so do I create this as an object? First in a create event and second in a step? I'm still getting used to how it all works.
 
Z

Zekka

Guest
Thanks, so do I create this as an object? First in a create event and second in a step? I'm still getting used to how it all works.
You probably create the array in the Create event -- then in Step or in the Keyboard events you do things that will update the selected/subselected instance variables. The second fragment of code I wrote goes through the entire menu, making it suitable for the Draw event. You can use similar code, though, to calculate the length of the menu/submenu in Step/Keyboard to determine if you need to wrap the cursor back around to the top, things like that.
 
C

colcha

Guest
Thanks for the responses. I'm getting errors with any instance of double square brackets such as [0[[0] - any idea why?
 
S

Snail Man

Guest
GM refers to 2D arrays like array[x,y] as opposed to other languages which use array[x][y]
 
Z

Zekka

Guest
Whoops, my mistake. I haven't used 2D arrays in GM for a while and I used the convention that other C-like languages use.
 
C

colcha

Guest
OK that compiled but I've messed up my code trying to integrate it into what I already had and a little lost now. Basically I had an arrow image that moved up and down the menu with a key press in my step event (the create has top part and the draw includes bottom part of that example code that was pasted - I have a step event with the controls and define what the cases do in a script).

So, I've reverted it back to the code I used on the original version:

Code:
var m;
for (m = 0; m < array_length_1d(menu); m += 1)
{
  draw_text(x + space, y + (m * space),string(menu[m]))
}

draw_sprite(sprite_index, 0, x + 16, y + mpos * space);
I know it's this section I need to change, but can't find any tutorials or explanations that are clear enough. Hopefully you can help here - forgive my amateurism but it's early days.
 
C

colcha

Guest
Right managed to work it all out and it's doing what I want now after a bit of playing around, thanks.
 
Last edited by a moderator:
Top