GML How do I customize a menu inserted into o_game?

Hey everyone,

I am trying to create a customized menu as a separate object, however, the code in the object is not working. It should show a list of text options as the you'll see in the code below, however, currently it shows a black screen. Previously, the menu was drawn as a non-interactive sprite and existed in a controller object labeled o_game, but I erased it because I found it to be more difficult to code within that object. After I erased the code in o_game, I made an object dedicated solely to the menu and placed it into a room exclusively for the menu. I made sure that o_menu was in the room and I tried running it in a newly created project file; neither of these attempts ran the code and created the menu. Am I going about this the wrong way? Can I create the same custom menu I previously had in o_game? Is there anything in o_game that could possibly obstruct the menu from being created. It should be noted I have room with o_game and the camera that loads before the menu room.

o_game draws the menu as an image like so:

GML:
//Draw
//720x360
//get gui dimensions
var gw = display_get_gui_width();
var gh = display_get_gui_height();

    //draw main menu and fade to first level
    //draw bounce
    var start_y = 350;        //how far the animation moves
    var factor = start_y;

    var max_frames = 60;        //how long animation goes for
    if current_frame < max_frames {
        current_frame++;
        var move = EaseOutBounce(current_frame, 0, 1, max_frames)
    } else {
        move = 1;
        //alow game start as menu has dropped
        if (keyboard_check_pressed(vk_space) or gamepad_button_check_pressed(0, gp_face1)) and
        !instance_exists(o_fade) {
            //move to the next room on the list
            //get string of the next room i.e. "rm_01"
            //var next_room_str = room_get_name(room_next(room));
            //get asset index of next room i.e 1, 2, 3...rooms start at 0
            //var next_room_name = asset_get_index(next_room_str);
            fade_to_room(room_next(room), 0, 0, 1, c_black);
        }
    }
    draw_sprite(s_main_menu, 0, 0, (move * factor) - start_y);

    //draw highscore
    if current_frame == max_frames {
        draw_set_halign(fa_right);
        draw_set_font(fnt_bookman);
        draw_text_ext_colour(gw - 10, 28, highscore, 5, 100, c_aqua, c_aqua, c_gray, c_gray, 1);
    }
}

//fade screen in
if fade_in {
    alpha = lerp(alpha, 0, fade_spd);
    draw_set_alpha(alpha);
    draw_rectangle_colour(0, 0, gw, gh, c_black, c_black, c_black, c_black, false);
    draw_set_alpha(1);
}

//display msg
if alarm[DISPLAY_MSG] > 0 {
    draw_set_halign(fa_center);
    draw_set_font(fnt_bookman);
    draw_set_color(c_black);
    draw_text(gw/2 + 1, gh *.85 + 1, msg);
    draw_set_color(c_white);
    draw_text(gw/2, gh *.85, msg);
}
o_menu:

GML:
//Create Event

//main menu
menu[0] = "Start";
menu[1] = "Options";
menu[2] = "Exit";

index = 0;

//Step
// get input

var _up = keyboard_check_pressed(vk_up);
var _down = keyboard_check_pressed(vk_down);
var _select = keyboard_check_pressed(vk_enter) or  keyboard_check_pressed(vk_space);

var _move = _down - _up;

if _move !=0 {
    //move index
    index += _move
    //clamp values
    var _size = array_length_1d(menu);
    if index < 0 index = _size - 1;
    else if index >= _size index = 0;


}

//Draw

draw_set_halign(fa_center);
draw_set_font(fnt_menu);

//line spacing
var _ gap = 40;
//draw items

for(var i = 0; i < array_length_1d(menu); ++i ) {

    draw_set_color(c_white);
    if i == index draw_set_color(c_teal);
    draw_text(room_width/2, room_height * 0.4 + _gap *i, menu[i]);



}

UPDATE: Using the Draw GUI event instead of the regular Draw event produces the text, although it isn't aligned properly, it is progress.
 
Last edited:

Nocturne

Friendly Tyrant
Forum Staff
Admin
I'm afraid I'm not clear on what the issue is here? Have you moved the menu code from a dedicated object into a general controller object? Is it doing anything? What are you expecting to happen and what is actually happening? If you have moved the code - and it was working before - then WHY did you move it? Why not keep a dedicated menu object? Keep in mind, we have no idea about your project, yet your post reads as if we should all know how it was working and how it should work. You need to explain things a little better if you want to get help I'm afraid! :)
 
