Class AutoSaveSystem<TSerializer>
A system that automatically saves the world state at configurable intervals.
public sealed class AutoSaveSystem<TSerializer> : SystemBase, ISystem, IDisposable, ISystemLifecycle where TSerializer : IComponentSerializer, IBinaryComponentSerializer
Type Parameters
TSerializerThe serializer type that implements both serialization interfaces.
- Inheritance
-
AutoSaveSystem<TSerializer>
- Implements
- Inherited Members
Examples
// Create auto-save system with default config
var autoSave = new AutoSaveSystem<MySerializer>(serializer);
world.AddSystem(autoSave, SystemPhase.PostRender);
// Configure for more frequent saves
autoSave.Config = AutoSaveConfig.Frequent;
// Manually trigger a save
autoSave.SaveNow();
Remarks
The AutoSaveSystem monitors elapsed time and optionally entity changes to trigger automatic saves. It supports both full snapshots and delta (incremental) saves.
Delta saves are significantly smaller than full saves when few entities change between saves. The system automatically creates a new baseline after a configurable number of deltas to prevent long restoration chains.
This system requires the world to implement ISaveLoadCapability. The standard World class provides this capability.
Constructors
AutoSaveSystem(TSerializer, AutoSaveConfig?)
Creates a new auto-save system with the specified serializer.
public AutoSaveSystem(TSerializer serializer, AutoSaveConfig? config = null)
Parameters
serializerTSerializerThe component serializer for AOT-compatible serialization.
configAutoSaveConfigOptional configuration. Uses default if not specified.
Properties
ChangesSinceLastSave
Gets the number of tracked entity changes (creations and destructions) since the last save.
public int ChangesSinceLastSave { get; }
Property Value
Remarks
This is the value compared against ChangeThreshold to drive the change-based auto-save trigger. It resets to zero after each successful save.
Config
Gets or sets the auto-save configuration.
public AutoSaveConfig Config { get; set; }
Property Value
CurrentDeltaSequence
Gets the current delta sequence number (0 if no saves have occurred).
public int CurrentDeltaSequence { get; }
Property Value
HasBaseline
Gets whether a baseline snapshot exists.
public bool HasBaseline { get; }
Property Value
TimeSinceLastSave
Gets the time elapsed since the last save.
public float TimeSinceLastSave { get; }
Property Value
Methods
CreateNewBaseline()
Creates a new baseline snapshot, resetting the delta chain.
public SaveSlotInfo? CreateNewBaseline()
Returns
- SaveSlotInfo
The save slot info for the new baseline.
Dispose(bool)
Releases resources used by this system.
protected override void Dispose(bool disposing)
Parameters
disposingboolTrue if called from Dispose(), false if called from finalizer.
OnInitialize()
Called after the system is initialized with a world. Override to set up queries and resources.
protected override void OnInitialize()
Reset()
Resets the auto-save state, clearing any existing baseline.
public void Reset()
SaveNow()
Manually triggers an auto-save immediately.
public SaveSlotInfo? SaveNow()
Returns
- SaveSlotInfo
The save slot info, or null if save failed.
Update(float)
Called each frame/tick to update the system.
public override void Update(float deltaTime)
Parameters
deltaTimefloatTime elapsed since the last update in seconds.
Events
OnAutoSave
Event raised when an auto-save occurs.
public event Action<SaveSlotInfo>? OnAutoSave
Event Type
OnAutoSaveError
Event raised when an auto-save fails.
public event Action<Exception>? OnAutoSaveError