In GameMaker, code is written in blocks. A block is a group of instructions, called statements, that tell your game what to do. Each statement runs in order, and together they create the logic behind your game.
A code block can do something simple, like add two numbers, or something complex, like make an enemy run away when its health is low. At the most basic level, a program looks like this:
<statement>;
<statement>;
...
Each line ends with a semicolon ( ; ), which works kind of like a period in a sentence. It tells your game, “Okay, that instruction’s done, now let’s move on to the next one!”
Grouping Statements
Sometimes you need several statements to run together. You can group them with curly brackets {} to form a single code block. This is common inside conditionals, such as if statements:
if (<conditional>)
{
<statement>;
<statement>;
...
}
All statements inside the brackets will run if the condition is true. You can read that like:
“If this happens, then do everything inside these brackets.”
Using begin and end
GameMaker also accepts the keywords begin and end in place of curly brackets.
if (<conditional>)
begin
<statement>;
<statement>;
end
Most coders like the curly brackets better, but both work!
Example
Here’s how a short code block might look inside the GameMaker Script Editor:
if (enemy_health < 20)
{
enemy_speed += 2;
enemy_run_away = true;
}
Let’s translate that into plain English:
“If the enemy’s health drops below 20, make it run faster and tell it to run away.”
Try It Out
You aren’t an expert just yet, but let’s see if you can figure out what the code in these examples are trying to say:
hunger = 5;
eat_breakfast();
brush_teeth();
if (cookie_flavor = "raisin")
{
throw_away();
}
if (is_wednesday)
{
gather_trash();
take_out_trash();
}