Windows Phone Games: Exploring the State Management project, “Go To Definition”

 

When you design games, especially for the phone, you will need to manage state, for instances, the user may want to halt a game, or they get a phone call and expect to come back to the game at the place they left it.  This is referred to by some people as “state” management, you might think of it using other terms, but basically for a game to be fun, there has to be a way for the game to be stopped by the user, saved by the user, and on the phone, tombstone’d.

Tombstoning occurs when the user gets a phone call or wants to move to another application such as the camera to take a picture.  You as a programmer need to make sure that there is a way for the game to save the state or near state where the end user leaves the game.

One of the better projects, is the Windows Phone Game State Management.  The code is written in manner that you can use the debugging tools in Visual Studio to help you understand how to use the software, it will take a bit of exploration to determine how to best use it for your game.

To get started with the AppHub State Management example, go to the following link:

https://create.msdn.com/en-US/education/catalog/sample/game_state_management

The first class in the project is Game.cs, it is not organized like the default game.cs you might created from the XNA Template.  The Game State management project is another way that you can create games.  If you look through the code in Game.CS, the initialize and update method are not included.  The assumption is that the screen manager will call drawable components or game components that will supply that functionality.

Reading the code can be tricky, for instance in Game.CS what is the function of the line:

ScreenManager screenManager;

If you place your cursor over the ScreenManager (the one with the capital S) and press F12 or right click and select “Go To Definition”:

image

The IDE will take you to the definition for ScreenManager:

public class ScreenManager : DrawableGameComponent

This means that the ScreenManager is a class based on the DrawableGameComponent and contains the methods you are used to seeing in the default template that builds a Game.CS class with Initialize, LoadContent, UnloadContent, Update and Draw.  In this case the ScreenManager class provides all of these methods, there properties and fields (or variables) that were added by the creator of the State Management project.

Using the “Go To Definition” is one of the the ways that professionals use to find out what is going on in code that they didn’t design.  There are other tools in the Visual Studio tools that you have to pay for, in Express this is one I use a great deal.  The AppHub workers are doing their best to get the code into good shape, but even the best documented code often requires that you do exploration on it.

Have FUN!