• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code [SOLVED... AGAIN] Ternary operations don't work in macros. Bug? No. Answer in post

hippyman

Member
I tried to make a simple one liner singleton macro

Code:
#macro IMGUI instance_exists(imgui) ? instance_find(imgui,0) : instance_create_layer(0,0,"Instances",imgui)
But for some reason this doesn't work. Do ternary operations not work for macros or does this have something to do with the instance functions?

I wanted to make sure that this is expected before I reported a bug.


EDIT: I found the REAL problem.

I need to assign the final value to a variable like this

Code:
#macro IMGUI __imgui = instance_exists(imgui) ? instance_find(imgui,0) : instance_create_layer(0,0,"Instances",imgui)
This code here works.
 
Last edited:

Coded Games

Member
Ok, in GMS 1.4 you could do this but in GMS 2 macros have to be constant values. Your best bet would be to just write a script to do this instead.
 

hippyman

Member
Ok, in GMS 1.4 you could do this but in GMS 2 macros have to be constant values. Your best bet would be to just write a script to do this instead.

Oooooooooh... that was a pretty important detail that I managed to completely read past in the docs. Thanks for mentioning that. Makes sense now.
 

GMWolf

aka fel666
Ok, in GMS 1.4 you could do this but in GMS 2 macros have to be constant values. Your best bet would be to just write a script to do this instead.
This is simply not true!
In GMS2 I use non constant macros all the time!
Just watch one of my streams they always end up with me abusing macros.
You can even split up statements in myltmult macros.
Things like
Code:
#macro loop for(var i
#macro from =
#macro to ; i <
#macro do ; i++)

loop from 5 to 10 do show_message(i);
This may be a bug?
Try wrapping the statement in parentheses.
 

hippyman

Member
This is simply not true!
In GMS2 I use non constant macros all the time!
You are sort of correct and I overlooked another detail. It works with single functions. Perhaps expressions aren't possible anymore.

If you need the value of a macro to change at run-time then you should probably make it a global variable, since these can be changed from code during a game, unless you set the macro to be a function. By setting the macro to a function it means that this function will be called every time you use the macro. For example:
Code:
#macro col make_colour_hsv(irandom(255), 255, 255)

Code:
#macro loop for(var i
#macro from =
#macro to ; i <
#macro do ; i++)

loop from 5 to 10 do show_message(i);
What on Earth is that?! You definitely didn't test this lol
 

Coded Games

Member
Wow, that super interesting. I was just citing the docs for GMS2 which straight up says "Basically, a macro (also called a constant when used like this) is a named variable that holds a constant single value (or string)." and has no mention of uses like you describe. The GMS1 docs show using expressions in macros.
 

hippyman

Member
Wow, that super interesting. I was just citing the docs for GMS2 which straight up says "Basically, a macro (also called a constant when used like this) is a named variable that holds a constant single value (or string)." and has no mention of uses like you describe. The GMS1 docs show using expressions in macros.
I think that might have just been an exaggerated example of what might be possible with macros. But what he posted is definitely not valid code. I checked it just to be sure because it would have blown my mind, but it just doesn't work like that.

The docs do state that you can still call functions but this doesn't work for some reason. It's still possible this is a bug. I'd like to hear from a YYG staff member hopefully just to definitively clear this all up.
 

GMWolf

aka fel666
You are sort of correct and I overlooked another detail. It works with single functions. Perhaps expressions aren't possible anymore.

If you need the value of a macro to change at run-time then you should probably make it a global variable, since these can be changed from code during a game, unless you set the macro to be a function. By setting the macro to a function it means that this function will be called every time you use the macro. For example:
Code:
#macro col make_colour_hsv(irandom(255), 255, 255)


What on Earth is that?! You definitely didn't test this lol
No I didn't test it. It may need some tweaking.

But check this out:
As you can see im CLEARLY not using constants here!
It's not even an expression, or a statement. Its more like... A fragment, I guess.

In a couple livestreams of mine I even wrapped double for loops as single macros.
 

hippyman

Member
That's definitely a creative use for macros. I'm thinking that it's just a bug with ternary operations. Good chance this is a bug.
 

bjshnog

Member
I don't know if it's changed in the last few months, but I've heard that macros effectively function as a global search and replace in the code upon compiling, so any instances of "IMGUI" would be replaced with "instance_exists(imgui) ? instance_find(imgui,0) : instance_create_layer(0,0,"Instances",imgui)" in your code.
It might work if you put a pair of parentheses around the expression you're substituting in, as someone has already suggested, like this:
Code:
#macro IMGUI ( instance_exists(imgui) ? instance_find(imgui,0) : instance_create_layer(0,0,"Instances",imgui) )
Also, I can confirm that things like this actually work, for the reason I mentioned:
Code:
#macro a scr0(
#macro b obj1
#macro c ,x,
#macro d y);

a b c d     // equivalent to scr0(obj1,x,y);
 

hippyman

Member
I posted the answer in the original post at the top. I love how a ton of us thought it was the parentheses lol
It was actually that I needed to assign the return value of the ternary operation to a variable in the macro.
 

bjshnog

Member
Hmm, would it have worked if you had put the expression (in your macro) in parentheses and then used __imgui = IMGUI in your code?

(edit: actually tbh you wouldn't even need to use parentheses if you did that)
 

Jobo

Member
GMC Elder
I can't provide a comment on the OP because I don't understand what's wrong, but I can clear up the macro confusion...

Macros are compile-time substitution, they are not "constant values". Given a macro
Code:
#macro SHOUT show_debug_message
all uses of this macro become substituted with its value at compile time.
Code:
if(true) SHOUT("AaaOOooEee");
becomes
Code:
if(true) show_debug_message("AaaOOooEee");
 

hippyman

Member
I can't provide a comment on the OP because I don't understand what's wrong, but I can clear up the macro confusion...

Macros are compile-time substitution, they are not "constant values". Given a macro
Code:
#macro SHOUT show_debug_message
all uses of this macro become substituted with its value at compile time.
Code:
if(true) SHOUT("AaaOOooEee");
becomes
Code:
if(true) show_debug_message("AaaOOooEee");
That's definitely helpful! It makes more sense for why it originally didn't work. I was just misunderstanding how macros worked in GMS 2.X



One things that's really funny about all of this is the singleton above is completely pointless.

You never need to make any calls to that object. You just need to create it at the start.

Which means a single instance_create_layer call would have sufficed.
 
Last edited:
Top