1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Initializing variables/dll before GameStart event + game_restart() proof

Discussion in 'Tutorials' started by gnysek, Nov 2, 2016.

  1. gnysek

    gnysek Member

    Joined:
    Jun 20, 2016
    Posts:
    1,305
    GM Version: Studio
    Target Platform: Windows
    Download: NA
    Links: NA

    Generally, most of GameMaker:Studio users initialize all needed global variables and external extensions in a special room at start of game, and then go to next room, and never back there.
    A downside is, that you cannot use game_restart(); this way. However, there is a way to do it and execute at start of game, but before whole GM:S runner engine starts, and game_restart() will restart game to a moment AFTER this script was executed, so it will be for sure ran only one time! Also, you don't need any objects/room/room creation code to do it!

    So, create a script which initializes all your variables, lets call it script_init. Put all code you need. Then, at top of script, add this line:

    Code:
    gml_pragma("global", "script_init()");
    That's all. It's undocumented, but if you have opportunity to test GM:S2 and import existing game, then you find it's used there for compatibility scripts. Since GM:S and GM:S2 share same runner (or very similar), it works on GM:S 1.4 also!

    You can add more scripts to it if you need to divide code, you can also refer to a different script than current one. It doesn't matter, since it's not a function in fact, but a directive to pre-compiler, so it's removed from scripts during compile time (calling script which have gml_pragma() inside won't execute it again).
     
    Last edited by a moderator: Nov 2, 2016
  2. icuurd12b42

    icuurd12b42 TMC Founder GMC Elder

    Joined:
    Apr 22, 2016
    Posts:
    1,839
    whoa. nice find
     
  3. ZeDuval

    ZeDuval Member

    Joined:
    Jun 20, 2016
    Posts:
    117
    Awesome! Are there any more undocumented parameter for gml_pragma()? Because "forceinline" is the only one that's actually documented, while the documentation text says "The available commands are as follows:", I always wondered...
     
  4. gnysek

    gnysek Member

    Joined:
    Jun 20, 2016
    Posts:
    1,305
    Yes, I found it when importing game in GMS2. But it's also in GMS1 runner, and since definition for gml_pragma in GML is "gml_pragma(name, arguments...)", it works (I think that runner for GMS1 even supports layers and cameras from GMS2, but IDE won't allow you to use this functions and return syntax errors, while for gml_pragma it allows two arguments).

    It's possible, that it allows more options than "forceinline" and "global", possibly for PS4 and Xbox, but until somebody who ported game to those consoles will spoil info, it will be a mystery forever.
     
    xot likes this.
  5. icuurd12b42

    icuurd12b42 TMC Founder GMC Elder

    Joined:
    Apr 22, 2016
    Posts:
    1,839
    But how did you find "global" and "a_function_name()" was a valid pragma? that would cause the system to call that function on start?
     
  6. gnysek

    gnysek Member

    Joined:
    Jun 20, 2016
    Posts:
    1,305
    As I said, if you import game in GMS2, it will add compability scripts. Since there's no more depth in GM2, but layers only, it creates a script with list of objects and their depths, and there's a code
    Code:
    // NOTE: MacroExpansion is used to insert the array initialisation at import time
    gml_pragma( "global", "__global_object_depths()");
    Comment states, that this code is run at import time (when loading data from EXE to memory), so before game loops starts, that's why it game_restart() doesn't run it again (as it don't import data from EXE again).
     
  7. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,459
    Thats incredible! I was looking for something like this! YYG really aught to document this! Makes GML extensions far more powerful!
     
  8. Under Logic

    Under Logic Member

    Joined:
    Jun 24, 2016
    Posts:
    7
    Does this still work? How will I know? Doesn't seem to pop up at all on the console.
     
  9. gnysek

    gnysek Member

    Joined:
    Jun 20, 2016
    Posts:
    1,305
    Yes, it works. Why it should popup at console?
     
    Under Logic likes this.
  10. Under Logic

    Under Logic Member

    Joined:
    Jun 24, 2016
    Posts:
    7
    Oh nevermind, I forgot to put "show_debug_message" in the script so I could see if it added the variable like it should. It does indeed display the variable in the console. Is there a way to access the variable after the game starts? No objects seem to recognize it.
     
  11. ZeDuval

    ZeDuval Member

    Joined:
    Jun 20, 2016
    Posts:
    117
    The variable you want to initialize there must be a global.variable!
     
    Under Logic likes this.
  12. Under Logic

    Under Logic Member

    Joined:
    Jun 24, 2016
    Posts:
    7
    Ahhhhhhhh. I made it more complicated than it should've been. xD Thank you.
     

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