The save system can save the current state of the inventory, and other component, to disk. This state can then be restored by loading the file that it was saved to.

The Save System

Save System Manager

The Save System Manager is responsible for saving the data to the disk. Serialized data can be saved by registering it with the SaverBase component. When the manager is saving it will loop through the SaverBase components and retrieve the data that needs to be saved. It will then serialize that data to disk.

At the time of loading the Save System Manager will go through all registered Savers and load the data. In most cases if Saver component will register itself to the Save System Manager on it’s Start function. Therefore the Savers must “Load On Start” if the Save System Manager Loads the Save Data before the Saver is registered.

Saver Components

Any components that contain data that should be saved will inherit the SaverBase class. The Inventory Saver component is an example which contains data that should be saved. The Inventory Saver will serialize and deserialize the items that are within of the Inventory. Your Inventory content will be saved/loaded by the Save System Manager if the Inventory Saver component is added to the same GameObject as the Inventory.

The Savers available out of the box are:

  • InventorySystemManagerItemSaver : The main saver component. It is used to save the Item states (attribute values, names, etc…). All savers which save Items only save the item IDs. They notify this component to save the actual Item by serializing it once even if it is used in multiple places. Without this saver the “InventorySaver” and other savers which save Item IDs won’t work (except for Immutable & Common Items)
  • InventorySaver : Saves the content of an Inventory component by saving the Item IDs and amounts in each Item Collection.
  • CurrencyOwnerSaver : Saves the content of a Currency Owner.

Some Savers are specific to integrations, for example Ultimate Character Controller (UCC) is incompatible with the standard Inventory Saver:

  • InventoryBridgeSaver : Saves the content of an Inventory which is bound to a UCC character.

Save Data

Save Data is the object that is saved to disk. This contains the serialized data of the inventory state as well as metadata such as the time that it was created. Since the save system is very extensible it may save anything which can be serialized by our serializer.

Save Menu

The save menu uses the Save System Manager to display the saves available to load/save. Check the UI documentation to learn more about the Save Menu.

Save System Manager API

// Save in file 0
SaveSystemManager.Save(0);

// Load in file 0
SaveSystemManager.Load(0);

// Delete save at file 0
SaveSystemManager.DeleteSave(0);

// Get the current state of the cached save data
var saveData = SaveSystemManager.GetCurrentSaveData();

// Get the cached serialized data of a specific Saver component
var saveData = SaveSystemManager.TryGetSaveData("The Saver Key", out var serializedData);

 

Custom Saver Component

To create a custom Saver component inherit the SaverBase class and override the Serialize and Deserialize methods.

/// <summary>
/// An example of a Saver component.
/// </summary>
public class ExampleSaver : SaverBase
{
    /// <summary>
    /// The examples save data.
    /// </summary>
    [System.Serializable]
    public struct ExampleSaveData
    {
        // Add you serializable data here.
        // To save items or currency use IDAmountSaveData.
        // public IDAmountSaveData[] ExampleAmounts;
    }
    /// <summary>
    /// Serialize the save data.
    /// </summary>
    /// <returns>The serialized data.</returns>
    public override Serialization SerializeSaveData()
    {
        // Get the item you want to save
        // var saveAmounts = GetComponent<Example>().Amounts;
        var saveData = new ExampleSaveData() {
            //Set the save Data Value
            //ExampleAmounts = saveAmounts
        };
        // Serialize the save data using our custom Serialization.
        return Serialization.Serialize(saveData);
    }
    /// <summary>
    /// Deserialize and load the save data.
    /// </summary>
    /// <param name="serializedSaveData">The serialized save data.</param>
    public override void DeserializeAndLoadSaveData(Serialization serializedSaveData)
    {
        // Get the save data, Make sure the type matches the type saved
        var savedData = serializedSaveData.DeserializeFields(MemberVisibility.All) as ExampleSaveData?;
        if (savedData.HasValue == false) {
            return;
        }
        var exampleSaveData = savedData.Value;
        //Apply the loaded save data on the object
        // GetComponent<Example>().Amounts = exampleSaveData.ExampleAmounts;
    }
}