Saving & Loading Stats

Learn how you can save and load stats.

UnitStats and Stat are serializable classes; thus, they can be saved by most save systems. If you're looking for a save system, try ESaveโ€”it has been tested with Skill Tree, and it's free!

Since every save system may be different, this example will only show you how stats can be saved with ESave.

Saving Stats

// Get the save file reference
var saveFile = GetComponent<SaveFileSetup>().saveFile;

// Where unitStats is a UnitStats object (this can be obtained from a UnitStatsProvider)
// Save the data
saveFile.AddOrUpdateData("stats", unitStats);
saveFile.Save();

Loading Stats

// Get the save file reference
var saveFile = GetComponent<SaveFileSetup>().saveFile;

// Load the stats
var unitStats = saveFile.GetData<UnitStats>("stats");

// Where statsProvider is a UnitStatsProvider object
// Set the data to the provider
statsProvider.data = unitStats;
statsProvider.Refresh();

Learn more about ESave here.

Last updated