• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
  • entries
  • comments
  • views

State Machines in Games

Sign in to follow this  
Followers 0


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:


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

Graphically it looks like this:


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:


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{Routing,Sentrying,Attacking,Ending}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;break;case State.Sentrying:mState = State.Attacking;break;case State.Attacking:// TODO: Make this based on game logic instead of random name generatorif (rng.NextDouble() < 0.75){Console.WriteLine("Random generator says NPC has survived.");mState = State.Routing;}else{Console.WriteLine("Random generator says NPC did not survive.");mState = State.Ending;}break;case State.Ending:// Nothing to do.break;}}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.


Sign in to follow this  
Followers 0


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?


Share this comment

Link to comment

Yet I learned nothing.

What exactly are you looking to learn?


Share this comment

Link to comment

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


Share this comment

Link to comment

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.

Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now