Robot Foot Games Helping you improve your games as we improve our own. :)

22May/115

WP7 Saving/Loading Tutorial

For those that don't know about Easy Storage here's the description from the codeplex project "EasyStorage is a library for making it quicker and easier for XNA Game Studio developers to manage the StorageDevice without all the details. EasyStorage uses a simple API to enable you to add file saving to your game."

Nick Gravelyn updated it to support XNA 4.0/Windows Phone 7 and since I've used it with both my games for the 360 I figured I'd check it out for my WP7 game. It's pretty simple to set up on the 360, and I wrote a tutorial on how to do that, and SUPER simple to use on WP7.

First off, download the latest build (changeset 57440). It comes with windows/360/WP7 stuff and if you plan on supporting any multiple of those platforms then you'll have to look through the included sample to see how they get setup differently. For the purpose of this tutorial we'll just port the WP7 code into out project. From the easystorage-57440 folder go into GS4 and copy the EasyStorage folder in there into the very base of your project. The base folder should look like this after you copy the folder in.

wp7_easystorage

Now we have to add the project and a reference to it. From the Solution Explorer in Visual Studio right click on the very top item (should be called "Solution ...") and Add > Existing Project. Navigate inside the EasyStorage folder you just copied into your project from the step above. Inside of that folder should be a file called "EasyStorage [Phone].csproj" and click open. Now you should see the EasyStorage [Phone] project along side your content project and the main project.

Now we need to add a reference to this project we just added in our main project. Expand your main project in the solution explorer, right click on the References folder and click Add Reference. Click on the "Projects" tab at the top and select the EasyStorage [Phone] file. Done!

NOTE: You'll notice that there's also project files in here for Xbox and Windows. If you plan on making a copy for either of those just follow the 2 steps above to add the specific project and reference.


 

Global.cs

I like to create a class called Global.cs that I can access anywhere so I can save/load where I need to. Right click on the main project and Add > Class. Name it "Global.cs." You can erase everything in there and replace it with this code. The comments should explain it pretty well.


 

Game.cs

Now we just need to setup our save device. In the constructor of your game, at the very bottom, add this code:

 

Here you can set your supported languages and we also set our isolated storage device to our global counterpart so we can access it if needed. You'll notice a bunch of cross platform code in here so if you don't have a need for it feel free to remove it. You'll notice a SaveCompleted event so if you want to let your users know if the game saved correctly you could do it in there. Here's the method for that:


 

When you want to save something you can simply use this block of code:

 

We want to make sure the save device is ready for a save before committing to it. You can query Global.SaveDevice.IsBusy if you want to know when your game is saving something (useful for a saving animation). Make sure the order of stuff you're writing to the save device because it MUST be read in that exact same order.

 

Now when you want to load something you can simply use this block of code:

 

Here we're checking that a fileName_options file exists and if it does we load the data inside. Since reader.ReadLine() returns a string you may need to cast it to a different type. There's a couple examples above (int.Parse and bool.Parse).

And that's everything! If you want to save some options (like music or sound effect volume) you could pass fileName_options as an argument while saving/loading. When you want to save some in-game stats you can pass in fileName_gameStats or whatever you'd like to. You should be able to use just the one container, but split up your save files based on what/where you're saving.

Comments (5) Trackbacks (0)
  1. Hello,
    I did everything exactly as you said to in this tutorial but I keep getting an null pointer error for this line:

    // make sure the device is ready
    if (Global.SaveDevice.IsReady)<<<<
    {

    The cause is this line:
    public static IAsyncSaveDevice SaveDevice;

    I've been trying figure why it's not working but I haven't been able to fix it.

    • It sounds like your ‘SaveDevice’ isn’t getting properly set then. Try putting a break point to make sure the following lines are getting hit (in the Game.cs constructor):
      saveDevice = new IsolatedStorageSaveDevice();
      Global.SaveDevice = saveDevice;

  2. I would like to use easy storage to save a custom class “Level”. My class has several references to other custom classes like enemies. I believe I understand how Easy Storage works to save common varable types, but I’m not sure how to save/load a custom class. Any tips would be greatly appreciated.

    This line might not be doing anything, but Visual Studio doesn’t get mad at me for writing it.

    writer.WriteLine(currentLevel);

    However this line is not acceptable to Visual Studio.

    savedLevel = reader.ReadLine();

    “Cannot implicitly convert type ‘string’ to ‘ProjectName.Level’.

    I’m not sure if I should continue to pursue using Easy Storage or research some kind of XML serialization.

    • You should definitely look into XML to serialize an entire class. I learned the basics of XML from Nick Gravelyn’s Tile Engine videos so that might be a good place to check.


Leave a comment

(required)

No trackbacks yet.