General idea of a game state machine

A game state machine runs within the update cycle of the game loop. A game state machine is the mechanism of binding all the game states together. In old techniques, this was a typical linear control flow. However, in modern development processes, it can be parallel control running in multiple threads. In the old architecture of game development, it was encouraged to have only one game thread. Developers used to avoid parallel processing as it was vulnerable to game loop and timer management. However, even in modern development, many developers still prefer to use a single thread for game development whenever possible. With the help of various tools and advanced scripting language, most game developers now use a virtual parallel processing system.

One of the processes of a simple game state machine is to create a common state interface and override it for each game state. In this way, it becomes easy to manage the state inside the game loop.

Let's see a loop of a simple game state machine manager. This manager should conduct four main functionalities:

  • Creating the state
  • Updating the state
  • Rendering the state
  • Changing the state

An example implementation might look like this:

public class MainStateManager
{
  private int currentStateId;
  //setting up state IDs
  public Interface GameStates
  {
    public static final int STATE_1 = 0;
    public static final int STATE_2 = 1;
    public static final int STATE_3 = 2;
    public static final int STATE_4 = 3; 
  }

  private void initializeState(int stateId)
  {
    currentStateId = stateId;
    switch(currentStateId)
    {
      case STATE_1:
        // initialize/load state 1
      break;
      case STATE_2:
        // initialize/load state 2
      break;
      case STATE_3:
        // initialize/load state 3
      break;
      case STATE_4:
        // initialize/load state 4
      break;
    }
  }
}
/*
* update is called in every cycle of game loop.
* make sure that the state is already initialized before updating the state
*/
private void updateState()
{
  switch(currentStateId)
  {
    case STATE_1:
      // Update state 1
    break;
    case STATE_2:
      // Update state 2
    break;
    case STATE_3:
      // Update state 3
    break;
    case STATE_4:
      // Update state 4
    break;
  }
}
/*
* render is called in every cycle of game loop.
* make sure that the state is already initialized before updating the state
*/
private void renderState()
{
  switch(currentStateId)
  {
    case STATE_1:
      // Render state 1
    break;
    case STATE_2:
      // Render state 2
    break;
    case STATE_3:
      // Render state 3
    break;
    case STATE_4:
      // Render state 4
    break;
  }
}
/*
* Change state can be triggered from outside of manager or from any other state
* This should be responsible for destroying previous state and free memory and initialize new state
*/
public void changeState(int nextState)
{
  switch(currentStateId)
  {
    case STATE_1:
      // Destroy state 1
    break;
    case STATE_2:
      // Destroy state 2
    break;
    case STATE_3:
      // Destroy state 3
    break;
    case STATE_4:
      // Destroy state 4
    break;
  }
  initializeState(nextState);
}
}

In some cases, developers pass the input signal to a particular state through the state manager as well.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.109.8