added 56 characters in body
Source Link
Philipp
  • 109.5k
  • 22
  • 244
  • 308

Note that you can also have more than one state-machine in your game. For example, your function combat_logic might in turn be implemented by a state machine itself switching between states like "select unit", "select unit action", "select action target", "execute action", "execute ai" etc. This pattern of state-machines within states of other state-machines is called "Hierarchical State Machines".

Note that you can also have more than one state-machine in your game. For example, your function combat_logic might in turn be implemented by a state machine itself switching between states like "select unit", "select unit action", "select action target", "execute action", "execute ai" etc. This pattern is called "Hierarchical State Machines".

Note that you can also have more than one state-machine in your game. For example, your function combat_logic might in turn be implemented by a state machine itself switching between states like "select unit", "select unit action", "select action target", "execute action", "execute ai" etc. This pattern of state-machines within states of other state-machines is called "Hierarchical State Machines".

Source Link
Philipp
  • 109.5k
  • 22
  • 244
  • 308

When your game has different modes it's in, and each mode should have different logic and different UI, then that's often a use-case for the state machine pattern.

So instead of having all your game logic in your one logic()-function, you create a separate logic-function for each state your game can be in.

You might also want to create an enter-function and leave-function for each state which do the logic for initializing and cleaning up the state. In order to keep everything that belongs to one state together, it can be a good idea to keep those three functions for each state together in one source file for each state. Or if your code gets too long for that, you might want to create a separate folder for each state.

Your logic-function would then look something like that:

void logic(void) {
    /* stuff you want to happen in every state */

    case (currentState) {
        STATE_WALK:
            walk_logic();
            break;
        STATE_DIALOG:
            dialog_logic();
            break;
        STATE_COMBAT:
            combat_logic();
            break;
    }
}

Now you can switch states by changing the value of currentState between a bunch of constants. But wait, remember to call the enter and leave functions. To make sure you don't skip them, you might want to create an utility function for that:

void switchState(int newState) {
    // exit the old state
    case (currentState) {
        STATE_WALK:
            walk_leave();
            break;
        STATE_DIALOG:
            dialog_leave();
            break;
        STATE_COMBAT:
            combat_leave();
            break;
    }
    // enter the new state
    case (newState) {
        STATE_WALK:
            walk_enter();
            break;
        STATE_DIALOG:
            dialog_enter();
            break;
        STATE_COMBAT:
            combat_enter();
            break;
    }

    // NOW we can change the state
    currentState = newState;
}

Note that you can also have more than one state-machine in your game. For example, your function combat_logic might in turn be implemented by a state machine itself switching between states like "select unit", "select unit action", "select action target", "execute action", "execute ai" etc. This pattern is called "Hierarchical State Machines".