Skip to main content
Woops. Yoda talk.
Source Link
David Gouveia
  • 25k
  • 5
  • 88
  • 127

I'd like to talk about an alternative that doesn't seem applicable from your example, but is actually very common and should be worth sharing - It's how I have always implement pausing in my games.

Have you ever used a stack based game state management system such as this to divide your game into states or screens? Besides being extremely useful at organizing and transitioning between multiple states (e.g. title screen, options screen, game screen, scores screen) it also makes pausing your game extremely easy - just push a new pause state on top of your current one.

For instance, here's what such a state could look like (in pseudocode):

class PauseState : GameState
{
    void Update()
    {
        if(OnKeyDown("P")) PopState();
    }

    void void Draw()
    {
        // Draw fullscreen black quad at 50% opacity
        // Draw "Pause" message in the middle of the screen
    }
}

And afterwards you could pause your game from any state just by doing:

if(OnKeyDown("P")) PushState(new PauseState());

Just set up your state manager so that it calls Draw on all states on the stack, but only calls Update on the topmost state. That's most the most important bit.

I'd like to talk about an alternative that doesn't seem applicable from your example, but is actually very common and should be worth sharing - It's how I have always implement pausing in my games.

Have you ever used a stack based game state management system such as this to divide your game into states or screens? Besides being extremely useful at organizing and transitioning between multiple states (e.g. title screen, options screen, game screen, scores screen) it also makes pausing your game extremely easy - just push a new pause state on top of your current one.

For instance, here's what such a state could look like (in pseudocode):

class PauseState : GameState
{
    void Update()
    {
        if(OnKeyDown("P")) PopState();
    }

    void void Draw()
    {
        // Draw fullscreen black quad at 50% opacity
        // Draw "Pause" message in the middle of the screen
    }
}

And afterwards you could pause your game from any state just by doing:

if(OnKeyDown("P")) PushState(new PauseState());

Just set up your state manager so that it calls Draw on all states on the stack, but only calls Update on the topmost state. That's most the important bit.

I'd like to talk about an alternative that doesn't seem applicable from your example, but is actually very common and should be worth sharing - It's how I have always implement pausing in my games.

Have you ever used a stack based game state management system such as this to divide your game into states or screens? Besides being extremely useful at organizing and transitioning between multiple states (e.g. title screen, options screen, game screen, scores screen) it also makes pausing your game extremely easy - just push a new pause state on top of your current one.

For instance, here's what such a state could look like (in pseudocode):

class PauseState : GameState
{
    void Update()
    {
        if(OnKeyDown("P")) PopState();
    }

    void void Draw()
    {
        // Draw fullscreen black quad at 50% opacity
        // Draw "Pause" message in the middle of the screen
    }
}

And afterwards you could pause your game from any state just by doing:

if(OnKeyDown("P")) PushState(new PauseState());

Just set up your state manager so that it calls Draw on all states on the stack, but only calls Update on the topmost state. That's the most important bit.

Source Link
David Gouveia
  • 25k
  • 5
  • 88
  • 127

I'd like to talk about an alternative that doesn't seem applicable from your example, but is actually very common and should be worth sharing - It's how I have always implement pausing in my games.

Have you ever used a stack based game state management system such as this to divide your game into states or screens? Besides being extremely useful at organizing and transitioning between multiple states (e.g. title screen, options screen, game screen, scores screen) it also makes pausing your game extremely easy - just push a new pause state on top of your current one.

For instance, here's what such a state could look like (in pseudocode):

class PauseState : GameState
{
    void Update()
    {
        if(OnKeyDown("P")) PopState();
    }

    void void Draw()
    {
        // Draw fullscreen black quad at 50% opacity
        // Draw "Pause" message in the middle of the screen
    }
}

And afterwards you could pause your game from any state just by doing:

if(OnKeyDown("P")) PushState(new PauseState());

Just set up your state manager so that it calls Draw on all states on the stack, but only calls Update on the topmost state. That's most the important bit.