Serializing to XML
by Doug Barnes · in Torque X 2D · 03/13/2008 (3:08 pm) · 0 replies
Hey I trying to write a save game system, and my idea is to write a lot of data to an xml then loading from the xml for a load game. Anyway I'm having some problems writing an array of T2DAnimatedSprites to xml, I can get everything else to work except for the array. heres the code
public struct SaveStruct
{
public string playerFaction;
public int playerCurrency;
public ArrayList units;
}
public void Save(ArrayList saveUnits,string playerFaction,int playerCurrency,string saveName)
{
SaveStruct gameSave = new SaveStruct();
gameSave.playerCurrency = playerCurrency;
gameSave.playerFaction = playerFaction;
gameSave.units = saveUnits;
Type[] extraTypes = new Type[4];
extraTypes[0] = typeof(String);
extraTypes[1] = typeof(int);
extraTypes[2] = typeof(ArrayList);
extraTypes[3] = typeof(T2DAnimatedSprite);
StorageDevice device = StorageDevice.ShowStorageDeviceGuide();
//open storage container
StorageContainer container = device.OpenContainer("RTS");
//get the path of the save game
string filename = Path.Combine(container.Path, saveName+".xml");
//creates a new file
FileStream stream = File.Create(filename);
//convert to xml data and put into stream
XmlSerializer serializer = new XmlSerializer(typeof(SaveStruct));
serializer.Serialize(stream, gameSave);
stream.Close();
container.Dispose();
}About the author