Useful Methods

5_ScriptableSave

If you scroll down in this script, you'll see the implementation of the Scriptable Save Example SO. This is also where you can customize it for your game. In my case, I added a few practical public getters and various public methods like SetLevel(), AddRandomItem(), ClearItems(), and more.

It is recommended to only allow this class to modify the _saveData directly and make all other classes use the public methods to affect the save. This way, you ensure that all modifications of the save are centralized in this class, and you can control it. Note that after each modification, I manually call Save().

public void AddRandomItem() => AddItem(new Item("RandomItem_" + Items.Count));

public void AddItem(Item item)
{
    _saveData.Items.Add(item);
    Save();
}

public Item GetItemById(string id)
{
    return _saveData.Items.Find(item => item.Id == id);
}

public void ClearItems()
{
    _saveData.Items.Clear();
    Save();
}

public void IncrementLevel(int value)
{
    _saveData.Level += value;
    Save();
}

public void SetLevel(int value)
{
    _saveData.Level = value;
    Save();
}

Depending on your game or save mode, you might not want to trigger a save after each modification.

Let's try now to use these methods. If you check the Items buttons in the hierarchy, you can see that each button directly references the scriptable save SO and calls a public method.

Play the game and start adding items, then clearing them. You will see that the changes are reflected in the save.

Scriptable Save SO also comes with built-in public methods:

  • Save()

  • Load()

  • PrintToConsole()

  • Delete()

  • Open File Location()

These are very useful and you can access them freely from code or from the inspector (like the buttons on the Save Reader). You can also expose them in a custom inspector to quickly access them for debug.

Last updated