Skip to content

Game.AutoSaveSystem

Assembly: Assembly-CSharp
Namespace: Game

Type: class

Base: GameSystemBase

Summary: AutoSaveSystem manages the automatic saving functionality for the game. It listens for changes to general settings to enable/disable the auto-save "watch", periodically checks whether an auto-save should be triggered, enqueues a save task when required, and prunes old auto-save files according to the configured retention policy. It also handles creating a save preview render texture and uses the game's TaskManager and asset database to persist the auto-save.


Fields

  • private float m_LastAutoSaveCheck
    Used to track the last time (UnityEngine.Time.realtimeSinceStartup) the auto-save timer was activated or the last auto-save check time. A value of -1f indicates the auto-save watch is inactive. When active this value is set to the current realtimeSinceStartup to base interval checks from.

  • private float timeSinceStartup => UnityEngine.Time.realtimeSinceStartup
    Private read-only property (backed as a field in the source) that exposes UnityEngine.Time.realtimeSinceStartup for use within the system to measure elapsed realtime independent of game timescale.

Properties

  • (no public properties)

Constructors

  • public AutoSaveSystem()
    Default constructor. The system is attributed with [Preserve] to avoid stripping during build/IL2CPP processes. Initialization is performed in OnCreate override.

Methods

  • protected override void OnCreate()
    Subscribes to SharedSettings.instance.general.onSettingsApplied so the system is notified when settings change (to enable/disable auto-save watch). Calls base.OnCreate().

  • private void OnSettingsChanged(Setting setting)
    Event handler invoked when settings are applied. If the new setting is GeneralSettings and the current game mode is a playable game, it enables or disables the auto-save watch depending on generalSettings.autoSave. When enabling, it prunes excess auto-saves and sets m_LastAutoSaveCheck to the current time. When disabling, it also prunes auto-saves and sets m_LastAutoSaveCheck to -1f. Logs activation/deactivation to COSystemBase.baseLog.

  • protected override void OnDestroy()
    Unsubscribes from SharedSettings.instance.general.onSettingsApplied and calls base.OnDestroy().

  • protected override void OnGamePreload(Purpose purpose, GameMode mode)
    Called during game preload. If auto-save is enabled in settings, ensures the auto-save watch is inactive (sets m_LastAutoSaveCheck = -1f) and logs the state.

  • protected override void OnGameLoadingComplete(Purpose purpose, GameMode mode)
    Called when game loading finishes. If the load purpose is LoadGame or NewGame and auto-save is enabled in settings, activates the auto-save watch by setting m_LastAutoSaveCheck to the current realtimeSinceStartup and logs activation.

  • protected override void OnUpdate()
    Called each frame/update. If the auto-save watch is active (m_LastAutoSaveCheck >= 0f) and the current mode is a playable game, retrieves GeneralSettings and calls CheckAutoSave if autoSave is enabled.

  • private async void CheckAutoSave(GeneralSettings settings)
    Checks if the configured auto-save interval has elapsed since m_LastAutoSaveCheck. If so, updates m_LastAutoSaveCheck to the current time and calls PerformAutoSave(settings) asynchronously.

  • private void PruneAutoSaves(GeneralSettings settings)
    Removes older auto-save files when auto-save retention is limited. If settings.autoSaveCount is Unlimited, nothing is done. Otherwise, it queries the auto-save asset database for SaveGameMetadata entries with target.autoSave == true, orders them by lastModified descending, and deletes any beyond the configured count. Exceptions are caught and logged.

  • public async Task PerformAutoSave(GeneralSettings settings)
    Public helper that triggers a safe auto-save task (via SafeAutoSave) and then prunes auto-saves according to settings. Awaits SafeAutoSave before pruning.

  • private static Task SafeAutoSave()
    Enqueues the static AutoSave() task on TaskManager.instance with the "SaveLoadGame" queue and priority 1. Returns the enqueued Task.

  • private static async Task AutoSave()
    The actual work performed for an auto-save. Creates a RenderTexture preview via ScreenCaptureHelper.CreateRenderTarget, captures a screenshot from Camera.main, gathers the current MenuUISystem to get save info, constructs a timestamped save name, and then saves the game using GameManager.instance.Save into the auto-save asset database (AssetDatabase.user). If a package asset with the same name exists it deletes it first. Exceptions during saving are caught and logged. The preview render texture is destroyed in a finally block.

  • private static ILocalAssetDatabase GetAutoSaveDatabaseTarget()
    Returns the asset database used for auto-saves. Implementation returns AssetDatabase.user.

Usage Example

// AutoSaveSystem is a GameSystem that automatically subscribes to settings in OnCreate.
// You can trigger a manual auto-save (and subsequent pruning) like this:

AutoSaveSystem autoSaveSystem = World.DefaultGameObjectInjectionWorld
    .GetExistingSystemManaged<AutoSaveSystem>();

if (autoSaveSystem != null)
{
    // Perform an immediate auto-save using current general settings
    await autoSaveSystem.PerformAutoSave(SharedSettings.instance.general);
}

Notes and implementation details: - Auto-save activation uses m_LastAutoSaveCheck = -1f to denote "inactive". When active it stores Time.realtimeSinceStartup to use real-time intervals (independent of timeScale). - Interval is read from GeneralSettings.autoSaveInterval and compared as a float against realtimeSinceStartup. - Pruning operates on SaveGameMetadata assets in the user asset database and removes auto-save packages beyond the configured count. - AutoSave is executed via TaskManager.instance.EnqueueTask to keep save operations off the main thread as appropriate; however it still interacts with Unity objects (RenderTexture, Camera.main) and UI systems, so care is taken to create/destroy render resources and catch exceptions. - The system subscribes to SharedSettings.instance.general.onSettingsApplied in OnCreate and unsubscribes in OnDestroy to avoid dangling handlers.