INTRODUCTION TO GML:

INTRODUCTION TO GML:
CONDITIONAL STATEMENTS
In previous lessons we learned how to use conditional actions that
control whether or not a block of actions is executed (e.g. Check
Empty, Check Collision, Check Object, etc.). When writing code,
you can use conditional statements in a similar way.
A conditional statement starts with the keyword if followed by an
expression between parentheses.
If the expression returns true, the following statement or block of
statements is executed. So a conditional statement looks like this:
if (expression)
{
<statement>;
<statement>;
}
COMPARISON OPERATORS
The statements between the curly brackets are only executed when the expression is true.
In the expression, you can use comparison operators to compare numbers as follows:
<
<=
==
!=
>=
>
means smaller than
means smaller or equal
means equal (note that we need two = symbols, to distinguish the comparison from the
assignment)
means unequal
means larger or equal
means larger than
You can combine comparisons using && (which means and), || (which means or), or ! (which means
not). For example, to indicate that the x value should be larger than 200 but smaller than the y value,
you can write something like this:
if (x > 200 && x < y)
{
// Do something
}
Let’s say, for example, we want an object to wrap around the screen – in other words , if its
x-position gets smaller than 0 while it is moving to the left, we change the x-position to the right side of
the room (room_width), and when it gets larger than the width of the room and it is moving to the right,
we change it to 0.
Conditional Statements
Page 1 of 4
Here is what the code would look like:
if (x < 0 && hspeed < 0)
{
x = room_width;
}
if (x > room_width && hspeed > 0)
{
x = 0;
}
Make note of the opening and closing brackets. It is important to carefully check whether they match,
meaning whereever you have an open bracket, you must have a bracked that closes that bracket.
Forgetting a bracket is a very common error when writing programs.
The way you choose to format your code is entirely up to you. For example, some people prefer to write
if statements like this:
if (x < 0 && hspeed < 0) {
x = room_width;
}
if (x > room_width && hspeed > 0) {
x = 0;
}
In the first example, the opening of the curly brackets came after the if statement on a separate line and
the closing of the curly brackets came at the end of the statement to denote the end of the block. This is
often the preferred method because it makes it easier to see where an if statement begins and ends.
In the second example, the curly bracket came on the same line as the if statement.
There is a third way you can write an if statement. When only one statement follows a conditional
statement, we do not need the curly brackets at all. So, for the above example, we could rewrite our if
statements so that they look like this:
if (x < 0 && hspeed < 0) x = room_width;
if (x > room_width && hspeed > 0) x = 0;
Although this results in less lines of code, please keep in mind that this will only work when you only
have one statement following the conditional statement. If you require more than one statement, then
you will need to use either the first or second format.
THE else STATEMENT
A conditional statement can have an else statement that is executed when the condition is not true.
Conditional Statements
Page 2 of 4
The structure then looks something like this:
if (<expression>)
{
<statement>;
}
else
{
<statement>;
}
Let’s say, for example, we have a monster that should move horizontally in the direction of the player.
So, depending on where the player is, the monster should change its direction. To make this happen, we
can write the following conditional statement:
if (x < obj_player.x)
{
hspeed = 4;
vspeed = 0;
}
else
{
hspeed = -4;
vspeed = 0;
}
The above conditional statement tests whether the x-coordinate of the the monster (x) is smaller than
the x-coordinate of obj_player. If it is, we are setting the motion of the monster so that it moves to the
right; else, we set the motion of the monster so that it moves to the left.
THE else if STATEMENT
A common pattern for nested if statements is to have a series of if-else if statements with another else
if statement:
The general format of an if-else if statement is as follows:
if (expression1)
statement1
else if (expression2)
statement2
else if (expression3)
statement3
For example, suppose I want to change the sprite of an object depending on the direction it is facing. I
can easily use a series of if-else if statements to assign the sprites by doing the following:
if (direction == 0)
sprite_index = spr_facing_right;
Conditional Statements
Page 3 of 4
else if (direction == 45)
sprite_index = spr_facing_up;
else if (direction == 180)
sprite_index = spr_facing_left;
else if (direction == 270)
sprite_index = spr_facing_down;
It is important to keep in mind that your boolean expression must be properly ordered because
statements are executed for the first true condition only, and skips all the other conditions.
Conditional Statements
Page 4 of 4