I'm afraid I'm not clear on what the issue is here? Have you moved the menu code from a dedicated object into a general controller object? Is it doing anything? What are you expecting to happen and what is actually happening? If you have moved the code - and it was working before - then WHY did you move it? Why not keep a dedicated menu object? Keep in mind, we have no idea about your project, yet your post reads as if we should all know how it was working and how it should work. You need to explain things a little better if you want to get help I'm afraid! :)
My bad, I wasn't sure how to explain it. I removed the code from the draw GUI event in the o_game object that used to draw the main menu sprite because I couldn't figure out how to make it more interactive within o_game. It was serving as a background image and didn't have any options. I deleted that code so that I can make an object that serves as the main menu instead. However, when I got rid of the code and inserted the newly created code into o_menu then placed the object into the room dedicated to showing the menu, nothing appeared. I guess you could call the o_menu a general controller object. I don't think it is doing anything and I was expecting it to show options for the menu. Here is the code in o_menu:


GML:
//Create Event

//main menu
menu[0] = "Start";
menu[1] = "Options";
menu[2] = "Exit";

index = 0;

//Step
// get input

var _up = keyboard_check_pressed(vk_up);
var _down = keyboard_check_pressed(vk_down);
var _select= keyboard_check_pressed(vk_enter) or  keyboard_check_pressed(vk_space);

var _ move = _down - _up;

if _move !=0 {
    //move index
    index += _move
    //clamp values
    var _size = array_length_1d(menu);
    if index < 0 index = _ size - 1;
    else if index >= _size index = 0;


}

//Draw

draw_set_halign(fa_center);
draw_set_font(fnt_menu);

//line spacing
var _ gap = 40;
//draw items

for(var i = 0; i <array_length_1d(menu); ++i) {

    draw_set_color(c_white);
    if i == index draw_set_color(c_teal);
    draw_text(room_width/2, room_height * 0.4 + _gap *i, menu[i]);



}
 
Last edited:
I'm afraid I'm not clear on what the issue is here? Have you moved the menu code from a dedicated object into a general controller object? Is it doing anything? What are you expecting to happen and what is actually happening? If you have moved the code - and it was working before - then WHY did you move it? Why not keep a dedicated menu object? Keep in mind, we have no idea about your project, yet your post reads as if we should all know how it was working and how it should work. You need to explain things a little better if you want to get help I'm afraid! :)
Is there information still missing? So far only you have commented.
 

TailBit

Member
Let's rephrase .. it o_menu's visible checkbox checked?

If not then it won't do its draw event, you could also put a show_debug_message into the draw event just to check if it trigger

The code itself works fine
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Is there information still missing? So far only you have commented.
Please be patient! Even I need to sleep sometimes and have a life outside the GMC... :p

So, as mentioned above, your code looks okay in principle and should be working fine. Which begs some questions:

1) Does the object have "visible" flagged in the object properties (if not then it'll be invisible in the game)
2) Is the layer you place it on below the background layer in depth? (that's above it in the layer order of the room editor...)
3) Is the layer you place it on also visible (there is a visible/invisible toggle for layers in the room editor)
4) Have you run the game using the debugger and checked to ensure that the object IS actually present in the room and not being destroyed by something?
5) Have you tried clearing the compiler cache? GM cache's things so building a project is faster, and this can sometimes become corrupted and cause weird issues like this. You can force a cache clean using the "broom" button at the top of the IDE.

:)
 
Let's rephrase .. it o_menu's visible checkbox checked?

If not then it won't do its draw event, you could also put a show_debug_message into the draw event just to check if it trigger

The code itself works fine
Yeah, it was active in the room it was placed in. For some reason, it only works in the Draw GUI event and not the Draw Event. Hopefully that doesn't cause any problems in the future.
 
Please be patient! Even I need to sleep sometimes and have a life outside the GMC... :p

So, as mentioned above, your code looks okay in principle and should be working fine. Which begs some questions:

1) Does the object have "visible" flagged in the object properties (if not then it'll be invisible in the game)
2) Is the layer you place it on below the background layer in depth? (that's above it in the layer order of the room editor...)
3) Is the layer you place it on also visible (there is a visible/invisible toggle for layers in the room editor)
4) Have you run the game using the debugger and checked to ensure that the object IS actually present in the room and not being destroyed by something?
5) Have you tried clearing the compiler cache? GM cache's things so building a project is faster, and this can sometimes become corrupted and cause weird issues like this. You can force a cache clean using the "broom" button at the top of the IDE.

:)
I think I might have fixed it by using the Draw GUI event instead of the Draw event. Thanks for the troubleshooting and forum tips.
 
Top