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

30May/110

Saving/Loading with EasyStorage using XNA 3.1

This tutorial uses EasyStorage version (57440).

Trying to get saving and loading to work on a 360 can be a pain at first, but it's pretty easy as long as you set it up correctly. If you plan on releasing your game on the 360 you also have to make sure it doesn't crash while saving/loading if someone pulls out the memory card. I'm using Nick Gravelyn's EasyStorage which you'll have to download in order to follow along. I'm using a project that has the Platform Starter Kit combined with the Game State Management for this tutorial. You must have the Game State Management sample incorporated into your project in order to follow this tutorial. You could just use a blank copy of the GSM if you want to wanted to get this working, then try to move it into your own project.


 

Adding the files to your project

First off, we need to transfer all of the necessary files to our blank copy of the GSM (game state management). All you have to do is drag the EasyStorage folder (found at GS3.1 > EasyStorage in the download above) to the very base of your project. Your base folder should look like this after the move. NOTE:  I'm still using XNA 3.1 in this tutorial so don't let the name in the image through you for a loop (it's from the 4.0 tutorial).

EasyStorageFolder

 

Inside of Visual Studio we need to add the project and add a reference to it. In the solution explorer right click on the VERY TOP item (mine's named "Solution 'EasyStorage_3.1'"). Navigate to Add > Existing Project... > (find the location of the EasyStorage folder that you moved in the step above) EasyStorage > EasyStorage [Xbox].csproj. When you select that you should see an "EasyStorage [Xbox]" project alongside the others.

Let's add the reference now. In the solution explorer expand your main project ("EasyStorage_3.1" for me) and right click on the "References" folder. Click on Add Reference and when the popup opens click on the Projects tab at the top. You should see 'EasyStorage [Xbox]' and just add that. We can move onto setting up our game now.

NOTE: The EasyStorage folder also contains projects for windows and windows phone but we're just focusing on Xbox for this tutorial. If you're also making a windows or windows phone copy of the game you'll have to repeat the step above and add the specific projects and references.


 

Global.cs

Now right-click on the Saving folder and click Add>Class and name it 'Global.cs'. The purpose of this file is to have a set of global variables so we can access them from wherever we want in our code. It should look like the following but you need to change the namespace to match the namespace of YOUR game project. Also change fileName1 to the name of your game.


 

PressStartScreen.cs

Now we have the framework laid out and we just have to set our SaveDevice before we can use it. I have the following code on my 'Press start to begin' screen, when you press start, which should be a part of any game because you can also find out which controller is controlling everything. The struct above our Global class has the variables we'll need to save, replace them with anything that you'll want to save in your own game. You need to make a new class (preferably in the Screens folder) called PressStartScreen.cs and replace the contents of that with the class below. Make sure you change YOUR_NAMESPACE_HERE and YOUR_GAME_NAME.

 

Now that you have that in your game you'll need to go into Game.cs and find the line that says

and change it to

This will now start your game with a press start to begin screen and when you continue it prompts you to choose a storage device (if you have more then just the hard drive connected) and the main menu screen is created.


 

A few notes about the above code, because there's a lot going on that will confuse you if you don't understand it. If you're playing on the PC then your SaveDevice is chosen automatically and your save file will be saved in My Documents/SavedGames I believe. Otherwise it makes a call to PromptMe() which does all the heavy lifting of choosing a save device. If you have only the hard drive connected to your Xbox then it will seem like nothing happened because it chooses that automatically as well. If you have multiple storage devices plugged in (hard drive, memory card) then you will get a pop-up asking you to choose a storage device. Look at the 2 lines in the PromptMe method that end with SaveDeviceEventResponse.Force. This has 3 different arguments that you can use:

  • Nothing means nothing happens if you cancel the device selector screen. No save device will be chosen and if you try saving later you'll get errors.
  • Prompt means it will ask you to either choose a storage device, or continue without saving.
  • Force means that you MUST choose a save device in order to continue. No ifs, ands, or butts about it!

The first line (from the 2 options we're talking about) happens when you cancel the device selector. The 2nd line happens if you disconnect the storage device at some point after choosing it.

Finally, we set our Global.SaveDevice to the sharedSaveDevice so we have access to it later when we want to save/load.

NOTE: I'm using SharedSaveDevice in this example, so anything you save is accessible to anyone playing the game on your console. If you want to save data and attach it to a certain player, as most AAA games do, you'll want to look into PlayerSaveDevice instead, but it's a little bit more work.


 

Player.cs

So now onto saving. The Platform Starter Kit has a Player.cs class, and that's where I do my saving. We need to add the saving/loading methods. You'll also need to add a using statement: using System.IO;

 

Take note of the order you save stuff. Since the variable "lives" is the first to be saved, it MUST be the first to be loaded!

So here's our load methods which look much the same as SaveGame() but instead of writing to a file, we're reading from it.

 

And that's everything! When we want to save our game we make a call to SaveGame() in our Player.cs class (or whatever you used) and when I load up my game I call LoadGame() in my LoadContent() method.

I know this can be pretty confusing at first, but once you finally get it, it'll be smooth sailing from there on!

30May/1114

Saving/Loading using EasyStorage with XNA 4.0

This tutorial is made with XNA version 4.0. If you're still using 3.1 you can use this tutorial (which is a little outdated (Nov '09) but it should work).

Trying to get saving and loading to work on a 360 can be a pain at first, but it's pretty easy as long as you set it up correctly. I'm using Nick Gravelyn's EasyStorage which you'll have to download in order to follow along (I'm using changeset 57440 for this tutorial so grab that version for best compatibility). I'm going to create a mock-up game using the Xbox 360 Game State Management (4.0 version) to try and make this more accessible to everyone instead of using the Platformer Starter Kit like in the previous tutorial. You should be able to rip all of the saving/loading code straight from here and drop it into your own game.A good suggestion if you're on the newer side is to go through this tutorial and get it working in a blank copy of the Xbox 360 Game State Management example BEFORE you try to just drop it into your game. Once you get it working you'll get a little confidence boost and you'll have some understanding of how it works.


 

Adding the files to your project

First off, we need to transfer all of the necessary files to our blank copy of the GSM (game state management). All you have to do is drag the EasyStorage folder (found at GS4 > EasyStorage in the download above) to the very base of your project. Your base folder should look like this after the move. I named the blank copy of the Xbox 360 GSM "EasyStorage_4.0" so whatever you named your project will show up there instead.

EasyStorageFolder

Inside of Visual Studio we need to add the project and add a reference to it. In the solution explorer right click on the VERY TOP item (mine's named "Solution 'EasyStorage_4.0'"). Navigate to Add > Existing Project... > (find the location of the EasyStorage folder that you moved in the step above) EasyStorage > EasyStorage [Xbox].csproj. When you select that you should see a "EasyStorage [Xbox]" project along side the others.

Let's add the reference now. In the solution explorer expand your main project ("EasyStorage_4.0" for me) and right click on the "References" folder. Click on Add Reference and when the popup opens click on the Projects tab at the top. You should see 'EasyStorage [Xbox]' and just add that. We can move onto setting up our game now.

NOTE: The EasyStorage folder also contains projects for windows and windows phone but we're just focusing on Xbox for this tutorial. If you're also making a windows or windows phone copy of the game you'll have to repeat the step above and add the specific projects and references.


 

Global.cs

I like to create a Global.cs class and store the save device in there so I can save/load wherever in my code I want. Right click on your main project and Add > Class. Name it Global.cs and replace everything in the file with the code below:

 

The comments should explain what each value does. The basic gist is that you should only have one container but you can have multiple files within that save container which allows your code to be much more organized.


 

PressStartScreen.cs

Now we have the framework laid out and we just have to set our SaveDevice before we can use it. I have the following code in my 'Press start to begin' screen which should be a part of any game because you can also find out which controller is controlling everything. Nick Gravelyn has an awesome example of how to support multiple controllers here. You need to make a new class in your Screens folder called "PressStartScreen.cs" and replace the contents of that with the class below.

 

Once you have this new file in your game you'll need to go into Game.cs and find the line that says

and change it to

 

Now when your game starts you'll be presented with a 'Press Start to begin' screen. When you press A or Start it prompts you to choose a storage device and the main menu screen is created. NOTE!! If you only have ONE storage device connected (Ex: you have a hard drive and no memory cards) then you won't get a popup at all, it will automatically choose the storage device found in the background (unaware to you).

A few notes about the above code, because there's a lot going on that will confuse you if you don't understand it. When you hit Start or A it calls the "PromptMe()" method which handles setting up the save device. If you have only the hard drive connected to your Xbox then it will seem like nothing happened because it chooses that automatically. If you have multiple storage devices plugged in (hard drive, memory card) then you will get a pop-up asking you to choose a storage device. Look at the 2 lines in the PromptMe method that end with SaveDeviceEventResponse.Force. SaveDeviceEventResponse has 3 different arguments that you can use:

  • Nothing means nothing happens if you cancel the device selector screen. No save device will be chosen and you won't be able to save anything later on.
  • Prompt means it will ask you to either choose a storage device, or continue without saving.
  • Force means that you MUST choose a save device in order to continue. No ifs, ands or butts about it!

If you chose Prompt or Force and you remove the memory card (if that's the storage device you chose) you'll get a popup saying something along the lines of "reselect your storage device." If you chose Nothing and remove the memory card your game may crash when it tries to save again.

 

Finally, when we select the save device (sharedSaveDevice.DeviceSelected) we assign Global.SaveDevice to the save device so we have access to it later when we want to save/load. We also load the main menu when they choose a device.ANOTHER NOTE! If you change the SaveDeviceEventResponse to Prompt or Nothing and you don't choose a save device the menu won't load. This is because we have the code for loading the main menu inside the block of code for when our save device gets selected. You'll have to modify this if you want to allow users to continue w/o a save device.

 

MORE NOTES: I've always used SharedSaveDevice which means 1 save per Xbox. If you want different gamertags to have their own save games you'll have to use a PlayerSaveDevice instead of SharedSaveDevice.


 

OptionsMenuScreen.cs

If you're working in a copy of the Xbox 360 Game State Management then you should have a file called "OptionsMenuScreen.cs" in your Screens folder. Open that up because we're going to change it up so you can save your game options. I'm just going to use the default options they give us but in reality you might have options in here to change the volume of your sounds or select some preferences.

 

Inside of the constructor (public OptionsMenuScreen()...) method let's add this block of code to the VERY bottom (we can also remove the FIRST "SetMenuEntryText();" call since we call it after we try loading previous values):

 

You'll also need to add "using System.IO;" to the top.

 

Since everything that we're trying to read is a string we need to convert it to the same type as the variable we're setting it to. That's where values like "int.Parse(...)" and "bool.Parse(...)" come from. The first one converts the string to an integer while bool.Parse(...) converts the string to a bool.

 

Now that we can load values, we need to add our save functionality! We're going to save when the user backs out of this screen (by pressing B or pressing the Back button) so we need to override the OnCancel method. Add this in the Handle Input region if you still have it, or just add it underneath the constructor.

 

NOTE: I didn't deal with saving/loading the "Ungulate" variable so that will also default to the initial value when you try it.


 

And that's everything! Here's a couple suggestions to keep everything organized.

  • When saving/loading stuff we're writing/reading from a file. The first value you write MUST be the first value you read. When writing this I accidentally wrote 4 lines to the file (saving) and when I tried reading them (loading) the order was messed up. I had to delete my save file and fix the order to get it to work again.
  • When you move to saving game data make a new file name in the Global.cs file (I have a couple examples commented out in there) and just move the saving/loading examples in OptionsMenuScreen.cs into the file where you're working with your game code. All you'll have to do is change "Global.fileName_options" in the save/load arguments to your new name and change what you're reading/writing.

 

I know this can be pretty confusing at first, but once you finally get it, it'll be smooth sailing from there on!

 

30May/110

Adding Lives to PSK

Find your main file (PlatformerGame.cs is default, GameplayScreen.cs if you're using GameStateManagement)

You should already have a line that reads, “Texture2D loseOverlay” and we’ll be using that for this tutorial.

 

PLAYER.CS

Somewhere near the top near the other variable declarations, add this line.

 

In the OnKilled(…) method add the following at the very bottom:

 

PLATFORMERGAME.CS

In the DrawHud(…) method find this statement:

 

And change it to this:

 

And there you have it, you now have the most basic life system you can incorporate. Everytime you die your life meter will decrement by one and when your lives are < 1 the loseOverlay will show instead of diedOverlay.

Using this combined with the GameStateManagement example, you can have a gameover screen show when you lose all your lives, and kick the player back to the main menu or whatever you’d like.

30May/110

Adding Checkpoints to PSK

Here's a video showing off what the checkpoint system should look like after this tutorial:

Here's the basic rundown of how I set this up. First, we'll add another tile to our list so we can add checkpoints through our text document levels. Second, we'll set up how the checkpoints should work. The most recent checkpoint you've touched will be your current checkpoint. If you know how the Sonic games have checkpoints it's very much like that. When you die you'll be returned to your most recent checkpoint.

In Tile.cs add the following in enum TileCollision:

 

In Level.cs After this line:

Add this:

 

Now find the following:

And change it to this:

 

In ‘private Tile LoadTile(...)’ add this near the others.

 

Now in Player.cs find the following:

And add this after it:

 

Now make sure you add some 'P's to your levels so you can try out the checkpoints. Currently they just use the BlockA0 texture so you'll have to add your own to the Tiles folder and change the case 'P': ... texture argument to match your own checkpoint texture.

30May/110

Saving where your player left off on PC

Save/Load feature in Platform Starter Kit. (Saves the player's position, so you can start where you left off) (NOTE: works with PC only)
UPDATE: You can find a tutorial for saving/loading on the 360 here: XNA 3.1 | XNA 4.0

In this tutorial I'm going to create a hotkey (S) that saves the player's current position into a text file. You can then use this text file when loading a level to have your player start where you left off. You could even combine this with the checkpoint tutorial, so if you save and quit you'll load at the last checkpoint you hit. Splicing things together is an awesome way to learn (for me at least) so I'll leave that to you.
At the top of Player.cs we need to add the following using statements:

 

In Player.cs add the following after Vector2 position:

 

Now in the Player constructor, add the following if/else statement, overwritting the current Reset method:

 

Now in the GetInput method we'll add a hotkey to save our game:

 

At the very bottom, I added the next couple methods, right after Draw().