Let's Talk About State Machines!

Sections: State Machines? State Manager? Enums? What's Next?

What are State Machines?

State Machine Diagram State machines are rules that define where a player is in the game's story. Think of a flow chart. You start in the beginning state then you check to see if you meet the criteria to move to another state. If so, then you do, and the way these machines are defined is entirely up to you as the programmer. States can only allow for input and output, meaning the player can only enter or leave the state. Some States like the beginning and end state only have one function. Players can leave the start state, but they cannot enter it and vice versa with end state.

What is a State Manager?

A state manager is a concept that I came up with when developing Prison Escape. The concept is that you have a Game Manager Entity that holds the States enum class and it's responsible for tracking the player's state through the game. This is what a state manager looks like in pseudo code.

void StateManager(State playerState, playerInput){ switch(playerState) { case matchingPlayerState: if(playerInput == expectedInput) player.state = States.newState; break; } }

Enums, what are they and why do they exit?

Enums are a type of data structure found in C# that allow the programmer to create there own set of data types. For example, if the programmer was making a card game they might create an enum to hold the different suits a card can be. This is what an enum looks like in pseudo code.

enum Suits { Spades, Diamonds, Hearts, Clubs }

The programmer can create a new card and set it's suit like so:

Card newCard = new Card(cardValue, Suits.Spades);

That's good and all, but...

So you may be wondering what state machines have to do with games. Well, they are a handy and simple way to track a players progress through a story. This is extremely helpful in determining the player experience as they travel from point A to point B. In my experiences it's best to use this kind of mechanism on small short story style games. You can even break the game's story up into scenes or "chapters" and each one contains it's own state machine and state machine manager. This is how I would scale a small visual story game into a much larger experience. I will likely cover the topic of scene management in a later blog.