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

gnysek

Member
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:

ZeDuval

Member
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...
 

gnysek

Member
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.
 
  • Like
Reactions: xot
I

icuurd12b42

Guest
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?
 

gnysek

Member
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).
 

GMWolf

aka fel666
Thats incredible! I was looking for something like this! YYG really aught to document this! Makes GML extensions far more powerful!
 
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.
 
Top