Variables, Operators & Expressions
It is time to dive in to some programming fundamentals. If you have any previous experience in other programming languages this may seem elementary, but given the subtle differenced between GML and other programing languages it still serves to pay close attention to these fundamentals to ensure a strong foundation.
Variables
Variables will be the basic building blocks of your programs. Variables allow you to store and retrieve data essentially whenever and wherever you like. An extremely basic example would be:
apples = 10;
now the variable apples is equal to 10. If you were to use apples in an equation, for example, the number 10 would be substituted wherever apple is:
apples = 10;
bananas = 5;
fruits = apples + bananas;
// The variable fruits is now equal to 15
Variable Types
In Gamemaker there are a number of different variable types, please consult this list to learn about the differences. It is important to remember variable types when doing mathematical operations or providing arguments for functions, as for instance you cannot add a number and a string together.
bullets = 10;
// The following code gives an error
ammo = ammo + "bullets";
// The following code works correctly
ammo = ammo + bullets;
Scope
Scope is a critically important concept that is often a source of confusion for even intermediate programmers. To nip this potential maelstrom in the bud, an in depth explanation of scope follows:
Scope refers to where in the program a particular variable can be recognised and accessed. We think it is easiest to clarify with an example. Imagine that in a two player game there are two objects, obj_player_1 and obj_player_2. Both of these objects have a variable called health which stores the player’s health value. Although we have two variables with the same name, this is perfectly legitimate code because each of the health variables are in the scope of their respective player objects. Therefore if some code in obj_player_1 uses the health variable, it will use its own health variable, likewise if code obj_player_2 uses the health variable it will use its own. In Gamemaker there are 3 different scopes that variables may be in:
- Instance Variables:
An Instance Variable is the most typical variable type you will use in Gamemaker. It is any variable which is defined in an Object, or more specifically an Instance, and is specific and unique to that instance.
// Instance Variables are defined "normally", without any additional syntax
ammo = 10;
show_debug_message(ammo);
- Local Variables:
A Local Variable is only accessible in the specific Event of Function that it is defined in. For example, if you create a local variable in the Create event of an object it will be accessible from within that Create event but nowhere else.
/* Local Variables are defined by putting "var" in front of them. Afterwards,
you don't need to use "var" to access them. Notice how they are a yellow colour.*/
var ammo = 10;
show_debug_message(ammo);
- Global Variables:
Global Variables are accessible from anywhere in your code. These are useful for data that must be accessed in a variety of places and times, although a degree of caution should be taken to avoid creating large amounts of unnecessary global variables as they do remain in memory.
/* Global Variables are defined by putting "global." in front of them. In
Gamemaker they will be a Pinkish colour.*/
global.ammo = 10;
show_debug_message(global.ammo);
Now, imagine a scenario where some code in obj_player_1 needs to know what obj_player_2's health is. How do we do that? We use the dot accessor, AKA the humble ".".
total_health = health + obj_player_2.health;
Notice that the health variables are green? This is because they are one of the inbuilt variables in gamemaker. There are a number of such inbuilt variables, for example x and y for the x and y position of objects in a room, that will appear green to help distinguish them from other variables. You can find more details about these, as well as other variable types and scope, in the manual .
Operators
Now we know about variables, it is time to learn about what we can do with these variables. First, you can do all the basic mathematical operations:
twentyone = 9 + 10; // 21, oh wait 19
a = 5 * (4 - 3); // 5
b = 10 / 2; // 5
c = 0 - 7; // -7
Additionally more complex mathemtical operations can be performed:
count++; // Increments count
count--; // Decrements count
x = 5 mod 3; // Modulo operation (returns remainder) (can use % instead of mod)
x = 5 div 3; // Division operation (returns quotient)
We can also do all manner of comparisons:
x == y; // Equals to
x != y; // Not equals to
x > y; // Greater than
x >= y; // Greater than or equal to
x < y; // Less than
x <= y; // Less than or equal to
Including Boolean combinations:
if (a == b && c == d) { do something... } // and
if (a == b || c == d) { do something... } // or
if (a == b ^^ c == d) { do something... } // xor
There are more operators intended for specific scenarios which can be perused in the manual .
You may have noticed an interesting keyword if in the previous code block, which will be coming up shortly when we cover Conditionals and Loops.
Expressions
Put simply, expressions are any kind of mathematical phrase in programming. Any combination of numbers, variables, strings and functions can be included in an expression, along with at least one operator. In fact, many of the lines of code above are expressions. Below you will find some examples of expressions missing some operators, as a bit of a brain teaser.