TorqueX/PlatformerFrameworkPause

From TDN

Contents

Introduction

One important feature in any game is pausing. Pausing allows players to take a break when nature calls or the pizza delivery guy is at the door and then get back into the action when they are good and ready. Despite the fact that pausing is so common, there isn't any real documentation in this area. Fear not though, I've spent a few hours tinkering around in this area and have compiled a handy guide to pausing your game.

Materials Needed

  • Visual C# Express
  • XNA
  • Torque X (TX)
  • Platformer Starter Kit (PSK)
  • A new project using the Platformer Demo

Adding the Pause Logic

To add a pause feature to our game we need to edit two files, Game.cs and PlayerController.cs. All of the pause logic will go in Game.cs so we'll start there.

The first thing we'll need is a field to track whether the game is paused or not. In the Private, protected, internal fields region of Game.cs add:

// Paused flag.
private bool _paused = false;

We will used _paused as a flag to determine if the game is paused or not. It is set to false by default since we don't want our game to start in a paused state. The next step is to setup a method to change the pause flag. Add the following to the Public methods region:

public void TogglePause()
{
   // Toggle _paused
   _paused = !_paused;

   if (_paused)
   {
      Game.Instance.Engine.GameTimeScale = 0.0f;
   }
   else
   {
      Game.Instance.Engine.GameTimeScale = 1.0f;
   }
}

TogglePause first toggles the value of _paused; true becomes false and false becomes true. After that it checks the value of _paused and sets GameTimeScale accordingly. GameTimeScale is a property that will, according to the Torque X API Help, "change the rate that time passes for game entities and scheduled events on the GameTimeSchedule." Setting GameTimeScale to 0 pauses the game and setting it to 1.0 resumes the game. In TogglePause, if _paused is true GameTimeScale is set to 0; if _paused is false GameTimeScale is set to 1.0. We want TogglePause to be a public method so our other game components can call it.

That's all the logic required to pause our game.

Hooking up the Control

Now we need to setup a way for players to pause our game. It's standard convention for a controller's start button to pause the game and bring up a pause menu so we'll bind our TogglePause method to the start button. Open up PlayerController.cs and add the following line to the _setupInputMap method:

// Bind the start button to TogglePause
inputMap.BindCommand(gamepadId, (int)XGamePadDevice.GamePadObjects.Start, null, Game.Instance.TogglePause);

With this line added anytime a player presses the start button TogglePause will be called. And that's that, we can now pause our game.

Taking it a Step Further with the LIVE Guide

If you're developing your game for Xbox LIVE Community Games you are probably going to make use Xbox Guide which means you'll also need to pause the game when the Guide becomes visible. Fortunately this can be accomplished quite easily.

The first step is to add a GamerServicesComponent to your game in Game.cs. In the Private, protected, internal fiels region add:

GamerServicesComponent _gamerServicesComponent;

Then, in the constructor, initialize _gamerServicesComponent and add it to the game:

_gamerServicesComponent = new GamerServicesComponent(this);
base.Components.Add(_gamerServciesComponent);

That code adds Gamer Services to your game. Gamer Services provide lots of cool things, but for this article we are only interested in the Guide. If you debug the game you'll now find that you can bring up the LIVE Guide by pressing the Guide button on your gamepad. You'll also notice that your game does not pause when you do this.

To get the game to pause when the Guide is visible we need to know when the guide is visible. We can find this out by checking a property of the Guide, IsVisible. Scroll to the Private, protected and internal methods region of Game.cs and add the following block:

if (Guide.IsVisible)
{
   _paused = true;
   Game.Instance.Engine.GameTimeScale = 0.0f;
}

This if block checks the Guide's IsVisible property which returns true when the Guide is ... you guessed it ... visible. When the Guide is visible we set our _paused flag to true (we want the game to be paused) and then set GameTimeScale to 0. Now if you debug the game you will find that it pauses when you bring up the Guide. What you will also find is that it does not unpause when you hide the Guide.

Why not unpause the game when the Guide is hidden? Convention with Xbox games has been that the game pauses when the Guide button is pressed and then remain paused until the player unpauses via one of the game's menus (which are usually brought up along with the Guide). You could resume your game when the Guide is hidden, but bucking convention usually leads to frustrated gamers because they aren't getting what they expect.

Further Reading


Credits

Original article and code by Sean Monahan