XNA Storage – The Beginning

Sunday, May 06 2007 - ,

With some of the projects that I am working I am going to have to be able to store player and game data, so with this in mind I thought that it was time I had a look at the storage systems that are included in the XNA Framework. The first stage in this project was to make sure that I had had a good read of the Documentation files for the Storage System that Shipped with the XNA Framework.

The first stage in this project is to start up a default Windows Games Project. I am going to concentrate most of this article on the Windows System and will add some more content in a future article for the Xbox 360 system.

When you do have the Default Windows Game up and running we need to make sure that we add some more Assembly references to the project and the Using statements that make them work. Add the following assembly to the project by right clicking on the solutions name in the Solution explorer and selecting the Add Reference option.

Add this Assembly to the Project "System.XML".

The Next stage is to add another class file to the solution, Do this by right clicking on the solution name selecting Add and then class. As I would like to separate the Storage Systems for the project so that it can be used by several different systems in my project call the class file "StorageSystem.cs". When complete we need to add the using statements for the project to this class file. For the moment I am going to add all of the XNA Systems, File IO and XML systems, towards the end of this project I will remove the statements that I am not going to need in this class file. Add the following using statements to the project.

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
// Note: Before you can build this file in a project, you must
// add a reference to System.xml. To to this, right-click
// on References in Solution Explorer and click Add Reference.
// In the .NET pane, scroll down and select System.Xml.
// Additional Using Statments Needed for the Storage Functions.
using System.IO;
using System.Xml.Serialization;
#endregion

The next stage is to set up the Structure that we are going to use to save the player data, Add the following to the code just inside the declaration for the Storage System Class.

[Serializable]
public struct PlayerData
{
   public string name;
   public Vector2 position;
}

The next stage is to create the Save Player Data functions.

public void SavePlayerData(PlayerData playerData, string containerName, string fileName)
{
   StorageDevice device = StorageDevice.ShowStorageDeviceGuide();
   // Open a storage container
   StorageContainer container = device.OpenContainer(containerName);
   // Get the path of the save game
   string tempFileName = Path.Combine(container.Path, fileName);
   // Open the file, creating it if necessary
   FileStream stream = File.Open(tempFileName, FileMode.OpenOrCreate);
   // Convert the object to XML data and put it in the stream
   XmlSerializer serializer = new XmlSerializer(typeof(PlayerData));
   serializer.Serialize(stream, playerData);
   // Close the file
   stream.Close();
   // Dispose the container, to commit changes
   container.Dispose();
}

And Finally the Load Player Data Functions.

public PlayerData LoadPlayerData(string containerName, string fileName)
{
   PlayerData playerData = new PlayerData();
   StorageDevice device = StorageDevice.ShowStorageDeviceGuide();
   // Open a storage container
   StorageContainer container = device.OpenContainer(containerName);
   // Get the path of the save game
   string tempFileName = Path.Combine(container.Path, fileName);
   // Check to see if the save exists
   if (!File.Exists(tempFileName))
      // Notify the user there is no save
      return playerData;
   // Open the file
   FileStream stream = File.Open(tempFileName, FileMode.OpenOrCreate, FileAccess.Read);
   // Read the data from the file
   XmlSerializer serializer = new XmlSerializer(typeof(PlayerData));
   playerData = (PlayerData)serializer.Deserialize(stream);
   // Close the file
   stream.Close();
   // Dispose the container
   container.Dispose();
   return playerData;
}

The Final Stage is to Test the Code that we have put together, to do this jump back into the main game class and replace the initialization class so that it looks like this. Note that this code is only to test the routines that are in the storage system class.

protected override void Initialize()
{
   // TODO: Add your initialization logic here
   base.Initialize();
   StorageSystem storageSystem = new StorageSystem();
   StorageSystem.PlayerData playerData = new StorageSystem.PlayerData();
   StorageSystem.PlayerData loadPlayerData = new StorageSystem.PlayerData();
   playerData.name = "Mykre";
   playerData.position = new Vector2(100, 100);
   storageSystem.SavePlayerData(playerData, "StorageExample", "SaveData.vrd");
   loadPlayerData = storageSystem.LoadPlayerData("StorageExample","SaveData.vrd");
   Console.WriteLine("Player Name : {0}", loadPlayerData.name);
   Console.WriteLine("Player Position : {0},{1}",
      loadPlayerData.position.X.ToString(), 
     
loadPlayerData.position.Y.ToString());
}

This is just the beginning with the storage system you are able to save game data related to the users profile and or data related to the games Title. On the windows machine the Game Title data would be saved in the root of the games main directory, this data could be the map or game related configuration data. Also the Player data gets saved in the my documents directory under Saved Games in a directory which matches the Container name. Inside this directory the default is for the data to be stored under an All Players Directory, In future articles I will adjust this code so that it will be able to save the data in a directory that will match the player index. This will allow you to save different sets of data for each of the player controllers on the system.

kick it on DotNetKicks.com kick it on gamedevkicks.com

Similar Posts

  1. Phoenix, Drawing the Main Background for the Project
  2. Getting Started with Blender 3D and XNA
  3. Phoenix, Drawing Text on the Screen
Clicky Web Analytics