Simple Menu Problem

A

alexanderarts

Guest
I'm just starting out with Game Maker so I am using tutorials to learn how to do everything.

I've managed to make a simple Yes/No pop-up menu that comes up in my game. It's designed for planets on a solar system map. When the player runs into a planet the planet creates an instance of this menu and gets info from it's creator instance so that it knows the planet's name and what map it's associated with. When the player hits Yes it should take them to the orbit room. If the player hits No the spaceship should be lightly pushed away from the planet. I've gotten both options to work, but not correctly.

I want the Yes and No options to both be selectable with the accept_key but also have a cancel_key that switches to No when on the Yes case and selects No when on the No case. Currently the accept key just does the Yes option no matter where it is and the cancel_key always does the No option.

I feel like the other stuff I've managed to do is so much more complicated, but I really can't figure out where I messed this one up. If anyone can see where I went wrong, please point it out to me! Thanks!!!

Here's the code:

CREATE:
///initialize the menu
creator = noone;

ALARM 0:
///menu alarm

STEP:
///control the menu
if (alarm[0] <= 0) {
if (obj_get_input.down_key) {
if (menu_index < array_length_1d(option)-1) {
menu_index +=1;
} else {
menu_index = 0;
}
alarm[0] = room_speed/3;
}

if (obj_get_input.up_key) {
if (menu_index > 0) {
menu_index -=1;
} else {
menu_index = array_length_1d(option)-1;
}
alarm[0] = room_speed/3;
}

if (obj_get_input.accept_key) {
switch (menu_index) {
case 0:
room_goto(creator.planet_scan_room);
break;

case 1:
obj_player_explorer.explorer_control = true;
obj_player_explorer.direction +=180;
obj_player_explorer.speed = 0.05;
obj_planet_parent.alarm[0] = 100;
instance_destroy();
break;

default:
break;
}
}

if (obj_get_input.cancel_key) {
switch (menu_index) {
case 0:
menu_index +=1;
alarm[0] = room_speed/3;
break;

case 1:
obj_player_explorer.explorer_control = true;
obj_player_explorer.direction +=180;
obj_player_explorer.speed = 0.05;
obj_planet_parent.alarm[0] = 100;
instance_destroy();
break;

default:
break;
}
}

}

USER DEFINED 0:

///event user 0
//make sure "creator" is set
creator = creator

//create menu
title = "Enter orbit around " +string(creator.planet_name) + "?";

option[0] = "Yes";
option[1] = "No";

//menu index
menu_index = 0;

DRAW GUI:

///draw the orbit menu text
draw_self();

var xx = display_get_gui_width()/2;
var yy = display_get_gui_height()/2;
draw_set_halign(fa_center);
draw_text(xx, yy-64, title);

//loop through array
for (var i=0; i<=array_length_1d(option)-1; i++) {
draw_set_color(c_gray);
if (i == menu_index) {
draw_set_colour(c_white);
}
//draw text relative to each other
draw_text(xx, yy+ (i*32), option);

}
//set color and h align back
draw_set_color(c_white);
draw_set_halign(fa_left);
 

Attachments

jo-thijs

Member
Hi and welcome to the GMC!

The code you gave us is not the code you're using.
For instance, this line:
Code:
draw_text(xx, yy+ (i*32), option);
should have been:
Code:
draw_text(xx, yy+ (i*32), option[i]);
to give the results in your screenshot.

On top of that, the code you've given us doesn't have the issues you described.

My bet is that you forgot to use break in the switch structure in the code you're using, but somehow did include break in the code you've given us.

Besides that, I'd also like to say that your code looks very good, except for a couple of details:
1) The missing I already mentioned.
2) This is unnecessary:
Code:
default:
break;
You don't need to put default in a switch structure.

3) This is unnecessary:
Code:
//make sure "creator" is set
creator = creator
It only wastes space and performance.
If creator wasn't set before, then it will throw an error.

4) This line looks like it is supposed to effect multiple instances:
Code:
obj_planet_parent.alarm[0] = 100;
However, the behaviour of that line when there are multiple instances of type obj_planet_parent depends on the target you compile it on.
In HTML5 for example, it won't work.

