GML There must be a more memory efficient way to do this

Discussion in 'Programming' started by Jonar, Jul 23, 2019.

Tags:
  1. Jonar

    Jonar Member

    Joined:
    Dec 9, 2018
    Posts:
    31
    I am currently working on the menu for my game and am using Friendly Cosmonaut's code. I decided that as well as a pause menu, there should also be menu in the title screen. Only issue is that as well as a separate main menu array, I also need another array for ever time you press back since I don't want the player to press back and then go to the pause menu. As a result, I have to basically double the number of menu arrays in the game. Here's an example:

    Code:
    #region controls
    ds_menu_controls = create_menu_page(
        ["UP",              MENU_ELEMENT_TYPE.INPUT,        "key_up",                 vk_up],
        ["LEFT",          MENU_ELEMENT_TYPE.INPUT,        "key_left",                vk_left],
        ["RIGHT",        MENU_ELEMENT_TYPE.INPUT,        "key_right",              vk_right],
        ["DOWN",        MENU_ELEMENT_TYPE.INPUT,        "key_down",            vk_down],
        ["RUN",            MENU_ELEMENT_TYPE.INPUT,        "key_run",               vk_lshift],
      
        ["INTERACT",        MENU_ELEMENT_TYPE.INPUT,        "key_interact",            ord("Z")],
        ["ATTACK",            MENU_ELEMENT_TYPE.INPUT,        "key_attack",              ord("X")],
        ["BLOCK",              MENU_ELEMENT_TYPE.INPUT,        "key_block",               ord("C")],
      
        ["BACK", MENU_ELEMENT_TYPE.PAGE_TRANSFER, MENU_PAGE.MAIN]
    ); #endregion
    #region controls in titlescreen
    ds_menu_controls_main = create_menu_page(
        ["UP",              MENU_ELEMENT_TYPE.INPUT,        "key_up",                 vk_up],
        ["LEFT",          MENU_ELEMENT_TYPE.INPUT,        "key_left",                vk_left],
        ["RIGHT",        MENU_ELEMENT_TYPE.INPUT,        "key_right",              vk_right],
        ["DOWN",        MENU_ELEMENT_TYPE.INPUT,        "key_down",            vk_down],
        ["RUN",            MENU_ELEMENT_TYPE.INPUT,        "key_run",               vk_lshift],
      
        ["INTERACT",        MENU_ELEMENT_TYPE.INPUT,        "key_interact",            ord("Z")],
        ["ATTACK",            MENU_ELEMENT_TYPE.INPUT,        "key_attack",               ord("X")],
        ["BLOCK",             MENU_ELEMENT_TYPE.INPUT,        "key_block",                ord("C")],
      
        ["BACK", MENU_ELEMENT_TYPE.PAGE_TRANSFER, MENU_PAGE.TITLE]
    ); #endregion
    
    // the only difference between either of these arrays is the last part. This is an insanely stupid way to do
    // this...
    
    All I want to know is if there is a more sane way to do this without having to change all of my code. Thanks to anyone that replies.
     
  2. Annoyed Grunt

    Annoyed Grunt Member

    Joined:
    Jun 20, 2016
    Posts:
    136
    Can you elaborate more on this?

    What does it mean? If you have two menus why do you need more than two arrays?
    It's also usually a good idea to provide a direct link to whatever tutorial you're following.
     
  3. Gamebot

    Gamebot Member

    Joined:
    Jul 3, 2016
    Posts:
    182
    Personally, I never liked enums especially with arrays. If your planning on a "struct" format using a ds_map is less code and easier to read and change. (When done correctly.)

    Though objects holding two or three 2d-arrays are not even an issue these days. Just ask my clanky old, busted, broke to pieces somehow still works computer.

    However, this is the one time I would probably use globals and several instances. To key each global.key use different instances of an object input box. When spawned each instance is given a number or string (which will translate to correct global) and proper text (What the box does such as UP, DOWN..ect). When you click on said box you can press the key you want, this would go to a script or directly relate the number to a switch statement which allows last keypress to equal global. Assuming all keys are not duplicates another quick check before allowing leaving area, otherwise default settings should kick in...unless this is something you want.
     

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