# Expressions

## This section deals with the different mathematical expressions.

Expressions can be real numbers (e.g. 3.4), hexadecimal numbers starting with a \$ sign (e.g. \$00FFAA), strings between single or double quotes (e.g. 'hello' or "hello") or more complicated expressions. For expressions, the following operators exist (in order of priority):

## Assigning

= - Used to assign a value to a variable. Note that this can also be used for comparing variables in GameMaker: Studio 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);

## Combining

&&, ||, ^^ (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

## Comparing

<, <=, ==, !=, >, >= - 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...}

## Bitwise

|, &, ^, <<, >>: 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...};

## Arithmetical

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

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

## Increment/Decrement

++, -- - 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.

## Division and Modulo

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);

## Unary

Also, the following unary operators exist:

• ! (not): not, turns true into false and false into true
• -: negates the next value
• ~: negates the next value bitwise

### Summary

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.

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 nor 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, 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:

Back: GML Overview
Next: The "If" Statement