Difference between revisions of "Creating a game save"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(RPG / Platform Game)
Line 30: Line 30:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==RPG / Platform Game==
+
The method below will actually do the saving. You need to set the values of your data structure, and then create a stream which will be used to link to the file. The binarywrite is able to write the data structure to the file, the Serialize line is the bit which writes the file.
For an RPG or Platformer we can focus on saving the player position in the game, and recording the game events in order to reprocess to a similar state. This will require you to  think carefully about what your game will need to be able to save its current position. For example a list of the items collected, a list of the enemies killed, a list of completed challenges, and also the standard information such as player position, score etc.
 
 
 
This will actually do the saving. You need to set the values of your data structure, and then create a stream which will be used to link to the file. The binarywrite is able to write the data structure to the file, the Serialize line is the bit which writes the file.
 
  
 
<syntaxhighlight lang=c#>
 
<syntaxhighlight lang=c#>
Line 48: Line 45:
 
         }
 
         }
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
Now to deserialize you need a load method. The third line of the method casts the serialized data back to an instance of Data:
 +
 +
<syntaxhighlight lang=c#>
 +
        public Vector2 LoadGame()
 +
        {
 +
            Stream streamread = File.OpenRead("mygame.bin");
 +
            BinaryFormatter binaryread = new BinaryFormatter();
 +
            Data data = (Data)binaryread.Deserialize(streamread);
 +
            streamread.Close();
 +
            return new Vector2(data.x, data.y);
 +
        }
 +
</syntaxhighlight>
 +
 +
In your update method, i have added the following to test the loading and saving:
 +
 +
<syntaxhighlight lang=c#>
 +
            if (keyState.IsKeyDown(Keys.B))
 +
            {
 +
                SaveGame(new Vector2(map.ObjectGroups["Objects"].Objects["Player"].X, map.ObjectGroups["Objects"].Objects["Player"].Y));
 +
            }
 +
 +
            if (keyState.IsKeyDown(Keys.N))
 +
            {
 +
                Vector2 testpos = LoadGame();
 +
                map.ObjectGroups["Objects"].Objects["Player"].X = (int)testpos.X;
 +
                map.ObjectGroups["Objects"].Objects["Player"].Y = (int)testpos.Y;
 +
            }
 +
</syntaxhighlight>
 +
 +
==RPG / Platform Game==
 +
For an RPG or Platformer we can focus on saving the player position in the game, and recording the game events in order to reprocess to a similar state. This will require you to  think carefully about what your game will need to be able to save its current position. For example a list of the items collected, a list of the enemies killed, a list of completed challenges, and also the standard information such as player position, score etc.

Revision as of 15:07, 21 January 2018

After trying to save the game in mid play, i have essentially discovered it is difficult if not impossible to save the game mid play.

Tower / Racing Game

For a Tower Defence game, or racing game you will only need to record the data about the game such as score, lives, credits, level etc.

You could create a struct for a GameSave:

public struct GameSave
{
    public string SaveName;
    public int Score;
    public int Lives;
    public int Credits;
    public int Level;
}

We can create a variable based on this struct:

public GameSave gameData;

This method will use serialization so we need to add the following into the using section at the top of your Game1.cs:

using System.IO;
using System.Xml.Serialization;

The method below will actually do the saving. You need to set the values of your data structure, and then create a stream which will be used to link to the file. The binarywrite is able to write the data structure to the file, the Serialize line is the bit which writes the file.

        public void SaveGame(Vector2 pos)
        {
            data.name = "test";
            
            data.x = (int)pos.X;
            data.y = (int)pos.Y;
            Stream streamwrite = File.Create("mygame.bin");
            BinaryFormatter binarywrite = new BinaryFormatter();
            binarywrite.Serialize(streamwrite, data);
            streamwrite.Close();
        }

Now to deserialize you need a load method. The third line of the method casts the serialized data back to an instance of Data:

        public Vector2 LoadGame()
        {
            Stream streamread = File.OpenRead("mygame.bin");
            BinaryFormatter binaryread = new BinaryFormatter();
            Data data = (Data)binaryread.Deserialize(streamread);
            streamread.Close();
            return new Vector2(data.x, data.y);
        }

In your update method, i have added the following to test the loading and saving:

            if (keyState.IsKeyDown(Keys.B))
            {
                SaveGame(new Vector2(map.ObjectGroups["Objects"].Objects["Player"].X, map.ObjectGroups["Objects"].Objects["Player"].Y));
            }

            if (keyState.IsKeyDown(Keys.N))
            {
                Vector2 testpos = LoadGame();
                map.ObjectGroups["Objects"].Objects["Player"].X = (int)testpos.X;
                map.ObjectGroups["Objects"].Objects["Player"].Y = (int)testpos.Y;
            }

RPG / Platform Game

For an RPG or Platformer we can focus on saving the player position in the game, and recording the game events in order to reprocess to a similar state. This will require you to think carefully about what your game will need to be able to save its current position. For example a list of the items collected, a list of the enemies killed, a list of completed challenges, and also the standard information such as player position, score etc.