I've recently discovered a clutch of "rogue" operators in GML: operators that are fully functional but totally undocumented in the help file. While I wouldn't recommend using any of them (they might make your code less portable), they might be interesting to be aware of.
:=
When first n00bishly using GML, I did what I'm sure a lot of others did: I used the =
operator for comparisons, e.g. if (a = b)
, as well as assignment, e.g. a = b
. Turns out this is really bad practice if you ever plan on learning other languages (like, say, javascript), because they treat the two very differently. Assignments in javascript actually return the right-hand value, not true/false; so if you were to say if (a = 0)
the conditions would never be true, just as if you'd said if (0)
. The solution, of course, is to use the comparison operator ==
for condition testing, e.g. if (a == b)
, and use =
only for assignments.
So where does :=
come into all this? If GML matched other languages, it should be purely an assignment operator, e.g. a := b
and not if (a := b)
. But since GML is special, it's merely a synonym for =
. Yep, you can say if (background_color := c_blue)
and it'll do the exact same thing as if (background_color = c_blue)
or if (background_color == c_blue)
in GML.
Dumb. Like I said, these aren't recommended.
<>
This is just a straight synonym for !=
. It returns true if the left-hand and right-hand expressions are not equal.
!<, !>
You would think these were cute alternatives for the and
operators: whereas you might say
to test if
a
is less than or equal to b
, you could also say to test if
a
is not greater than b
for the identical result. However, you would think wrong, even though there are some places around the net that suggest it's true. In GM 8.0, at least, these do not work; they throw a syntax error if used in code. (Though, oddly, they'll just be ignored if used as part of watchers in debug mode: will just return
a
no matter the right-hand variable is.)
|=, &=, ^=
Okay, so these are documented in the help, but not in the operators section, and there's also a confusing typo where they're listed (suggesting that one of them is ). But they are very useful if you know them; they're the bitwise brethren to the
+=
, -=
, *=
, and /=
operators. For example, a |= b
is equivalent to a = a|b
.
Thanks for them!
ReplyDelete