Jump to content

April 2017 »

23 24 2526272829

Recent Comments

- - - - -

State Machines in Games – Part 1

4: Adsense

I recently had a conversation with a beginner on GameDev.net about the importance of state machines.

Most of the beginners on the site are teenagers enrolled in secondary school. Often they pick up concepts here and there over the Internet when they think it will help, but don’t take the time to learn topics that don’t seem interesting.

State machines. Bah! Who needs them, right?

Games need them.

Let’s get the boring part over with…

The Computer Science Part

Finite State Machines, FSMs, or Finite State Automata, are an abstract machine. The machine is made up of multiple states, also called a node or vertex. The machine transitions to a new state by a trigger or event or condition. The transitions are also called edges. One node is designated as the start node. Notes may be marked as exit nodes. The machine enters at the start node, transitions to new states, and eventually ends on an exit node.

So what does that mean?

We can explain the same state machine in many different ways. It is usually easier to understand a state machine when it is put into a pretty picture. It is usually easier to modify, codify, and program state machines when they are in tables. So we’ll do both.

Let’s start with this very simple state machine:

Attached Image

Note that there are 2 entries for state 2. That is because it has two transitions.

Graphically it looks like this:

Attached Image

But we don’t have to use boring names like 0, 1, 2, and 3 for state names, just like we don’t have to stick with x and y for variable names.

Let’s redraw the graphs with better names:

Attached Image

Suddenly it looks much less like a boring theory topic and more like something to do with games.

In fact, it looks like an NPC’s game logic.

Implementing a simple state machine

Now we can start out simple implementation.

For the common quick-and-dirty simple state machine that will never change, we can simply hard code the machine into the code. In that case the programmer calls on his favorite little friend, the switch statement:
public class StateMachine
public enum State
State mState = State.Routing;
Random rng = new Random();

public string GetStateName()
return mState.ToString();
public string UpdateState()
return "Running state " + GetStateName() +". Your game logic goes here.";

public void NextState()
switch (mState)
case State.Routing:
mState = State.Sentrying;
case State.Sentrying:
mState = State.Attacking;
case State.Attacking:
// TODO: Make this based on game logic instead of random name generator
if (rng.NextDouble() < 0.75)
Console.WriteLine("Random generator says NPC has survived.");
mState = State.Routing;
Console.WriteLine("Random generator says NPC did not survive.");
mState = State.Ending;
case State.Ending:
// Nothing to do.
public bool IsDone()
return mState == State.Ending;

"But wait", you might exclaim, "That doesn’t look like a big ugly table or a fancy graph!" That’s right. I said above that you can have many different ways to represent a state machine, and this is one of them.

Always Leave Them Wanting More

This sums up the first lesson. You know what a state machine is in an abstract way. You saw that they might potentially have some uses in games. And you hopefully want to know more.

Stop by next time for the second lesson about extending the basic state machine.

Source code is attached. You can download it to get a sneak peak at what is coming up.

Attached File  StateMachineTutorials.zip (13.42KB)
downloads: 113

Mar 11 2013 02:27 PM

I am loving these articles

Mar 17 2013 01:02 AM

Great write-up!  Have you given any thought to also posting it -- perhaps with all the posts collected together -- as an article using the new system?

Mar 19 2013 09:32 AM
Yet I learned nothing.
Mar 20 2013 08:49 PM

Yet I learned nothing.

What exactly are you looking to learn?

Mar 21 2013 01:43 AM

I dunno. Why do we use strings for states? Wouldn't that slow it down if we used the state machine for RTS units?

Mar 22 2013 02:32 PM

I dunno. Why do we use strings for states? Wouldn't that slow it down if we used the state machine for RTS units?

This part of the series is a text-based dungeon room explorer. In this text-based example a text-based state machine makes sense to me.

For an RTS it would likely use enums (integer constants) which is something I did in the later parts of the series.

Note: GameDev.net moderates comments.