Task: The savegame

04.22.2010
Created for the project: Contre-Jour on 2010 at Supinfogame - Skill: C#
Softwares: Unity 3D

As an adventure game, Contre-Jour has a storyline. So, it was necessary to save the player progress in the game. As well, the game is organized in little levels linked together with some entrances/exits. When the hero moves to another level, the progress is saved in a binary file.

The save concerns different data:

  • The player data: coins collected, current level, current entrance in the level...
  • The data concerning all the levels: the position of the pushable objects, the coins and items taken, the doors opened, the switches positions,...
  • The savable parameters concerning the GSE: To play events only once, ...

To do this, I used a binary file with a specific nomenclature. This kind of file is sequentially organized. A playhead reads or writes the data and moves forward.

The savegame nomenclature

In the main flow

  • The player data at first
  • The byte number of the whole flow concerning the levels

In the levels flow

The flow contains the total of levels and a list with each level data as items: the number of bytes and each level flow corresponding.

In each level flow

Each flow contains the number of the objects saved and the sequential list of the data for each one. An object data is made of an identifier and a specific flow. (For a pushable crate, we keep the position for example). GSE boxes are handled like other objects but we save the parameters instead.

On the code side

Originally, I used two kinds of basic C# functions.

To save:
//Create a file or open the existing one and delete the content
BinaryWriter binWriter = new BinaryWriter(File.Open("myfile.txt",FileMode.Create));
//Write data in the file
binWriter.Write("CJSaveGame");//a string
binWriter.Write(10);//an int
binWriter.Write(10.5f);//a float
binWriter.Write(true);//a bool
//etc.
binWriter.Close();//close the file
To load:
if(File.Exists("myfile.txt")){//Verify the file existence
//Open the file
BinaryReader binReader = new BinaryReader(File.Open("myfile.txt" , FileMode.Open));
try{
if(binReader.PeekChar() != -1)//If the file isn't empty
{
//ReadString: read bytes and move the readhead
string str = binReader.ReadString();//a string
int integer = binReader.ReadInt32();//an int
float number = binReader.ReadSingle();//a float
bool boolean = binReader.ReadBoolean();//a bool
}
}
finally{
binReader.Close();//close the file
}
}

Summary

Like all next-generation videogames, Contre-Jour saves automatically the player progress with checkpoints (Here,the level change). Then, the player can go back to the current level if he quits.
Work: I coded the save and load processes.