1

I am starting to code a game in C# using Windows Forms (not using XNA) and I am trying to figure out how to save/load.

Right now all I have is the main form, a few User Controls, and one class in a separate project.

I have looked for a decent tutorial and this is the best I've found:

http://www.codeproject.com/Articles/1789/Object-Serialization-using-C

Would this method be suitable for a Windows Form game? Or is there a better approach for me to take?

Also, if this is the best route, how would I save data from several different classes in a single save file and then read the data in?

Unfortunately I don't have any relevant code. Any tips would be greatly appreciated.

Jason D
  • 2,634
  • 6
  • 33
  • 67
  • 1
    This question might be better suited over at http://gamedev.stackexchange.com/ or they might even have similar questions/answers for you to check out. – Jesse Webb May 09 '13 at 19:47
  • I've used XML Serialization (System.Xml.Serialization namespace) before and it is very simple and efficient especially if you are a "very novice programmer" as you put it. It allows you to save the state of a class and load it later, if you have multiple different classes and want to save them all then you just make a class to hold all other classes and save that. – Mitch May 09 '13 at 20:22

2 Answers2

3

serialize consecutively save class instance count and after that save class instances, then deserialize class count and it's instances. one method for writing and one method for reading using formatters for serialize and deseialize.. save all current state consist of life count and state of game , place of player and other properties. for example see this :
Reading :

IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(op.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
FCnt = (int)formatter.Deserialize(stream);
for (int i = 0; i < FCnt; i++)
{
   facility[i] = (Facility)formatter.Deserialize(stream);
}
stream.Close();

Writing :

IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(FN, FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, FCnt);
for (int i = 0; i < FCnt; i++)
{
formatter.Serialize(stream, facility[i]);
}
stream.Close();
mojtaba
  • 339
  • 1
  • 4
  • 17
  • I apologize, but I need something a bit more straightforward than that. I am a very novice programmer. – Jason D May 09 '13 at 20:11
  • follow http://stackoverflow.com/questions/5017274/c-sharp-binaryformatter-and-deserialization-complex-objects and http://www.c-sharpcorner.com/UploadFile/d3e4b1/serializing-and-deserializing-the-object-as-binary-data-usin/ – mojtaba May 09 '13 at 20:25
2

If you want to serialize the data for your game you might consider creating a "state" class and placing the information you want to save in there. Then you can serialize the instance of your state object in one hit.

ose
  • 4,065
  • 2
  • 24
  • 40