Silverlight Game Programming Tutorial - State Management
In this article we will take a look at state management. The term “state management” can have two interpretations: one is managing the transitions between application states (for example main menu to game play to high score screen), and the other is the persistence of a state information across application executions. Even though the two terms don’t have much in common technically, we will cover both here.
The first concept we will explore is the persistence of state information between the executions of the Silverlight application. In a traditional web based application the state of the user session is either stored on the server (in a session state), or by way of cookies on the client. Both systems are used to compensate for the inherently stateless nature of a HTTP transaction. Silverlight is different in that all execution takes place on the client. In fact the Silverlight application doesn’t even need a server: it can be embedded in a local HTML document with no network access at all. In this respect a Silverlight application is more like a desktop application than a traditional web app. So without a server to save data onto how does a Silverlight save non-volatile data?
Silverlight has access to an IsolatedStorage class, which gives the developer the ability to save data that persists after the application has been terminated. This information is traditionally saved to the local hard disk, but the reality is that as developers we don’t need to know or care how the data has been saved. All we need to know that, if IsolatedStorage has not been disabled, we can save data that will be available next time the application is run.
We will make use of this ability to store the players score between games. The functionality will be implemented in the ApplicationManager SavedScore property.
ApplicationManager.cs C# Silverlight source code
Traditional .net developers will notice that this code looks very similar to retrieving data from an app.config file. IsolatedStorageSettings.ApplicationSettings presents a key/value dictionary that we can use to load and save persistent data. It’s worth noting that you can not assume that the IsolatedStorage contains your data, as the end user can delete the contents of the isolated storage at any time. This is why we do so many checks before returning any data.
It’s also not recommended that you store any sensitive data using IsolatedStorage. This is because, while not easily accessible, it can be modified or hacked by the end user. If the players current score was used as part of a global ranking system it’s entirely possible that a clever user could modify their local IsolatedStorage data to give themselves an inflated score. Encrypting the data, or sending it to a server you trust, would be the best solution for any sensitive data.
Despite these few caveats the IsolatedStorage class makes saving and retrieving persistent data quite simple.
The second aspect to state management is the transition from one logical state to another. In the Flex version of the game we made use of the built in state management functionality to have two functions called with every state: one when the state was entered, and another when it was left. By placing the initialisation and cleanup code in these functions we could transition between logical states. Silverlight doesn’t have the same ability built in, so we will create a class called StateManager to do the job for us.
StateManager.cs C# Silverlight source code
The idea is to maintain a collection of delegate pairs; one that is called when entering a state and one when it is exited. Some extra code has been written to ensure that if a state change is made during another state change, that the pairs of functions are called in a logical way i.e. an exit state function will always be called after it’s accompanying enter state function. Apart from that all we need to do is register delegate pairs to a state and then make a call to setState when we want to change the current state.
We make use of the state management to introduce a main menu which we can drop back to by pressing the escape key. Although it’s a simple touch, being able to manage states will allow us to easily move from the main menu to the game, and maybe then to a high score screen or an end of level video clip, and back to the main menu.
Check out the online demo, and download the source code from the Sourceforge SVN repository, or download an archive containing all the source code here.
Back to Silverlight Game Programming Tutorials
What You Need
Create a Flash platform game with Flixel and Adobe Flex
This tutorial series steps you through the process of creating a simple platform game using the Flixel game engine and Adobe Flex.
This post is part of the series: Silverlight game development
A tutorial series showing you how to create a 2D game with Silverlight 2.