1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GMS 2 Menu object not appearing after following a tutorial

Discussion in 'Programming' started by SARAndipitii, Jun 16, 2019.

  1. SARAndipitii

    SARAndipitii Member

    Joined:
    Jun 13, 2019
    Posts:
    4
    Hi!

    So I'm a beginner when it comes to GMS and coding in general. I followed a tutorial for creating menus:

    https://www.youtube.com/watch?v=fWAhi10q1ZE&

    I followed everything to the letter with the exception of the general layout of the menus, some visual elements and controls, but I don't see my object at all. It's already added to the room and "visible" is checked.

    Below is the code I'm using.

    Create
    Code:
    gui_width = display_get_gui_width();
    gui_height = display_get_gui_height();
    gui_margin = 32;
    
    menu_x = gui_width+200;
    menu_y = gui_height - gui_margin;
    menu_x_target = gui_width - gui_margin;
    menu_speed = 25; //lower is faster
    menu_font = font_main;
    menu_itemheight = font_get_size(font_main);
    menu_committed = -1;
    menu_control = true;
    
    menu[0] = "Continue";
    menu[1] = "New Game";
    menu[2] = "Settings";
    menu[3] = "Credits";
    menu[4] = "Exit";
    
    menu_items = array_length_1d(menu);
    menu_cursor = 2;
    

    Draw GUI
    Code:
    draw_set_font(font_main);
    draw_set_halign(fa_left);
    draw_set_valign(fa_middle);
    
    for (var i = 0; i < menu_items; i++)
    {
        var offset = 2;
        var txt = menu[i];
        if (menu_cursor == i)
        {
            txt = string_insert("> ", txt, 0);
            var col = c_white;
        }
        else
        {
            var col = c_gray;
        }
        var xx = menu_x;
        var yy = menu_y - (menu_itemheight * (i * 1.5));
        draw_set_color(c_white);
        draw_text(xx-offset,yy,txt);
        draw_text(xx+offset,yy,txt);
        draw_text(xx,yy+offset,txt);
        draw_text(xx,yy-offset,txt);
        draw_set_color(col);
        draw_text(xx,yy,txt);
    }
    

    Step
    Code:
    if (menu_control)
    {
        if (keyboard_check_pressed(vk_up)) or keyboard_check_pressed(ord("W"))
        {
            menu_cursor++;
            if (menu_cursor >= menu_items) menu_cursor = 0;
        }
        if (keyboard_check_pressed(vk_down)) or keyboard_check_pressed(ord("S"))
        {
            menu_cursor--;
            if (menu_cursor < 0) menu_cursor = menu_items-1;
        }
        if (keyboard_check_pressed(vk_enter)) or keyboard_check_pressed(vk_space)
        {
            menu_x_target = gui_width+200;
            menu_committed = menu_cursor;
            menu_control = false;
        }
    }
    
    if (menu_x > gui_width+150) && (menu_committed != -1)
    {
        switch (menu_committed)
        {
            case 0: game_end(); break;
        }
    }
    

    I'm not getting any syntax errors, but I have no idea why it's not appearing.

    What am I doing wrong?
     
  2. Sk8dududu

    Sk8dududu Member

    Joined:
    Aug 14, 2017
    Posts:
    196
    You cannot see the object? Or you cannot see anything that the object draws?
    If your object doesn't have a sprite, you won't be able to see it, regardless of it being visible or not.
     
  3. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    223
    first thing that comes to mind is (just a guess)
    in the draw event, draw_self();
     
    Last edited: Jun 17, 2019
  4. Corey

    Corey Member

    Joined:
    Apr 8, 2019
    Posts:
    21
    The thing about Draw GUI event is it's classified as a Draw event that follows the screen, and only draws GUI elements. If it's set to only draw GUI, it will do just that and only that and ignore other concepts defined by the sprite itself. You will have to declare the sprite of the object; in this case, create a Draw event and use this code:

    draw_sprite(sprite, subimg, x, y);
     
  5. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    873
    If you use the Draw GUI and draw text (which is what is being done by the OP) you do not need a sprite. This is how you draw text menus.

    This is not needed as the Draw GUI event contains all the drawing of the text that is needed.

    You do not do this as there is no sprite, and the menu is a series of draw text commands.

    The problem is that the OP has not actually followed the tutorial exactly and has one single mistake.
    You have not followed it exactly. Even clicking on that YouTube link it shows immediately the mistake that you have made.
    In the tutorial video, the line is meant to be:
    Code:
    menu_x = gui_width;//+200;
    
    but you have still got it as:
    Code:
    menu_x = gui_width+200;
    This means that your menu is being drawn 200 pixels off the right side of the screen.
     
    Yal and woods like this.
  6. Sk8dududu

    Sk8dududu Member

    Joined:
    Aug 14, 2017
    Posts:
    196
    @BaBiA Game Studio
    He said "I cannot see the object". If you have no sprite then no matter what you draw, you still can't see the object. You can just see what the object draws.
     
  7. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    873
    Yes, but he is following a tutorial that draws a text menu, so it does not require the object to have a sprite just to draw the text options of the menu. If it was a sprite-based menu (a background image etc) then yes you would be correct. But if you are trying to draw a load of text and say that you cannot see your object, then it seems pretty obvious that the problem is that the text is not drawing. And considering that the OP is drawing all the text outside of the screen it is no wonder that the text does not show up. :)
     
  8. Sk8dududu

    Sk8dududu Member

    Joined:
    Aug 14, 2017
    Posts:
    196
    I didn't find it obvious. I tend to take things quite literally though.
     
  9. SARAndipitii

    SARAndipitii Member

    Joined:
    Jun 13, 2019
    Posts:
    4
    As BaBiA has said, I'm trying to create a text-based menu in which I would not need a sprite.

    I've changed it back and it still doesn't show.
     
  10. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,934
    How about adding this line to the top of your drawing script, before the loop?

    Code:
    draw_set_color(c_red)
    draw_set_alpha(1)
    draw_arrow(display_get_gui_width()*0.5,display_get_gui_height()*0.5,menu_x,menu_y,10)
    
    If you see an arrow or a line, it will point you from the center of the GUI layers towards where the menu is (telling you how to adjust coordinates). If you don't see an arrow, the menu object doesn't exist in the room or is invisible. If you see an arrow but not a menu (i.e. the arrow points to a place on-screen), there's a problem with the font.
     
  11. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    873
    Then the other problem is that you are using left-aligned text instead of right-aligned text (in your Draw GUI event).
    Your code:
    Code:
    draw_set_halign(fa_left);
    
    Tutorial code:
    Code:
    draw_set_halign(fa_right);
    
    Using left-aligned means that the left edge of the text will start from the position that it is draw, so it will continue off the edge of the screen. Right-aligned will put the right edge of the text at the position you are drawing.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice