Lesson 2
Variables
Learn how variables work.
Values
Code, at the very core of it, is all based on binary values, either a 0 or a 1. But in higher level languages, like GML, we get to pretend that it’s a bit more flexible. So instead of everything being binary, we have a few different “types” of data we can represent:
Strings > "Hello", "gun", etc
Real numbers > 100, 52.5, etc
Booleans > true / false
And so on. So if we are setting the position of something, we’d want to use a real value: not "100", but 100. If we wanted to represent a name, we’d use a string: not Paul, but "Paul".
Variables
Once we have types of data, we need to store them somewhere. That’s where variables come into the picture.
A variable is simply a storage container that holds some data that can vary over the course of the execution of the program (vary > variable). This is in contrast to a constant, which is a similar concept, except the value cannot change, it’s fixed to a single value for the execution of the program.
In order to use a variable, we must first “declare” it. In GML, this is as simple as assigning it a value with the equals sign:
my_hp = 100;
There we have created a variable called my_hp and assigned it the real number 100. We’ve declared the variable, in other words. Sometimes there’s some confusion because in normal parlance, because equals can intuitively mean equality, but in code it means assignment. We’ll learn about equality a bit later.
In GM, some variables come “pre-declared” by GM itself, such as variables like x and y. We don’t need to declare these built-in variables ourselves.
But, like in the case of the my_hp variable, there’s plenty of times when we’ll want to create our own little packet of data that GM hasn’t automatically set up, and so for these “custom” variables, we do need to declare.
Code flow
Code executes in order from top to bottom. Essentially, it’s an ordered list of instructions. This is often known as the “flow of the code” or “code flow”.
One line executes, then the next line executes, and so on.
x = 100;
y = 100;
In that set of instructions, first x is set to 100 and then y is set to 100. The two don’t happen in parallel, they happen sequentially.
This might seem a little obvious, but all too often I see beginners make coding mistakes where they have incorrect ordering of instructions according to the flow of the code.
hp = 100;
hp = hp + 50;
In order to be able to add 50 to hp, we first have to have declared it and assigned it a value. So the code has to flow in this particular way in order to be correct: declaration first, modification afterwards.
One final note for this lesson: You might have noticed semicolons ; at the end of each line. This is essentially code “punctuation”.
A semicolon ; ends a statement, and a statement is basically one complete instruction. So
x = 100Is the statement (a complete instruction), and to let the computer know “Hey, we’ve finished with this instruction, the next text will be a part of the next instruction”, we cap it off with a semicolon.
x = 100;It’s almost exactly the same as how we use a full stop to denote the end of a complete sentence, so that readers know the next text is going to be a part of the next sentence.
GM will usually let you get away with not using semicolons, but it’s far better code design to include them (and there are certain situations where not including them will cause bugs, so better to get into the correct habit from the start).