# Save Manager

### Save Manager <a href="#id-9x0fhq4tjdd1" id="id-9x0fhq4tjdd1"></a>

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.

<img src="/files/tbamddjBd6Go4wr0PCx2" alt="" width="302">

<img src="/files/BEjtMPoEZ6SnlbmfCMt5" alt="" width="375">

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).

```csharp
public class SaveManager : MonoBehaviour
{
    [SerializeField] private ScriptableSaveExample _scriptableSaveExample;
    [SerializeField] private IntVariable _levelVariable;

    private void Awake()
    {
        _scriptableSaveExample.OnLoaded += SetLevelValue;
        _scriptableSaveExample.OnSaved += SetLevelValue;
    }

    //Load on start as other classes might register to OnLoaded in their Awake
    private void Start()
    {
        if (_scriptableSaveExample.LoadMode == ScriptableSaveBase.ELoadMode.Manual)
            _scriptableSaveExample.Load();
        else
            SetLevelValue();
    }

    private void OnDestroy()
    {
        _scriptableSaveExample.OnLoaded -= SetLevelValue;
        _scriptableSaveExample.OnSaved -= SetLevelValue;
    }

    private void SetLevelValue()
    {
        _levelVariable.Value = _scriptableSaveExample.Level;
    }
}
```

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.

<mark style="color:red;">**Questions**</mark><mark style="color:red;">:</mark> <mark style="color:red;"></mark><mark style="color:red;">**Why would I do this instead of using the “save” option on the scriptable variable ?**</mark>

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.

<mark style="color:red;">**Could I not reference the scriptable save directly to access the Level in the SaveData instead of using a Scriptable Variable ?**</mark>

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.

<mark style="color:red;">**Would it be unreasonable to imagine writing back to the save when the Level Variable value changes ?**</mark>

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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://obvious-game.gitbook.io/soap/scene-documentation/5_scriptablesaves/save-manager.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
