Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to implement pausing by following this tutorial. The tutorial recommends essentially this as the implementation:

if (!paused) {
    Simulate(gameTime);
}

How does this work? What does Simulate mean here?

share|improve this question
    
So I just had a look at the downloadable source code and it seems that Simulate is a method thats being called. Is there a way to code "continue game like normal" instead of this simulate? –  Tom Apr 25 '11 at 14:32

2 Answers 2

up vote 12 down vote accepted

It's just a simple way to wrap your entire Update method in a conditional, without mixing the pausing logic with your game's updating logic.

So instead of:

protected override void Update(GameTime gameTime)
{
    if(!paused)
    {
        // Lots and lots of code!
    }
}

You can have:

private void Simulate(GameTime gameTime)
{
    // Lots and lots of code!
}

protected override void Update(GameTime gameTime)
{
    if(!paused)
    {
        Simulate(gameTime);
    }
}

Of course, if you would prefer to express it as "continue the game like normal", you could also do this:

protected override void Update(GameTime gameTime)
{
    if(paused)
        return;

    // Lots and lots of code!
}

These all do the same thing. Which method you choose to do it is simply a matter of taste. Basically what the tutorial is telling you is:

  • Determine if your game should be paused and store that in a variable (ie: bool paused)
  • If it is paused, skip over the code that is responsible for updating your game's state (essentially freezing it in time, ie: "pausing" it.)
share|improve this answer
    
+1, great answer, nicely explained –  Jesse Emond Apr 25 '11 at 14:47
    
"By lots of code!" do you mean other code thats in the update method or pausing code? –  Tom Apr 25 '11 at 15:17
    
Ok I understand what you mean now, but because my game has different gamestates and various methods to update the game, then I think the example in the tutorial I was using is a bit basic perhaps? To be honest I only want a simple method that evaluates whether a button is pressed to allow the game to puse so I would appreciate it if someone could write me one so I know what to do in future :) –  Tom Apr 25 '11 at 15:34
2  
Just dont skip the code that checks for leaving paused state... –  Nate Apr 25 '11 at 15:48
1  
@Tom If you are using GameComponents, you want to set GameComponent.Enabled = false on each of them when your game pauses. You may also get away with simply skipping the call to base.Update(), which is what calls Update on each GameComponent. Having a "Pause Button" is already described in the tutorial you linked. –  Andrew Russell Apr 25 '11 at 15:50

As Andrew mentioned, you can simply add a flag in the Update loop to check the game's state. However, if your game is a bit more complex, you might want to take a look at the Game State Management code sample, that demonstrates multiple screens, transitions and a pause menu.

share|improve this answer
    
My game has three gamestates and different methods for updating the game. I'm guessing from "lots of code" Andrew means all the code that the game runs to update the game? –  Tom Apr 25 '11 at 15:28
    
Yup, that's what I understand it to be. It's the non-pause related part of the game (i.e. everything else :) ) –  keyboardP Apr 25 '11 at 15:40
    
+1 I was going to link this too. The idea is that components belong to a screen, and it's the screen's responsibility to update those objects. Pausing just means pulling up a screen that blocks all those behind it from being updated. There is no need to fiddle with each component's individual on/off state, and there's no danger of locking yourself out if you know your pause screen has a resume button. –  doppelgreener Apr 25 '11 at 16:28
    
I just entered some update code (My snake character which moves on its own) into the "if(!paused)" method which is supposed just allow the game to continue as long as the pause button isnt pressed - When I played the game it turned out that the pause button altered the speed of the snake and didnt actually pause it which has confused me. Has anyone gopt any suggestions? –  Tom Apr 25 '11 at 16:52
1  
@Tom: Are you in fact toggling pause on and off constantly? –  doppelgreener Apr 25 '11 at 17:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.