• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML using brackets in IF statements? (does it matter?)

Dr_Nomz

Member
So this is how most of my IF statements look:
if (keyboard_check(vk_shift) == true)

But, does it NEED brackets? And if so, why? What's the difference with or without them? Should I always use brackets?
 
In GML, with this specific example, you don't need the brackets for an if statement. It's really only necessary for stuff like BEDMAS.

You can also ditch the " == true" bit as well for such a function return. Any value 0.5 and up is considered true, or 1 in GML, and so a function returning 1 doesn't need a comparison which would also return 1.
 

Neptune

Member
I feel like brackets add a lot of clutter in some places... so I avoid them when its easily understandable what the conditions are.
Code:
if jump
{

}
As apposed to:
Code:
if (jump && (this || that || run)) || paused
{

}
Even in this example, I usually skip the furthest bounding set of brackets...
Because GM is lenient about it, it is totally up to you, but its necessary when using && and ||, otherwise your logic will be messed up.

And as, Battlerifle said, these are the same:
Code:
if jump {}
if jump == true {}
if jump == 1 {}
if jump >= 0.5 {} //I guess?
 
D

dannyjenn

Guest
I personally always put the condition in parentheses, but it's not required.

And as, Battlerifle said, these are the same:
Code:
if jump {}
if jump == true {}
if jump == 1 {}
if jump >= 0.5 {} //I guess?
Actually, your third example (and maybe your second example) won't work unless jump is a boolean (set to either true, false, 1, or 0). Because suppose jump is not a boolean, and suppose jump has a value of 3. It would then be substituted as follows:
Code:
if(3){ } // works, because anything >=.5 is regarded as true. 3>=.5, so 3 is regarded as true.
if(3==true){ }
if(3==1){ } // <-- notice the problem? hint: 3 is not equal to 1
if(3>=0.5){ } // yes, 3 is bigger than .5
So the third example is clearly wrong. But as for the second example... I'm not sure. The constant true has a numerical value of 1, so by substitution it has the same problem as the third example. But it's possible that the compiler somehow regards ==true as >=0.5 (I don't know).
 
Top