A good way to store game settings / saves

Started by
5 comments, last by rsmccli 15 years, 1 month ago
I am about to begin a small project game to sharpen my C# skills. I was wondering what would be a good way to save settings or perhaps game saves. I am talking about stuff that wouldn't be stored in memory while the program is running. I was thinking you could probably do this with XML, but I would like to know if there is a better format than this, or even just some other options that I am not aware of. Thanks.
Advertisement
XML is easy, mainly because there's a number of parsers available for it, and writing it isn't too hard either. Compared to binary formats however (which are usually somewhat trickier to set up), they often contain much more overhead and are usually slower to parse.

For some game settings, XML is a fair choice. For savegame data, depending on exactly what kind of data you want to save, it's probably not the best choice. On a side-note, you may want to look into serialization.
Create-ivity - a game development blog Mouseover for more information.
The first thing you need to do is think about what information needs to be saved, and what the logical structure and interpretation of that information is.
Quote:Original post by Captain P
they often contain much more overhead and are usually slower to parse.

Yeah, you'll be waiting a whole 1/2 second more for your saved game to load up :P.
As long as the file isn't too big (and it wouldn't be more than a meg for settings or game saves) then it will be fairly quick to parse and won't use up too much memory.

I use XML for map and player definitions but for settings i just store each setting on a separate line like so:
framerate = 30screenWidth = 1280screenHeight = 1024...
[Window Detective] - Windows UI spy utility for programmers
I have done this three ways.

The first way I tried was to assign each data to a line in a text file, such as XTAL256 said. In the engine I use, I can import and export data as a 'stringArray' meaning each line is a new value in an array. So my txt file would look something like this:

30
1280
1024

Then when I would scan the data in, I would have to parse it to the proper variables...
//javascript version of an 'Object class'function PersistantData(fr, sw, sh){  this.framerate = fr;  this.screenWidth = sw;  this.screenHeight = sh;}//declared, but not instantiatedvar pd; //check if file existsif(system.file.exists("persistantData.txt")){  //open the existing file  var fileIn = system.file.openRead("persistantData.txt");  //load the data into an array  var dataArray = fileIn.readStringArray();  //close the file  fileIn.close();  //parse the data into a PersistantData object  parsePD(dataArray);}else //if the file does not exist{  //create a new file  var fileOut = system.file.openWrite("persistantData.txt");  //create a new array object to export  var dataArray = new Array("[30]", "[1280]", "[1024]");  //export it  fileOut.writeStringArray(dataArray);  //close the file  fileOut.close();  //parse the data into a PersistantData object  parsePD(dataArray);}//function to parse the dataArray into a PersistantData objectfunction parsePD(pdArray){  //instantiate pd as a PersistantData object...  pd = new PersistantData(pdArray[[0]], pdArray[[1]], pdArray[[2]]);}


The second way, which might not be that great, was to actually create an object for perstistant data, and then convert it to a string, then export this string to a file. When I wanted to import the data I would then import the data as a string and then 'eval' it (eval is a &#106avascript command, I am not sure if there is one similar in C#).
//javascript version of an 'Object class'function PersistantData(fr, sw, sh){  this.framerate = fr;  this.screenWidth = sw;  this.screenHeight = sh;}//declared, but not instantiatedvar pd; //check if file existsif(system.file.exists("persistantData.txt")){  //read the file in  var fileIn = system.file.openRead("persistantData.txt");  //converts the string to a PersistantData object  pd = eval(fileIn.readString());  //close the file  fileIn.close();}else //if the file dosn't exist, IE oobe or deleted{  //instantiate the object and assign it to the pd field  pd = new PersistantData([30], [1280], [1024]);  //create and open a new file called 'persistantData.txt'  var fileOut = system.file.openWrite("persistantData.txt");  //convert the object to a string and write it out to the file  fileOut.writeString(pd.toSource());  //close the file  fileOut.close();}

The last way I have tried is to use XML:
//Javascript Object classfunction PersistantData(fr, sw, sh){  this.framerate = fr;  this.screenWidth = sw;  this.screenHeight = sh;}//declare, but not instantiatedvar pd;//check if file exists...if(system.file.exists("persistantData.xml")){  //if it does, load it  var fileIn = system.xml.loadDocument("persistantData.xml");    //then parse it into a PersistantData object  parsePD(fileIn);}else //if it does not exist, create it{  var fileOut = system.xml.createDocument();    //basically build the file from code  fileOut.addChild("persistantData");  fileOut.selectSingle("persistantData").addChild("framerate").text = "[30]";  fileOut.selectSingle("persistantData").addChild("screenWidth").text = "[1280]";  fileOut.selectSingle("persistantData").addChild("screenHeight").text = "[1024]";    //save the new file  fileOut.save(system.folders.document+"persistantData.xml");    //parse it into a PersistantData object  parsePD(fileOut);}//function to convert the xml data to a PersistantData objectfunction parsePD(xmlFile){  //create new object with data from the xmlFile  pd = new PersistantData(parseInt(xmlFile.selectSingle("persistantData").selectSingle("framerate").text),           parseInt(xmlFile.selectSingle("persistantData").selectSingle("screenWidth").text),           parseInt(xmlFile.selectSingle("persistantData").selectSingle("screenHeight").text));}


I hope this helps, I guess I didn't have to actually write the code, but I was boored. As you can see, the concept is almost the same for everything. You could even load it from a database, etc, etc. It is all the same in the end.
I would go with xml as well.

you'll be using the System.Xml namespace, so start at the MSDN documentation.

Aslo to get you started I googled using System.Xml, and thought the following article would be good

Reading and Writing Xml in C#

if it's as promising as I think it is it'll be the only tutorial you need.

Then once you save game you write to the xml, and if you want to open up a game you simply read a specific xml file.

Hope this helps!
Thanks for the replies. This has really helped me out. This site is excellent.

This topic is closed to new replies.

Advertisement