Geenat.com Nathan’s blog about software development, design and the people behind it.

Video Game State Machines, The Easy Way!

Written on April 2nd, 2011

First, what’s a State Machine?

State Machines are used in everything from video games to applications to embedded systems such as control stations. In the case of video games, they can be used for actor logic, basic artificial intelligence, screen management, level management, or anything else in need of organized state-driven logic.

If you’re a newcomer to state machines, they may sound complex, but in reality are fairly simple and easy to implement in code.

What does a State Machine look like?

The example pseudo-code below is written with C in mind, but the concepts are directly translatable to most other languages including Java, Python, and Flash/Actionscript.

State Machine using traditional switch/case in C:

enum
{
    STATE_IDLE = 0,
    STATE_RUN,
    STATE_JUMP,
    STATE_DEAD
}

int state = STATE_IDLE;

void StateUpdate()
{
    switch(state)
    {
        case STATE_RUN:
            printf("Run logic.");
        break;
        case STATE_JUMP:
            printf("Jump logic.");
        break;
        case STATE_DEAD:
            printf("Dead logic.");
        break;
    }
}

void Update()
{
    // Change the state.
    state = STATE_RUN;

    // Run the current state.
    StateUpdate();
}

As illustrated above, the most common and obvious way to implement a State Machine is through the use of a variable to hold the current state, a number of pre-defined constants to represent different states, and a structure of control statements such as switch/case to perform logic based on the current state.

Looks simple, right? How could we improve on this?

Function pointers can make extremely simple State Machines that work without any extra constants or conditional statements. Function pointers are built into most languages in one form or another. Consider the following:

State Machine using a function pointer in C:

void (*StateUpdate)();

StateUpdate = NULL;

void StateRun()
{
    printf("Run logic.");
}

void StateJump()
{
    printf("Jump logic.");
}

void StateDead()
{
    printf("Dead logic.");
}

void Update()
{
    // Change the state.
    StateUpdate = StateRun;

    // Run the current state.
    StateUpdate();
}

Easy. These are of course very simple examples; in a real video game you’re likely to have initialization states and multiple State Machines running alongside each other.

Since writing this article, I’ve had a number of people ask me how to implement the above example in a language other than C. Below I will illustrate how one could implement the same State Machine in a higher level language such as Python. In Python, functions are first class datatypes, which means you can re-assign them like variables. No function pointers needed!

State Machine using first class functions in Python:

def StateUpdate():
	pass

def StateRun():
	print "Run logic."

def StateJump():
	print "Jump logic."

def StateDead():
	print "Dead logic."

def Update():
	StateUpdate = StateRun
	StateUpdate()

What can I say, Python rocks!

Posted in C, Game Development, Programming, Python

Send me a Coffee!

Did I help you solve a problem? You're welcome! If you feel like sending a token of your gratitude, you're welcome to do so using Paypal

2 Responses to “Video Game State Machines, The Easy Way!”

  1. Josh Says:

    Great article – very straightforward and a good basic definition.

    I hope you continue on with your ideas on handling complex state machines – what if there are restrictions on which states are allowed (run->jump->run and not run->jump->jump) or the state itself is stateful (run->jump in the air->run)? With the examples above, what extra methods or validation would you add?

  2. ODev Says:

    clearly explained the concept..thanks for the post.

Leave a Reply

Submit Comment