Save Manager
5_ScriptableSave
Last updated
5_ScriptableSave
Last updated
Now, let's see how we can use the Scriptable Save to initialize Scriptable variables.
I created a simple script called SaveManager.cs. You can find it attached to the SaveManager object in the hierarchy.
This script has a reference to both the save and the variables it can initialize (in this case I only have one, but you can have as many as you need).
Essentially, whenever the save events OnSaved or OnLoaded are called, we just need to set the values of our variables to those of the save. This way, we use the scriptable save as the source of truth. We are now free to use and reference our scriptable variable in our different components, as we used to do before.
Questions: Why would I do this instead of using the “save” option on the scriptable variable ?
It's up to you. For simple games, using the Player Prefs saving option of Scriptable variables might be enough. However, for more complex games or if you prefer to save all your data in a single file, this approach might be preferable.
Could I not reference the scriptable save directly to access the Level in the SaveData instead of using a Scriptable Variable ?
You could, but then you wouldn't get access to the benefits of using a scriptable variable, notably the OnValueChanged callback, the built-in compatibility with the bindings, and the simplicity of only getting a single variable instead of the whole save. Finally, restricting the scriptable save to key components gives you control over which components can access the save.
Would it be unreasonable to imagine writing back to the save when the Level Variable value changes ?
It wouldn't be! In fact, you can do this, but it's tricky. You can easily fall into a loop where OnValueChanged modifies the saves, which then calls OnValueChanged again, and so on. This is especially problematic if you save/load/delete the save at runtime. If you choose to implement this, you need to be very careful and do thorough testing, but it is indeed possible and can be convenient sometimes! I'm not showing this as an example, as I believe it can be confusing for people starting with Soap.