The basics of state machines in GameMaker (Updated with better descriptions and visual aids)

One way to really keep your code organized in any engine for any type of game or simulation is to use something called a finite state machine, or just state machine for short.

What is a state machine?

As Wikipedia defines it:

A mathematical model of computation used to design both computer programs and sequential logic circuits. It is conceived as an abstract machine that can be in one of a finite number of states.

In layman’s terms, it’s a type of code structure based on fancy-shmancy flowcharts like this one:

state machine 2

Whipped this up in about a minute thanks to this nifty tool.

The general idea is that at any given time, your system is in one of several states defined by a distinct set of behavior. When certain conditions are met or certain actions are performed, the system will transition to another state.

You can use state machines for all sorts of things, but they’re particularly helpful for structuring menus, AI and character movement.

Visualizing a state machine:

Let’s say we want to make a state machine for a basic 2D platformer in which the player can move left and right, jump and grab corners. I tend to make a state for each type of movement — idle, walking, jumping, falling and hanging — but to keep things simple we can set up our state machine based on three sets of physics:

  • The Ground physics (horizontal movement only),
  • The Air physics (horizontal and vertical movement with gravity), and
  • The Hanging physics (no movement, you’re just snapped to the ledge)

So each of those is a separate state and actions like jumping, falling off ledges, etc. are the transitions. The state machine would look something like this:

state machine

Implementing the state machine:

Some game engines have their own state machine systems, or at least visual scripting tools that can be used to set up state machines. Unity, for example, has the Mecanim animation system. GameMaker has no such tool, but it’s easy to set up one with GML code. We’ll use the platformer example we just came up with.

First, add an enumeration at the start of the game to use as a shorthand for the different states:

    // Etc.

Next, define the current state variable in your player object’s Create event:

myState_Physical = STATE_PHYSICAL.ground;

And finally, put a switch statement in the Step event with a case for each state:

switch (myState_Physical)
    case STATE_PHYSICAL.ground:
        Script_PhysState_Ground ();
    }; break;
    case STATE_PHYSICAL.air:
        Script_PhysState_Air ();
    }; break;
    case STATE_PHYSICAL.hanging:
        // Etc.
    }; break;

That switch statement is essentially a bunch of if statements mashed together; it only processes a case if myState_Physical is equal to the given value, so if the player’s current state is STATE_PHYSICAL.ground it’ll call Script_PhysState_Ground (), if their state is STATE_PHYSICAL.air it’ll call the Air script, and so on and so forth.

And that’s really all there is to it! The transitions can be handled in a number of different ways; they could be as simple as setting the current state variable to a new value, or you could define scripts for switching to and from specific states. The important thing is that you have your code divided up in a logical, readable and manageable manner.

Additional notes:

Here’s a few other suggestions on using state machines effectively:

  • Depending on how complex and nuanced your character’s behavior is, you may want to define the behavior and transitions in a script for each state like I did above.
    • In fact, it might be best to store each script in a variable and call them with script_execute(); that way you only need to define the state machine in a parent object and you can change the behavior of the children by simply changing a few variables in their Create event.
  • Remember that you can use state machines for all sorts of mechanics, not just AI and movement:
    enum STATE_INPUT
    enum STATE_ANIM
        // Etc.

I may add some other suggestions as they come to mind. For now, happy gamedev! 😉


New video: An old Phanta-era fangame prototype

So I just remembered this was a thing and wanted to share it ASAP. The video’s still processing as of writing this, it might be having some issues with ContentID so I may have to update this post with a redone video later.

It’s is basically a canned crossover fangame/tech demo thingamajig I started during Phantasmaburbia’s development. The full details are in the video description over on the Youtube page.


Streaming for Operation Supply Drop!

Interested in watching me play Halo 4 with a bunch of folks way more experienced than me? My community college is doing a fundraiser for Operation Supply Drop, we’re streaming games until 9 PM tonight and from noon to 5 PM tomorrow. I’m going to be part of a Halo 4 tournament later, but we’re live right now at so go check it out and spread the word! 🙂

EDIT: So it looks like they won’t be able to broadcast the actual tournament, it’s just going to be more Titanfall footage while folks play Halo 4 in the background. At this point, I’m not even sure if there WILL be a tournament. So I guess whatever happens happens…


Hello, everyone! According to that header up there I must be this Michael Charnecki guy. I make stuff from time to time, usually games or game-related, and then I post it here if I feel it’s worth sharing. Click that “About” tab up there for more info!

If you’re here for the game dev resource list, you can find it under the “Miscellaneous” tab (direct link for impatient people). If you’re looking for all of my dumb, goofy personal stuff, go check out my Tumblr account or click on the Twitter, Facebook and Youtube icons up above.

As of writing this I’m still in the process of tidying things up around here, so apologies for any messy outdated or transitional stuff you may encounter! Thanks for visiting and have an awesome day!