Handling Game State Management in C++

One thing that is initially overlooked in game design is handling menus and levels within the user interface. However, we know that it’s always going to be, and is, a crucial part to any successful game, as it helps provide the user with an extra level of control and eases general flow. It’s just a case then of finding the code to do it.

This tutorial will cover a simple technique for handling state management with enums or “enumerated types“. If you have used state management or even just enums in C# before, then this will all feel very familiar to you. There are however some very subtle differences between the markup of the two languages.

First off, I’ll assume that you already have a program that runs inside of a window, ready for holding graphics and text. You should also have a main game loop that you use for any updates.

Setting the Enum Type

We start by setting our enum. It’s set like a structure, in the way that we can create an object with this enum as its type. It doesn’t hold any values itself though – the key is that it can only be set to certain values that you give it. I’ll cover this a bit better below.

This should go before your game loop (so that it doesn’t keep trying to set itself and cause errors).

1
enum eGameState {GSM_MENU, GSM_LEVEL, GSM_END};

Looks a lot like a structure, or even an array.

The first part should be okay. It’s just setting our new enum type and giving it the name of “eGameState” so we can use it later.

The stuff inside the braces are the values that you want it to hold. These technically don’t mean anything, as they’re just given IDs from 0 upwards, i.e. GSM_MENU = 0, GSM_LEVEL = 1…

Basically, each item in that list is turned into a constant (which is why I like to use capitals). It is similar to declaring them separately with #define or const.

The constants shouldn’t be defined before they go into the enum, otherwise you’ll get an error. Creating the type will do it for you.

Declaring the Variable

We then create an instance as we would for a class or struct:

1
eGameState gameState = GSM_MENU;

You can set this to be whatever option you like from the list that you put into your game state type, inside the braces. Remember, ONLY the options from the list can be used (the ones in capitals).

The Switch-Case

To check our gameState var, we set up a switch statement. It means we can check one variable against lots of values, which is what we want. Plus it saves us dealing with loads of really annoying “else if” statements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
switch (gameState)
{
  case GSM_MENU :
  {
    // Menu state
  }
  break;
  case GSM_LEVEL :
  {
    // Level state
  }
  break;
  case GSM_END :
  {
    // End state
  }
  break;
}

This goes inside our main game loop.

We then write whatever we like into each part and treat it as a new state. For example, if you check for a button press to go from the menu into your level area, you just change the value of gameState.

1
2
3
4
if(buttonPressed)
{
  gameState = GSM_LEVEL;
}

When the game loop updates again, the switch will then find that the gameState has changed and go to the new screen.

If you already have a game set up without state management, you can replace the code inside your main loop with this, and insert your original code into the “Level state” section.

That should cover it. Any questions, leave some comments below or contact me on Twitter @_bigSteve.

Have fun :)


9 thoughts on “Handling Game State Management in C++

  1. That,s a great idea. Though I’m not much of a gamer but I will definitely try this tool in some
    other directions connected with flash format. Thanks for sharing.

    1. Yes you can apply the same rules to any other game or software language out there. In fact, I’ve used this same technique in a Flash game myself.

    1. You’re absolutely right, but I’m all in favour of simple concepts. This post will serve as a middleman between not knowing anything about game state management at all, and your very useful link. Thanks.

  2. This is really good advice, I can finally get to grips with changing the state of my game. Although im stuck with my code? Not sure if you can help.

    enum eGameState {GSM_MENU, GSM_LEVEL, GSM_END};
    eGameState gameState = GSM_MENU;
    —————————–
    switch (gameState)
    {
    if( gameState != broken)
    { case GSM_MENU :};
    {
    // Menu state
    }
    break;
    if( gameState != broken)
    { case GSM_LEVEL :};
    {

    // Level state
    }
    break;
    if( gameState != broken)
    { case GSM_BREAK :};
    {
    // End state
    }
    break;
    }
    ————————-
    if(Shoot == 1)
    {
    gameState = break;
    }

    I have used your integers but i cant get my head around fixing the game state once it ‘breaks’? Is this what patching a game means..

    Sorry im new to Visual Basic.

    1. Whoops found my mistake, I was typing my code in Dreamweaver. Silly me, no wonder my update didnt run. Dreamweavers compiler doesnt understand Visual Basic code who knew!

      Anyways I got another problem, it seems my sprite class wont draw?

      int Sprite

      if(private override public Draw (int:) =! null)
      {{
      Fullbatch.sprite.draw.int;
      Update.draw;
      }}
      endif;

      ??

      1. How long have you been coding for? Is this your first game? I recommend you start with something basic and follow a STEP BY STEP tutorial to create a game first and then keep modifying and playing about with it to understand how the code works. I recommend using c sharp and the xna framework to get started…

Comments are closed.