If / else statementsIf statements are used when wanting things to happen only if a certain condition is met. They take the form:
if (a condition is met)
{
perform actions
}When the condition is met (ie it is true) then all the actions you put inside the braces {} will be performed. If the condition is not met then none of the actions will be performed. Note that you need a closing brace } for every opening brace that you use { if you do not have an equal number then you will get an error. It is important to use braces so then GM knows which actions it is only supposed to perform when the condition is true. If you don't use braces then only the first action you put will be conditional on the if statement and the others will be executed all the time regardless.
Checking Boolean ValuesA Boolean value is one that can only be true or false. Here is a basic example of testing a Boolean in an if statement:
if (place_meeting(x,y,obj_wall))
{
speed = 0;
}This code would check whether you are meeting a wall or not and if so it would set the speed to 0. This is an example of a function returning a value that can only be true or false. You can also check variables value, for example if you have a Boolean variable called message_allowed, then you could use this code:
if (message_allowed)
{
show_message("Hello");
}This would only show "Hello" when message_allowed is set to true and would not do anything if it's set to false. You could also check for a Boolean value being false, this can be done using the ! operator (which means
not in gml). ie:
if !(place_meeting(x,y,obj_wall))
Will check if you are
not meeting a wall. And
if !(message_allowed)
Will check if message_allowed is not true (ie it is false).
Checking Variable Comparisons As well as just checking Boolean values you can also check variable comparisons. A basic example would be:
if (health == 100)
{
lives = 3;
}This code would only set lives to 3 when health is
equal to 100. Notice the difference that a single = is used when
assigning a value to something, but a double == is used when
comparing values. As well as comparing values to be equal to each other you can also compare:
if (health < 100)
This will check is if health is
less than 100.
if (health > 100)
This will check is if health is
greater than 100.
if (health <= 100)
This will check is if health is
less than or equal to 100.
if (health >= 100)
This will check is if health is
greater than or equal to 100.
if (health != 100)
This will check is if health is
not equal to 100. NOTE: Make sure you do not try to use if (!health == 100) when trying to check if a variable is not equal to a value, this is a common mistake. The problem is GM will actually read the code as if (!health) == 100, this is something completely different to what you want. If you do want to use this make sure you use brackets in the correct location, ie if !(health == 100) this is now the same as using if (health != 100), this error occurs most often when brackets are not used with if statements.
That is all the comparisons you can use in gml.
Combining conditionsThere is also the possibility to combine conditions, for example:
if (x > 10 && x < 100)
{
zone = 1;
}This checks if x is greater than 10
and x is less than 100 and sets the variable zone to 1 if it is (the && sign means
and in gml). For this condition to be met x
must be both greater than 10 and less than 100. Another combination you can use is:
if (sprite_index == spr_walking || in_air)
{
image_speed = 3;
}This checks if the sprite_index is walking
or the variable in_air is true (the || sign mean
or in gml). For this condition to be met the sprite index can be either walking
or the variable in_air can be true or both condition can be true, basically the only time when it will not be met is when the sprite_index is not walking and in_air is false.
The last way which you can check combinations is using ^^ (the ^^ sign means
xor in gml). This is exactly the same as || except now the condition is not met when
both are true, only when one or the other is true. You do not really need to concern yourself with this operator though as it is infrequently used and you are unlikely to need it.
Else StatementsElse statements are used when wanting something to happen upon the if statement condition not being met. It takes the form:
if (condition is met)
{
perform first action
}
else
{
perform other action
}Basically what happens is if the if condition is met it performs the first action as normal otherwise (if it is not met) it performs the other action. For example:
if (message_allowed)
{
show_message("Hello");
}
else
{
show_message("Disabled");
}Like normal this will show "Hello" when the variable message_allowed is true but now when message_allowed is not true it will show "Disabled". Another example:
if (x > 10 && x < 100)
{
zone = 1;
}
else
{
zone = 2;
}This will set zone to 1 when z is greater than 10
and less than 100 as shown previously otherwise (if x <= 10 or x >= 100) it will set zone to 2. You can also combine else statements with an if statement and then even multiple else statements if you want. For example:
if (value == 3)
{
show_message("1st statement");
}
else if (value == 4)
{
show_message("2nd statement");
}
else if (value == 5)
{
show_message("3rd statement");
}
else
{
show_message("other statement");
}What this does is it will show "1st statement" when value is 3 otherwise it will show "2nd statement" when value is 4 otherwise it will show "3rd statement" if value is 5 otherwise (if value is not 3,4 or 5) it will will show "other statement".
Switch StatementsSwitch statements can be used when wanting to perform different actions depending on the value of a single variable. In fact it pretty much is used in replacement to that long if, else code I just wrote. Taking that as an example it could be written instead using a switch statement like so:
switch (value)
{
case 3: {show_message("1st statement"); break;}
case 4: {show_message("2nd statement"); break;}
case 5: {show_message("3rd statement"); break;}
default: {show_message("other statement");}
}The advantage of doing so is it is then easier to read and recognise. How a switch statement works is it checks the value given after it in the brackets (often this is just a variable used here) then it checks for any cases you have put which match this value. Upon finding the case it then executes
ALL the rest of the code after it including the code in other cases. This is why you need to put that
break there otherwise all the rest of the code will be executed included the cases where the value is not matched, it can be easy to forget to use break so try to always remember. The default case at the is used when none of the given cases match the value, this is the equivalent of using that final else statement at the end.
It is important to note that all this is doing is checking for a matching
value it is not actually checking for a variable matching a value.You cannot use things like switch (x, y) to switch 2 variables and you
cannot use comparisons like:
switch (x)
{
case < 40: {}
case > 40: {}
}As things like this do not work you will need to use a normal if statement instead.
There is another thing to note when using switch statements, which is when you want multiple cases to perform the same action. It can now be done easily like so:
switch (value)
{
case 4: case 5: case 9: {show_message("1st action"); break;}
case 2: {show_message("2nd action");}
}This will show "1st action" when value is equal to 4,5 or 9 and show "2nd action" when value is equal to 2. It is the equivalent of using the following if statement set:
if (value == 4 || value == 5 || value == 9)
{
show_message("1st action");
}
else if (value == 2)
{
show_message("2nd action");
}ExpressionsThis part is more advanced and you are unlikely to need to know this at a beginners level so you can skip straight to the 'Uses Of Variables' section (link
here) if you cannot follow it.
In the manual you might have seen that if statements are actually explained to have the form:
if (expression is true)
{
perform statement
}Well what we have been dealing with are referred to as expressions, an expression is a combination of values, variables, operators, or functions that are calculated to a value. For example when you use:
x < 100
What GM actually does is it reads that expression and returns a value of 1 (if the expression is true) or 0 (if the expression is false), effectively that expression is just equivalent to the value of 1 or 0 depending on whenever it is true or not. You can even assign an expression to variable, like so:
less_than_hundred = (x < 100);
The variable less_than_hundred will then equal 1 or 0 depending on whether the expression is true or not because like I said the expression x < 100 when ran by GM is effectively just the evaluated value (in fact
all assignment values are referred to as expressions as we will come onto in a bit). Now how if statements work is they evaluate the
entire expression within the brackets after it, then check whether the expression is true or false. If it is true then it executes the code inside the first braces, if it is false it executes the code inside the else braces. General code, like assignments is referred to as a statement in case you are wondering what it means in the manual by this.
So what GM is actually looking at is this:
if (entire expression is true)
{
perform statements
}That is basically it.
But just to add to the confusion, there is another thing that GM has to deal with.. expressions do not always have to return a Boolean true or false value. For example (x == 0)+7 is an expression, it will equal 7 or 8 depending on whether x == 0 or not. Even 4+2 is an expression it will equal 6 and a value just of it's own is an expression. As I mentioned earlier all assignment are just calculated expression, now when assigning these expression to a variable it make sense for them to be able to be non Boolean values but when using it as a condition in an if statement they often don't really make any sense. For example:
if ((x == 0)+7)
or
if (4+2)
Neither of these you would have any practical use, however GM still needs to be able to handle them if they are used. It does actually says in the manual how GM handles things:
If the (rounded) value is <=0 (false) the statement after else is executed, otherwise (true) the other statement is executed
This means (note the
rounded bit in there) that if an expression in an if statement is evaluated as < 0.5 it is considered false and if it is evaluated as >= 0.5 it is considered true. You may actually occasionally see this functionality exploited by people to shorten code, for example in this code:
col_inst = collision_line(x1, y1, x2, y2, obj, prec, notme);
if (col_inst)
{
move_towards_point(col_inst.x, col_inst.y, 4);
}If you check the manual you will see that the collision_line function does not actually return a Boolean true or false, it in fact returns the instance id of an instance if one is found, if one is not found it returns a negative number, therefore the col_inst variable is actually being set to a value like 100102 or -4. The reason this code still works is because when it finds a an instance it will return an instance id which is a large positive number (therefore col_inst will be evaluated as true since it will be >= 0.5) and when it does not find an instance it will return a negative value (therefore col_inst will be evaluated as false since it will be < 0.5). The non shortened code should actually look like this:
col_inst = collision_line(x1, y1, x2, y2, obj, prec, notme);
if (col_inst > 0)
{
move_towards_point(col_inst.x, col_inst.y, 4);
}It is technically bad notation to drop the > 0 comparison and exploit how GM handles non Boolean expressions to make the code shorter, however it is often done by people when using functions which return an instance id and in some other scenarios also, so try to watch out for people doing it in case it causes you any confusion.