1. Hey! Guest! The 32nd GMC Jam will take place between Feb 22nd, 12:00 UTC (Friday noon) and Feb 25th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 In-game menu question

Discussion in 'Programming' started by Vallo92, Feb 12, 2019 at 8:01 PM.

  1. Vallo92

    Vallo92 Member

    Joined:
    Jun 20, 2016
    Posts:
    26
    Hello everyone, I wanted to ask you a question regarding the creation of a menu in play.
    I leave you immediately an image of a very famous game to make you understand better:
    [​IMG]
    Basically, I would like to create a menu containing keys during the game. The player will have the possibility to click the menu keys to perform certain operations. The menu consists of the background sprite, plus several objects that allow operations to be performed (cancel key, confirm key etc.) The problem is that the menu is created during the execution of the game, and under it are active objects, which perform actions if clicked.
    The simplest idea is to create a variable for the menu, and make click operations of the game objects only if (for example) "menu = false".
    This idea, however, does not confuse me much, because I would have to insert an if within each object at the beginning of each code.
    I would like to know from you more expert if there are better methods to create gui of menu with keys during the game, without deactivating objects or filling them with if.

    Thank you!
     
  2. BattleRifle BR55

    BattleRifle BR55 Member

    Joined:
    Jun 24, 2016
    Posts:
    855
    The way I do it is this: The entire menu is one object in one event. If there are layers on top of layers, then they're controlled via one variable to determine which one will be functioning while both layers can still draw at the same time. This is how I go about it -

    if (layer is 0)
    Controls for layer 0

    Draw layer 0

    if (layer is 1)
    {
    Controls for layer 1

    Draw layer 1
    }

    This way, you're not fumbling with all sorts of objects and variables to control and avoid functions that may or may not overlap. It keeps things simple. Granted, people would recommend to mimic this for both the Step and Draw events to keep the Draw event solely for drawing and the Step event solely for controls, but that's up to you.
     
  3. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,496
    Personally, I don't think your idea is that bad. If you wanted to be fancy, you could take a 'screen shot' (save the application surface to another surface) and then deactivate all the instances and display the screen shot behind the buttons so it looks like they are there but really arn't.
     
  4. Vallo92

    Vallo92 Member

    Joined:
    Jun 20, 2016
    Posts:
    26
    Thank you for your reply, but what you have told me is not very clear to me. Could you explain me better?
    I had thought about this possible solution, but I wanted to avoid dumping the objects below. I do not want to be able to interact with them, but I would like them to continue to execute their code and make their own movements.
     
  5. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,496
    If you don't want the game paused, then your way is probably the most straight forward. Another way to do it, depending upon how you've set up the rest of the project is don't let any object directly check for mouse presses, touches, whatever directly. Have one controller object that does that and have all other objects pull from that. Then you only need to change one variable when the menu is open. For example:

    Code:
    
    //obj_input control object
    left_mouse_pushed = mouse_check_button_pressed(mb_left);
    no_menu_left_mouse_pushed = left_mouse_pushed && global.menu_open;
    
    ///example uses from another object
    
    //example use 1
    left_mouse = obj_input.left_mouse_pushed;
    
    //example use 2
    left_mouse = obj_input.no_menu_left_mouse_pushed;
    
    
    I don't know that this is really better than your current idea though.

    Yet a third way would be to have a script which checks whether an object has been clicked, use that script in all objects and have it test for menu, or 'height' or some other variable.
     
  6. Alexx

    Alexx Member

    Joined:
    Jun 21, 2016
    Posts:
    332
    You could just check if any instances are colliding with your popup menu, and exit any interaction event if true. (alternatively just check if popup is active)
    You could do this manually for each object, or create a master parent object for all objects that have interaction.

    This would allow the game to continue whilst the popup menu is present - except for any interaction that you exclude.
     
    Last edited: Feb 13, 2019 at 7:51 PM
  7. BattleRifle BR55

    BattleRifle BR55 Member

    Joined:
    Jun 24, 2016
    Posts:
    855
    Here's a simple example for the Draw event. Initialize "layer" in the Create event.
    Code:
    var _text = 0, _option = 0;
    
    if (layer == 0)
    {
        if (keyboard_check(vk_up))
            _text = 1;
    
        if (keyboard_check_pressed(vk_space))
            _option = 1;
    }
    
    draw_rectangle(10,10,100,100,0);
    
    if (_text)
        draw_text_color(50,50,"Hey",c_black,c_black,c_black,c_black,1);
    
    if (layer == 1)
    {
        draw_set_alpha(0.5);
        draw_rectangle_color(20,20,90,90,c_gray,c_gray,c_gray,c_gray,0);
        draw_set_alpha(1);
    
        if (keyboard_check(vk_up))
            draw_text_color(50,50,"Sup",c_black,c_black,c_black,c_black,1);
    
        if (keyboard_check_pressed(vk_space))
            layer = 0;
    }
    
    if (_option)
        layer = 1;
    In playing with this, you'll notice that pressing Up performs a different action depending on which layer is currently active, despite the lower rectangle always being drawn.
     

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