Managing Save File Versions

Is it possible to update my game without breaking existing save files from older versions?

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.

Working with Data

Learn techniques for managing game data, including save systems, configuration, and networked multiplayer.

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Preventing Save File Tampering
What happens if a player tries to cheat by editing the save file? How can we prevent that?
Managing Large Save Files
How do professional games handle really large save files, like open world games with lots of player progress?
Saving in Multiplayer Games
For multiplayer games, how do we handle saving when multiple players can modify the same object?
Cross-Platform Save Files
In real games, how do developers manage compatibility between different platforms' save files?
Implementing Autosave
How do games typically handle automatic saving? Should we save after every player action?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant