Defining #Macros in multiplace places

Discussion in 'Programming' started by Mert, Jan 11, 2019.

  1. Mert

    Mert Member

    Joined:
    Jul 20, 2016
    Posts:
    172
    Hi. I was testing scaling and orientation settings of my app for different targets. I define #macro resW and #macro resH in two fields (see the following code). >> In the red label on script window.

    The GMS2 IDE gives me the error : macro resW already exists. My app only defines them in one condition, it means that I do not re-define them afterwards.

    My app runs on both Android and Web with no issue, but my question is would this create any issue, or am I coding it in a bad way ?

    Code:
    case 0: //Android
        device_mouse_dbclick_enable(false);
        display_set_ui_visibility(1024);
        os_powersave_enable(false);
       
        //Set the display size
        #macro resW 720
        #macro resH 1280
       
        //Resize GUI
        display_set_gui_size(resW,resH);
        break;
       
        case 1: ///On Browser
        #macro resW browser_width
        #macro resH browser_height
       
        break;
     
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    5,922
    Just don't use a macro.

    If the IDE is saying you can't do it, then it probably won't even let you compile it. Macros and enums are handled by the IDE and compiler, not by the code itself.

    Can you still set macros as outcomes of functions or conditions or results of scripts?
     
  3. Mert

    Mert Member

    Joined:
    Jul 20, 2016
    Posts:
    172
    I'm using them completely for cosmetic reasons, and I access these valuables constantly throughout the game.(Placing graphics and making the app responsive for both PC & Mobile Web views). I thought using them would be beneficial, and the code looks too neat.

    I've never tried as you described them in the question.
     
  4. YellowAfterlife

    YellowAfterlife ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    2,072
    You cannot use macros like that - you would need a configuration to regulate which macros get used,
    Code:
    #macro resW 720
    #macro Web:resW browser_width
     
    Mert likes this.
  5. PlayerOne

    PlayerOne Member

    Joined:
    Mar 14, 2018
    Posts:
    285

    Keep in mind that macros are global in scope. To create multiple macro's with the same name in your project - regardless if it's an object or script - will give off an error on compile.
     
  6. Death

    Death Member

    Joined:
    Aug 8, 2018
    Posts:
    76
    Yup, macros are constants, since you define the same macro twice, i.e., trying to redefine a constant, you'll get the error...
     
  7. Binsk

    Binsk Member

    Joined:
    Jun 22, 2016
    Posts:
    467
    Macros aren't really even "global in scope" they don't even exist at run-time. They are only a compile-time convenience. GameMaker will simply read the definition of a macro, regardless of its location or the code around it, and replace all instances of it with its value at compile-time. Once the project is compiled and by the time you run it all macros have ceased to exist. This is why if-statements and such don't work with them because those are considered at run-time not compile-time.
     
    PlayerOne likes this.

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