This is a critical consideration for any game that will receive updates. The solution involves implementing a versioning system for your save files. Here's how it typically works:
First, include a version number in your save file format. This should be one of the first pieces of data you write:
#include <fstream>
#include <string>
struct GameSave {
int Version{1}; // Current save format version
std::string PlayerName;
int Score;
// Other game data...
};
void SaveGame(const GameSave& Save) {
std::ofstream File{
"save.dat", std::ios::binary};
File.write(
reinterpret_cast<const char*>(
&Save. Version),
sizeof(Save.Version)
);
// Write remaining data...
}
When loading, first read the version number and handle different versions appropriately:
GameSave LoadGame() {
GameSave Save;
std::ifstream File{
"save.dat", std::ios::binary};
// Read version first
int FileVersion{};
File.read(
reinterpret_cast<char*>(&FileVersion),
sizeof(FileVersion));
if (FileVersion == 1) {
// Handle version 1 format
} else if (FileVersion == 2) {
// Handle version 2 format
}
return Save;
}
When you update your game and need to change the save format, increment the version number and add code to convert old saves to the new format. For example, if version 2 adds a new Level
 field:
GameSave ConvertSaveV1ToV2(
const GameSave& OldSave
) {
GameSave NewSave;
NewSave.Version = 2;
NewSave.PlayerName = OldSave.PlayerName;
NewSave.Score = OldSave.Score;
// Set default for new field
NewSave.Level = 1;
return NewSave;
}
The key is to never remove old loading code - always maintain the ability to load all previous versions of your save format. This ensures players can continue their games even after updates.
Answers to questions are automatically generated and may not have been reviewed.
Learn techniques for managing game data, including save systems, configuration, and networked multiplayer.