简体   繁体   中英

How to save a game state?

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?

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

  1. Write a routine that takes your game state and outputs simpler, serializable objects.
    • These simple objects will mostly have all public getter/setter properties with no functionality.
    • Try to write out only the bare minimum information you need to recreate your game state again later.
    • for example see how chess serializes game state: chess notation
  2. Serialize those objects into JSON (because its easy and flexible)
  3. Write that json to disk.
    • I would probably use a convention base approach here.
    • such as "Saves\\game1.json" is the state for the first game slot.
    • Just infer the saved games based on the presence of the files of the right name.

Deserialization

  1. Look for game saves based on the above convention.
  2. Deserialize the JSON back into the simple serialization objects.
  3. Recreate the more complex game state by looking at the serialization objects.
    • Consider using a visitor pattern.
  4. Resume the game using the newly hydrated game state.

[ 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);

您可以简单地将其保存在XML文件中,然后在启动时读取XML文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM