Implementing Autosave

How do games typically handle automatic saving? Should we save after every player action?

Autosave systems need to balance data safety with performance. Here's a typical implementation:

Event-Based Saving

Save after significant events rather than every action:

class AutosaveManager {
  std::chrono::steady_clock::time_point
  LastSave;
  bool SignificantChangeOccurred{false};

public:
  void OnPlayerAction(
    const Action& PlayerAction) {
    if (IsSignificantAction(PlayerAction)) {
      SignificantChangeOccurred = true;
      TryAutosave();
    }
  }

  bool IsSignificantAction(
    const Action& PlayerAction
  ) {
    return
      PlayerAction.Type == ActionType::LevelUp ||
      PlayerAction.Type == ActionType::ItemAcquired ||
      PlayerAction.Type == ActionType::QuestComplete;
  }

  void TryAutosave() {
    auto Now = std::chrono::steady_clock::now();
    bool TimeElapsed =
      (Now - LastSave) >
      std::chrono::minutes(5);

    if (SignificantChangeOccurred &&
      TimeElapsed) {
      SaveGame();
      LastSave = Now;
      SignificantChangeOccurred = false;
    }
  }
};

Background Saving

To prevent gameplay interruption, save in a separate thread:

#include <thread>
#include <queue>
#include <mutex>

class BackgroundSaver {
  std::thread SaveThread;
  std::queue<SaveRequest> SaveQueue;
  std::mutex QueueMutex;
  bool Running{true};

public:
  BackgroundSaver() {
    SaveThread = std::thread([this]() {
      while (Running) {
        ProcessSaveQueue();
        std::this_thread::sleep_for(
          std::chrono::milliseconds(100)
        );
      }
    });
  }

  void QueueSave(const GameState& State) {
    std::lock_guard Lock(QueueMutex);
    SaveQueue.push({
      State,
      std::chrono::steady_clock::now()
    });
  }
};

Many games also maintain multiple autosave slots and rotate between them to prevent corruption if a save is interrupted.

The key is finding the right balance between saving frequently enough to prevent significant progress loss while not impacting performance.

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.

Managing Save File Versions
Is it possible to update my game without breaking existing save files from older versions?
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?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant