Book Image

Gamemaker Essentials

4 (1)
Book Image

Gamemaker Essentials

4 (1)

Overview of this book

Table of Contents (16 chapters)

Statements and loops


In nearly every programming language, there is the option to use statements such as if and else. Often, we need to use loops such as the for loop. Statements and loops make a programmer's life much easier when it comes to keeping code readable and organized, so let's take a look at how these look in GML.

Loops allow developers to greatly reduce the amount of code needed to execute a repetitive task. Instead of using multiple if statements, the programmer can summarize it all into a single loop, keeping the code short and concise.

Statements

First, we will look at the simpler of the two statements. We will begin with the most commonly used statement, the if statement.

The if statement

The if statement allows us to ask our game questions and then tell it what to do depending on the answer. The answer will either be true or false, never anything else.

In GameMaker, an if statement can be structured in multiple ways. An experienced programmer will often use brackets to enclose...