Difference between revisions of "Creating a game save"
(→RPG / Platform Game) |
(→RPG / Platform Game) |
||
Line 33: | Line 33: | ||
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. | 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#> | ||
− | public | + | public void SaveGame(Vector2 pos) |
{ | { | ||
data.name = "test"; | data.name = "test"; |
Revision as of 13:53, 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;
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.
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.
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();
}