·

Learn GML: Enums

Introduction

Let’s consider the code below:

direction = 1;

This works, but what does “1” actually mean in this context? Is it one of the four cardinal directions? Is it an angle? Who knows!

This is where enums come in.

Enums (short for enumerators) let you replace confusing numbers with readable names, making your code easier to understand.

What Is an Enum?

An enum is a way to group related constant values under a single name.

Instead of writing:

if (state == 2)

You can write:

if (state == PlayerState.Jump)

This makes your code much clearer at a glance. No more guess-work with what “2” could mean in this context.

Why Enums Are Useful

Enums help you:

Think of them as labels for numbers.

Creating Your First Enum in GameMaker

You define an enum like this:

enum PlayerState
{
    Idle,
    Walk,
    Jump,
    Attack
}

GameMaker automatically assigns numbers behind the scenes:

Idle = 0
Walk = 1
Jump = 2
Attack = 3

And now you don’t need to remember the numbers anymore. You just use the names of the enumators.

Using Enums in Code

Now instead of writing raw numbers:

state = 0;

You write:

state = PlayerState.Idle;

And when checking state:

if (state == PlayerState.Walk)
{
    x += 4;
}

This is much easier to read and maintain.

Enums Work Great With Switch Statements

One of the most common uses is with state machines:

switch (state)
{
    case PlayerState.Idle:
        sprite_index = spr_idle;
        break;

    case PlayerState.Walk:
        sprite_index = spr_walk;
        break;

    case PlayerState.Jump:
        sprite_index = spr_jump;
        break;
}

Without enums, you’d be stuck remembering what each number means. With enums, the code explains itself.

You Can Assign Custom Values Too

Enums don’t have to start at 0 or increment automatically.

enum EnemyType
{
    Slime = 10,
    Bat = 20,
    Golem = 30
}

This is useful if you want grouping, spacing, or compatibility with external data.

Enums for Game Settings

You can use enums for many systems.

Input types:

enum InputType
{
    Keyboard,
    Gamepad,
    Touch
}

Menu states:

enum MenuState
{
    Main,
    Options,
    Credits
}

Enemy behaviors:

enum AIState
{
    Patrol,
    Chase,
    Attack,
    Retreat
}

Common Beginner Mistake

A lot of new GameMaker users do this:

if (state == "idle")

This works, but it’s slower, less safe, and prone to typos like:

"idle" vs "idl"

Enums avoid this entirely because the compiler checks them for you.

After typing in “AIState.” GameMaker will show you a list of elements from the enum!

Enums vs Variables

Here’s a simple way to think about it:

Example:

hp = 100; // variable

state = PlayerState.Idle; // enum

Final Thoughts

Enums are one of the simplest upgrades you can make to your GameMaker code, but they have a huge impact.

They turn messy, number-based logic into clean, readable systems that scale with your game.

If you’re building anything with states, menus, enemies, or settings, enums should be your default choice.

Put Them Into Action!

Enums are particularly useful in:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *