·

Learn GML: Variables

What is a Variable?

A variable is a container that has a name and holds onto a piece of information you want to remember, change, or use later.

In GameMaker, variables are how your game keeps track of things. They store numbers, text, and all sorts of data inside your game.

Almost everything in coding depends on variables.
They’re how your player keeps their score, how enemies know their health, and how your game remembers where things are on screen.

When you create a variable, you’re basically saying,

“Hey GameMaker, I need a place to store this information. Hold onto it for me.”

Making a Variable

To make (or “declare”) a variable in GML, you give it a name and assign it a value. Here are a few examples:

var _num = 126.4545;
var _str = "Hello World";
new_num = _num * 100;
my_string = _str + " I said";

Here’s what’s happening:

  • var _num = 126.4545; creates a variable named _num and stores a number in it.
  • var _str = "Hello World"; stores a piece of text (called a string).
  • new_num = _num * 100; makes a new variable and sets it to _num multiplied by 100.
  • global.my_string = _str + " I said"; creates a variable and adds more text to it.

Variable Data Types

Each variable can hold a specific type of information, called a data type.
Here are the most common ones you’ll use:

TypeExampleDescription
Value100, 2.45, -56Any number with or without decimals.
String"Hello world!"Text inside quotation marks.
Booleantrue or falseA yes/no value—perfect for switches and conditions.

You’ll also see variables hold more complex data later on (like arrays and structs), but for now, numbers, words, and true/false values are your main tools.

Variables Can Store Results

Sometimes, you don’t give a variable a number or word specifically, you give it the result of a function.

var _id = instance_nearest(x, y, obj_tree);
root = sqrt(1000);
global.string = string_upper("Hello World");

Here you’re saying:

Each one takes a function’s output and tucks it neatly into a variable.

Assigning Variables

When you first create a variable, you’re assigning a value to it.
That just means you’re giving it a name and telling it what default value to hold.

a = 100;
b = 200;
c = 300;

You can also perform an operation at the same time:

a += b;    // add b to a
a = b + c; // store the result of b + c in a

That little = symbol is called the assignment operator.
It doesn’t mean “equals” like in math; It means put this value into that variable.

GameMaker also accepts := for assignment, though most people just use =.

a := 50;

Other Operators

You can change values using quick shortcuts:

For example:

score += 10; // Add 10 points to score
lives--;     // Lose one life

Naming Your Variables

Just like people, every variable needs a name. This name is how you’ll refer to it later, so GameMaker knows exactly which “box” you’re talking about.

When naming variables, follow a few simple rules:

  1. Start with a letter or an underscore _
    • score, _health, carSpeed
    • 6fish (can’t start with a number)
  2. Only use letters, numbers, or underscores
    • num1, player_score, highscore_2025
    • foo bar, *num, or anything with spaces or special symbols
  3. Keep it short, but clear
    • GameMaker allows up to 64 characters per name but shorter, meaningful names are easier to work with.
    • Instead of numberOfEnemiesRemainingInLevelTwo, you could just write enemy_count.

Readability in GameMaker is super important. You’ll come to find that the more your game looks like plain English, the easier it is to read.

Wrap Up

A variable is a named storage box where your game keeps information.
You can fill it, read from it, or change it at any time.