Reputation: 23
So i am currently working on a simple game project that most people start of with "breakout". part of my goal is to make a gamesave of the current gamestate the simplest way possible. Eg.
==scenario==
then lets say i lose the game or i want to go to bed because i'm tired, so i want to reload the gamestate to where i come back to the same position of the paddle, ball and bricks. the only problem is i have multiple forms and i just want to save this specific form in its specific state.
i have an idea on how to approach (i would find a way to save the position of the paddle, ball and the bricks and that exact time) just i have not a clue on how i would execute it. How exactly can i achieve this feat?
Upvotes: 1
Views: 3267
Reputation: 13685
This can be a very complex problem but in your case I would suggest starting out simple and going from there as your game becomes more complex. Essentially the probablem is known as Serialization. Or, writing the memory of your application out to disk or a database in a way that it can be read back into memory later.
There are a lot of techniques for doing this, such as converting your objects into XML or JSON or some other binary file format. A bitmap file for example is just a serialized form of some image structures in memory.
I don't really know the technology you are using to make your game other than C# (XNA?) But essentially you want to use some sort of file-system API that's available in your environment and write your serialized objects there. Based on your question you might need to re-design some of your game to facilitate this. Generally it's good practice to have a single 'source of truth' representation of your game state but even if you don't you can still probably figure it out.
So here is a rough outline of steps I would take...
Serialization
Deserialization
[EDIT: adding some extra links and a quick code sample]
Here is an open source json library that is quite good: http://json.codeplex.com/
// somehow convert your game state into a serializable object graph
var saveState = gameState.Save();
// use json.net to convert that to a string
string json = JsonConvert.SerializeObject(saveState, Formatting.Indented);
// save that string to the file system using whatever is available to you.
fileSystemService.WriteAllText("Saves\game1.json", json);
Upvotes: 6
Reputation: 129065
You can simply save it in an XML file, and then read the XML-file at startup.
Upvotes: 1