Working with Data

Managing Save File Versions

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

Abstract art representing computer programming

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.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 75 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2025 - All Rights Reserved