·

State Machines in GameMaker

State Machines in GameMaker: A Simple Beginner-Friendly Guide

If you’ve ever written code like this:

if (keyboard_check(vk_left))
{
    x -= 4;
}

if (keyboard_check_pressed(vk_space))
{
    yspeed = -8;
}

you’ve already started building character behavior. But as your game grows, managing different actions like walking, jumping, attacking, climbing, and dying can quickly become messy.

That’s where state machines come in.

In this tutorial, we’ll build a simple state machine in GameMaker and learn how it keeps your code organized, scalable, and easy to maintain.

Before you Begin

You’ll want to be familiar with the following concepts:

What Is a State Machine?

A state machine is a programming pattern where an object can only be in one “state” at a time.

For example, a player might have these states:

Instead of checking every possible action every frame, we let the current state decide what the player should do.

At any given moment, the player is only in one state.

Step 1: Create State Constants

In the Create Event:

enum PlayerState
{
    Idle,
    Walk,
    Jump
}

state = PlayerState.Idle;

move_speed = 4;
gravity = 0.4;

Using an enum makes states easier to read than using raw numbers.

Step 2: Create a State Switch

In the Step Event:

switch (state)
{
    case PlayerState.Idle:
        ... // Idle behavior
        break;

    case PlayerState.Walk:
        ... // Walk behavior
        break;

    case PlayerState.Jump:
        ... // Jump behavior
        break;
}

This is the heart of the state machine.

Each frame, GameMaker runs the code associated with the current state.

Why State Machines Are Useful

Without a state machine, your Step Event often grows into hundreds of lines of nested conditions.

if (!dead)
{
    if (!attacking)
    {
        if (!climbing)
        {
            if (!stunned)
            {
                ...
            }
        }
    }
}

With a state machine, behavior becomes isolated.

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

    case PlayerState.Attack:
        ...
        break;

    case PlayerState.Climb:
        ...
        break;
}

Each state only worries about its own logic.

A Common Improvement: State Enter Functions

As projects grow, you’ll often want code to run only once when entering a state.

For example:

function ChangeState(_new_state)
{
    state = _new_state;
}

Then instead of:

state = PlayerState.Jump;

you use:

ChangeState(PlayerState.Jump);

This allows you to later add animation changes, sound effects, or setup code in one place.

When Should You Use a State Machine?

State machines are ideal for:

Any object that has distinct behaviors can benefit from a state machine.

Final Thoughts

State machines are one of the most useful programming patterns in GameMaker. They make your code easier to read, easier to debug, and much easier to expand as your game grows.

Start small with a few states like Idle, Walk, and Jump. Once you’re comfortable, you can add Attack, Dash, Climb, Hurt, and any other behaviors your game needs.

The key idea is simple:

An object should only be responsible for the behavior of its current state.

Comments

Leave a Reply

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