• 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!

GameMaker Using OR to compare different values

S

Spencer S

Guest
Just a quick question:
will this work?
Code:
var a = 1;
var b = 1;
var c = 2;
if a == (b || c) {
    // execute code
}
Or do I have to write it out like this:
Code:
var a = 1;
var b = 1;
var c = 2;
if (a == b) || (a == c) {
    // execute code
}
 
Ignoring the input's you've got written down there for abc, those logical processes are very different.

The first one is basically saying "does a equal (is b or c true)?"

side note: a real number apparently is considered true if it is greater or equal to 0.5.

The second one is saying "does a equal b or does a equal c?"
 

Yaazarai

Member
You've got to look at the evaluation of the comparison.

All operators work from left to right with the left operand being compared to the right operand. From there parenthesis take precedence first, so whatever happens in paranethesis will happen before whatever is outside of the parenthesis.

So when we look at comparisons they look like htis:
Code:
left_operand || right_operand
left_operand && right_operand
left_operand ^^ right_operand
...
Now look at what happens when we add parenthesis:
Code:
temp_right_operand = left_operand || right_operand
result = left_operand || temp_right_operand
Now if we expand this code to get right of the temporary variable we get:
Code:
result = left_operand || (left_operand || right_operand)
Just like with our code using the temporary variable we do the first part in parenthesis (equivalent to temp_right_opernad) and then we compare the result of that to the left_operand and store that in the result variable.

To sum it all up:

Operators compare from left to right with the variables/functions/formulas on the immediate left and immediate right of the operator. You have to be careful though because if you don't use parenthesis you code is privy to the order of operations which arguably most people don't know the order of--I don't off the top of my head. So if you want to be safe always use parenthesis.

For example read the GMS2 order of operations help page.
 
Top