Expressions


An expression is a mathematical phrase that can contain ordinary numbers, variables, strings, or GML functions as well as one or more operators (like add, subtract, multiply, etc...). The values used in an expression can be real numbers (e.g. 3.4 * 6), hexadecimal numbers starting with a $ sign (e.g. $00FFAA | $88FFAA), strings between double quotes (e.g. "hello" + "world") or more complicated expressions using multiple operators and values.

For expressions, the following operators exist (in order of priority):

= - Used to assign a value to a variable. Note that this can also be used for comparing variables in GameMaker Studio 2 and you may see this in examples and other peoples codes. However, this is a legacy from old GameMaker versions and you should use the == operators for comparing and = for assigning, as shown in these examples:

a = 12;
speed = 5;
val = (old_val + 5);


&&, ||, ^^ (and, or and xor) - Combine Boolean values to give either true or false. If any of the following examples resolves to true then the code would be run:

if (a == b && c == d) {do something...}// and
if (a == b || c ==d) {do something...}// or
if (a == b ^^ c == d) {do something...} // xor


<, <=, ==, !=, >, >= - These are comparisons and can only give a true or false result (where true can also be interpreted as 1, and false as 0). Examples of use:

if (a < b) {do something...}
if (a != b) {do something...}


|, &, ^, <<, >>: You can perform bitwise operations with these, where | = bitwise or, & = bitwise and, ^ = bitwise xor, << = shift left, >> = shift right. Examples of use:

x = (x & $ffffffe0) + 32;
if (y ^ $1f) > 0 {do something...};


You can find additional information on how to use the bitwise operators and what they do from the section: Bitwise Operators.

+, -, *, / - Add, subtract, multiply and divide. Examples of use:

c = a * b;
str = a + "world";


++, -- - Add or subtract one from a value. It is worth noting that placing this before or after the value to be added to or subtracted from will have slightly different results. For example:

  • ++a will increment the variable and return the incremented value.
  • a++ will increment the variable but return the value before it was incremented.
Therefore, if you have something like this:

var a = 1;
show_debug_message(string(a++));
show_debug_message(string(++a));


The debug output would be 1 and 3. Here are some examples of use:

for (var i = 0; i < 10; i++;) {do something...};
if hit == true score--;


NOTE: On the YoYo Compiler target platforms (those marked (YYC)), these expressions are evaluated from left to right, while on all other target platforms they are evaluated from right to left, meaning that this:

val = max(num, ++num, num++);


will give different results depending on the platform.

div, mod (%) - Division and modulo, where div gives you the amount a value can be divided into producing only an integer quotient, while mod gives you only the remainder of a division. Examples of use:

secs = time mod 60;
time_str = string(time div 60);


Also, the following unary operators exist:
  • ! : boolean "not", so !true == false
  • - : negates the next real or integer value (not valid for strings or booleans)
  • ~ : negates the next value bitwise

As values in all expressions you can use numbers, variables, or functions that return a value, and sub-expressions can be placed between brackets too. All operators work for real values, but comparisons also work for strings and "+" concatenates strings.


Order Of Operations

When doing multiple operations in a single expression, it is very important that you use brackets () to separate out the order of operation, as different platforms may perform them differently if not explicitly stated in this way. For example, consider the following code:

a = b == c || d;

The different target compilers will perform the operations in different orders since they are not explicitly shown, giving rise to "odd" results that you may not expect when you play your game. to avoid this, use the () to separate out the parts, like this:

a = (b == c || d); //better
a = ((b == c) || d); //best

Here are some final examples of the various different expressions:

{
x = 23;
colour = $FFAA00;
str = "hello world";
y += 5;
x *= y;
x = y << 2;
x = 23 * ((2 + 4) / sin(y));
str = "hello" + "world";
b = (x < 5) && !(x==2 || x==4);
}


Accessors

There are also expression "short-cuts" called accessors for use with certain Data Structures and Arrays, which enable you to add, or replace data within these formats quickly and easily and without the use of any function calls. For full details, please see the following page:

  1. Accessors