J
Jerek
Guest
I just upgraded to GMS 2 and love it so far. However, I'm a bit confused as to what happened to macros as they are no longer in the resource tree. Any idea where they are or if it's an obsolete feature now?
#macro Winw window_get_width()
I can be certain you don't need to call the script, since I have an enum/macro script that I never call and it works fine.Huh, I had a suspicion about macros working like that but I assumed you'd still have to call the script. I'll have to test that. I hate to be a non-believer but it raises some questions about what happens if you macro a variable before it is declared. how does it know?
#macro testMacro test
var test = "Test";
show_debug_message(testMacro);
That's exactly how they work. When not referencing a local var like in your example, but referencing an instance- or an global var, you don't even need to "initialize" it the first time with it's original name, but use the macro right from the start. However, if you reference a var(and not a number, string, expression or function) it needs to be initialized before it's first usage just like with vars using them "normally".I can be certain you don't need to call the script, since I have an enum/macro script that I never call and it works fine.
I did a quick test. This code works fine too:
I assume macros work kind of like a pre-compile find and replace, so it doesn't really matter where they are declaredCode:#macro testMacro test var test = "Test"; show_debug_message(testMacro);
/// initialize_stuff()
// this is where the magic happens
gml_pragma("global","initialize_stuff()");
// init_Enums
enum day{
monday, tuesday, nextday, otherday
}
// init_Constants
#macro myName global.__myName
#macro releaseYear 2016
// init_Globals
global.__debugMode = true;
myName = "SlimShady";
Oh, wow! That's really neat. I wonder why this pragma functionality wasn't documented! I know I'm saying goodbye to manual init now!That's exactly how they work. When not referencing a local var like in your example, but referencing an instance- or an global var, you don't even need to "initialize" it the first time with it's original name, but use the macro right from the start. However, if you reference a var(and not a number, string, expression or function) it needs to be initialized before it's first usage just like with vars using them "normally".
Bonus round:
If you'd want to have all your macros, enums and globals in one place(script) and have it so that you don't need to call it even once in the beginning, do it like this:
Credit for discovering this neat, little trick goes to @gnysekCode:/// initialize_stuff() // this is where the magic happens gml_pragma("global","initialize_stuff()"); // init_Enums enum day{ monday, tuesday, nextday, otherday } // init_Constants #macro myName global.__myName #macro releaseYear 2016 // init_Globals global.__debugMode = true; myName = "SlimShady";
Exactly my thoughts! As to why it's not documented, I don't know, but it will be in the future in one way or another...Oh, wow! That's really neat. I wonder why this pragma functionality wasn't documented! I know I'm saying goodbye to manual init now!
So this gml_pragma script does what exactly? I'm only guessing from your example, and it seems this would be incredibly useful, but is this a way of running a script at game start -without- relying on the room creation code? I mean, that is literally on my wishlist so I'd be psyched to know that exists. That said, what are the arguments? The second one seems self-explanatory, but some of what you have there is confusing. I figured it was for scope, but in your myName example, you macro __myName but declare myName. Is that a typo on your part?Exactly my thoughts! As to why it's not documented, I don't know, but it will be in the future in one way or another...
And it doesn't stop at global vars, I think you can also already create datastructures, for example.
PS: Works with GM1.4 and GM2!
// make myName a reference to the global variable
#macro myName global.__myName
// assign a value to the global variable
// both following lines are the same
myName = "SlimShady" // using the macro
global.__myName = "SlimShady" // or the variable itself
// without doing this, the following would cause an error when used in some draw event
draw_text(0,0,myName);
/// gml_pragma(instruction,arg,...)
switch argument[0]{
case "forceinline":
// embed this script-code directly at every place where the script is used
exit;
case "global":
script_execute(asset_get_index(argument[1]));
// and do it before the actual game starts
exit;
}