GameMaker: Studio

GameMaker: Studio

View Stats:
Can you do both "or" and "and" in the same statement?
Just wondering if something like this would work or not.

if (global.example==true) && (global.ovenpotato==false) || (totalenemies<3)

< >
Showing 1-7 of 7 comments
Jeb May 3, 2017 @ 3:51am 
Yep.
MrDave May 3, 2017 @ 5:18am 
I would use brackets to make sure the order is the way you expect.
if ((global.example==true) && (global.ovenpotato==false)) || (totalenemies<3)
Harry White May 3, 2017 @ 5:31am 
Originally posted by MrDave:
I would use brackets to make sure the order is the way you expect.
if ((global.example==true) && (global.ovenpotato==false)) || (totalenemies<3)
Okay, thanks.
Daynar May 3, 2017 @ 6:57am 
Also you dont need to check if a boolean == true, as that is redundant.
if bool will trigger if the variable is true and if !bool will trigger if it is false.
if (global.example && !global.ovenpotato) || totalenemies < 3
MrDave May 3, 2017 @ 8:18am 
Oh another thing on this topic, if you have many arguments inside an IF statement GML will calculate them all even if one has already passed or failed the criteria.

This means it is more efficient to do nested IF statements.

This:
if (a > 0) { If (a < 10) { //do } }
Is faster than this:
If (a > 0 and a < 10) { // do }
Daynar May 3, 2017 @ 12:20pm 
Originally posted by MrDave:
Oh another thing on this topic, if you have many arguments inside an IF statement GML will calculate them all even if one has already passed or failed the criteria.
I don't think that's true anymore. I believe they updated it. I couldn't find any documentation about it so I did a test.

<start script> ///if_check() show_debug_message("if_check() executed!") return true <end script> <start first room creation event> if true && if_check() show_debug_message("test1 triggered") if !(false && if_check()) show_debug_message("test2 triggered") if true || if_check() show_debug_message("test3 triggered") if false || if_check() show_debug_message("test3 triggered") <end first room creation event> <start output> if check executed! test1 triggered test2 triggered test3 triggered if check executed! test3 triggered <end output>
This would suggest that gamemaker doesn't check all statements if it doesn't have to.
MrDave May 3, 2017 @ 2:42pm 
Thanks Daynar, my information was correct at one point: http://www.yoyogames.com/blog/23
Originally posted by YoYoGames:
In many languages, the moment any one of the checks returns false, the rest are skipped and the next function is stepped into. However GameMaker:Studio will evaluate ALL of them, even if the first one is false. Therefore it is recommended that you "nest" your ifs
But it looks like it has been updated. Sorry for the wrong info.
< >
Showing 1-7 of 7 comments
Per page: 1530 50

Date Posted: May 3, 2017 @ 3:48am
Posts: 7