5) You use both draw_set_color and draw_set_colour.
It'd be nice to be consistent on which one you use.
 
A

alexanderarts

Guest
That's super weird. My code DID have the but it didn't show up after copy and pasting...

Well I took out the unneeded stuff you mentioned and made my 'color's consistent. Double and triple checked my "break;"s but still having the same issue...

Is it possible that the room speed (which is 60) or some other setting could cause the case to switch incorrectly? I'm using the free Studio version, too, by the way.
Maybe I should re-do the menu control part to be more basic and just have it set specifically for a 2 choice array. The tutorial I had looked at was showing that the array length could be used so that it worked with lots of different menu lengths.

I've made some minor changes, I'll post my current code here and try to double check if I mess something up in copy-pasting it somehow.

*edit* I found the ["i"] doesn't work in the "rich-text" editor, so that's why it didn't show up - maybe it will show up if I use the "code" setting, like you did.

Code:
CREATE:
///initialize the menu
    creator = noone;
//menu index
    menu_index = 0;

ALARM 0:
///menu alarm

STEP:
///control the menu
if (alarm[0] <= 0) {
    if (obj_get_input.down_key) {
        if (menu_index < array_length_1d(option)-1) {
            menu_index ++;
        } else {
            menu_index = 0;
        }
        alarm[0] = room_speed/3;
    }
    if (obj_get_input.up_key) {
        if (menu_index > 0) {
            menu_index --;
        } else {
            menu_index = array_length_1d(option)-1;
        }
        alarm[0] = room_speed/3;
    }

    if (obj_get_input.accept_key) {
        switch (menu_index) {
            case 0:
                room_goto(creator.planet_scan_room);
                instance_destroy();
                break;
             
            case 1:
                obj_player_explorer.explorer_control = true;
                obj_player_explorer.direction +=180;
                obj_player_explorer.speed = 0.05;
                obj_planet_parent.alarm[0] = 100;
                instance_destroy();
                break;
             
        }
    }
    if (obj_get_input.cancel_key) {
        switch (menu_index) {
            case 0:
                menu_index ++;
                break;
             
            case 1:
                obj_player_explorer.explorer_control = true;
                obj_player_explorer.direction +=180;
                obj_player_explorer.speed = 0.05;
                obj_planet_parent.alarm[0] = 100;
                instance_destroy();
                break;
             
        }
    }
}

USER DEFINED 0:
///event user 0

   //create menu
    title = "Enter orbit around " +string(creator.planet_name) + "?";
    option[0] = "Yes";
    option[1] = "No";

DRAW GUI:
///draw the orbit menu text
draw_self();

var xx = display_get_gui_width()/2;
var yy = display_get_gui_height()/2;
draw_set_halign(fa_center);
draw_text(xx, yy-64, title);

//loop through array
for (var i=0; i<=array_length_1d(option)-1; i++) {
    draw_set_color(c_gray);
    if (i == menu_index) {
        draw_set_color(c_white);
    }
    //draw text relative to each other
    draw_text(xx, yy+ (i*32), option[i]);
}
//set color and h align back
draw_set_color(c_white);
draw_set_halign(fa_left);
 
Last edited by a moderator:
A

alexanderarts

Guest
Hmm, I think I figured it out, thinking about it. Maybe the planet is just creating a bunch of menus under that menu I can see since the menu is created on a collision. Then I think the menu says NO but on some other menu created under that it actually says YES. I'll try to code something in the menu creating planet that makes sure its only creating one at a time and see if that fixes it.
 
A

alexanderarts

Guest
YUP! I fixed it... man, I feel dumb. But thanks to you, jo-thijs I realised there really WASN'T anything wrong with my menu code, haha!
So in the menu creator (the planet that the player hits) I made sure to say - only make a menu if there isn't a menu already... derp!

Code:
//step

//collision with player
if (place_meeting(x, y, obj_player_explorer) and alarm[0] <=0 and !instance_exists(obj_menu_orbit)) {
    obj_player_explorer.explorer_control = false;
    obj_player_explorer.speed = 0;
    menu = instance_create(display_get_gui_width()/2, display_get_gui_height()/2, obj_menu_orbit);
    menu.creator = id;
    with(menu)
        event_user(0)
}
 
Top