Mango’s new GameStateManagement

With the beta release of the  Windows Phone Developer Tools for Mango, an updated GameStateManagement sample has been released: http://create.msdn.com/en-US/education/catalog/sample/game_state_management

What exactly has been updated?

1. Lifecycle events taken from Microsoft.Phone assembly (part of the Silverlight assemblies), also supporting the reactivation of ‘preserved game instances’
2. Introduction of the IScreenFactory interface
3.  Introduction of the InputAction class

1. Lifecycle events taken from Microsoft.Phone assembly

The original GameStateManagement sample used the Game’s constructor to deserialize the peristed game state. By overriding the OnExiting method it serialized the game state.

In the Mango release this has been changed to use the following events:

// Hook events on the PhoneApplicationService so we're notified of the application's life cycle
Microsoft.Phone.Shell.PhoneApplicationService.Current.Launching +=
    new EventHandler<Microsoft.Phone.Shell.LaunchingEventArgs>(GameLaunching);
Microsoft.Phone.Shell.PhoneApplicationService.Current.Activated +=
    new EventHandler<Microsoft.Phone.Shell.ActivatedEventArgs>(GameActivated);
Microsoft.Phone.Shell.PhoneApplicationService.Current.Deactivated +=
    new EventHandler<Microsoft.Phone.Shell.DeactivatedEventArgs>(GameDeactivated);

The Launching event is called when the application is started by tapping the icon for the application from the installed programs list or by tapping the tile for the application on the device’s Start screen.

The Actived event is called when the user returns to your application. The application can be either tombstoned (serialized state) or dormant. The dormant state is new (fast app switching) and in this case the ActivedEventArgs.IsApplicationInstancePreserved will be set to True.

The Deactived event is called when the user navigates away from your application and offers the possibility to tombstone your application.

For more info on the the execution model of WP7 apps see this link: http://msdn.microsoft.com/en-us/library/ff817008(v=VS.92).aspx.

2. Introduction of the IScreenFactory interface

In the original GameStateManagement sample, it was required that all GameScreen constructors were parameterless as it uses Activator.CreateInstance(screenType).

This restriction has been lifted by introducting an IScreenFactory interface. The interface defines a method CreateScreen, which is called with a parameter Type screenType.

By implementing the public GameScreen CreateScreen(Type screenType) method, you can choose to fetch additional parameters from Isolated storage or the State dictionary and use them to instatiate your game screens.
If all your GameScreens have default constructors you can implement the interface as followed:

public GameScreen CreateScreen(Type screenType)
{
    // All of our screens have empty constructors so we can just use Activator
    return Activator.CreateInstance(screenType) as GameScreen;
}

See the GameStateManagementSample’s ScreenFactory implementation for more details.

3.  Introduction of the InputAction class

Here is the verbatim quote of the class’ summary:

 Defines an action that is designated by some set of buttons and/or keys.

 The way actions work is that you define a set of buttons and keys that trigger the action. You can
 then evaluate the action against an InputState which will test to see if any of the buttons or keys
 are pressed by a player. You can also set a flag that indicates if the action only occurs once when
 the buttons/keys are first pressed or whether the action should occur each frame.

 Using this InputAction class means that you can configure new actions based on keys and buttons
 without having to directly modify the InputState type. This means more customization by your games
 without having to change the core classes of Game State Management.

For WP7 apps this can be used to react on the Back button.

For example:


InputAction menuCancel = new InputAction(new Buttons[] { Buttons.Back }, null, true);

...
if (menuCancel.Evaluate(input, ControllingPlayer, out player))
{
    //executecancel code
}

Follow me on twitter? Sure! http://www.twitter.com/jodegreef

Advertisements

6 Responses to Mango’s new GameStateManagement

  1. Pingback: Anonymous

  2. Hasan says:

    That was quick 🙂 The update was posted yesterday.

  3. Pingback: Dew Drop – May 25, 2011 | Alvin Ashcraft's Morning Dew

  4. Pingback: Mango’s new GameStateManagement – www.nalli.net

  5. hades32 says:

    You can really handle the START button? I.e. the WINDOWS button?!
    I don’t think this can be right… You can’t even handle the search button…

  6. jodegreef says:

    you’re right, only the Back button.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: