# Character Stats

&#x20;The [character-stats](https://stylishesper.gitbook.io/inventool/scripting/api/character-stats "mention") class simply works with the [stat-profile](https://stylishesper.gitbook.io/inventool/scripting/api/stats/stat-profile "mention") class, so that's the only data required to be saved and loaded.

## Saving

You can use the `CreateSavableData` method from an instance of a `CharacterStats` to get savable data.

```csharp
SavableProfile mySavableData = myCharacterStats.CreateSavableData();
```

## Loading

Use the `SetData` and provide a `SavableProfile` object as the parameter to load data.

```csharp
myCharacterStats.SetData(mySavableData);
```

## Examples

{% hint style="warning" %}
The example scripts automatically save on exiting Play mode and load on entering it.\
For real use, you’ll need to call the `Save` and `Load` methods whenever you actually want to persist or retrieve data.
{% endhint %}

The code below is how you can save and load character stats data with [ESave](https://app.gitbook.com/o/vQC9VbiGJfIp0CaKhYvJ/s/korEE1oahhwAkGIn7ek0/ "mention"). ESave is my own save system—it's entirely free, and it's been tested with Inventool. ESave is not a requirement; you can use the save system of your choice. However, the example below will only work with ESave.

### With ESave Free

```csharp
using Esper.ESave;
using Esper.Inventool;
using Esper.Inventool.DataManagement;
using UnityEngine;

public class CharacterSavingExample : MonoBehaviour
{
    /// <summary>
    /// The ID used to save and load the data.
    /// </summary>
    private const string dataID = "MyCharacter";

    /// <summary>
    /// The character stats component.
    /// </summary>
    private CharacterStats characterStats;

    /// <summary>
    /// The save state.
    /// </summary>
    private SaveState saveState;

    private void Start()
    {
        saveFile = GetComponent<SaveFileSetup>().saveFile;

        // Load the data on start
        Load();
    }

    private void OnApplicationQuit()
    {
        // Save when exited play mode or the game
        Save();
    }

    /// <summary>
    /// Saves character data.
    /// </summary>
    private void Save()
    {
        var savableData = characterStats.CreateSavableData();
        saveFile.AddOrUpdateData(dataID, savableData);
        saveFile.Save();
    }

    /// <summary>
    /// Loads character data if it exists.
    /// </summary>
    private void Load()
    {
        if (saveFile.HasData(dataID))
        {
            var savableData = saveFile.GetData<SavableProfile>(dataID);
            characterStats.SetData(savableData);
        }
    }
}
```

### With ESave Pro

In case you're using the pro version...

```csharp
using Esper.ESave;
using Esper.Inventool;
using Esper.Inventool.DataManagement;
using UnityEngine;

public class CharacterSavingExample : MonoBehaviour
{
    /// <summary>
    /// The ID used to save and load the data.
    /// </summary>
    private const string dataID = "MyCharacter";

    /// <summary>
    /// The character stats component.
    /// </summary>
    private CharacterStats characterStats;

    /// <summary>
    /// The save state.
    /// </summary>
    private SaveState saveState;

    private void Awake()
    {
        // Get the component
        characterStats = GetComponent<CharacterStats>();
    }

    private void Start()
    {
        // Initialize ESave (if not already done)
        ESave.Initialize();

        if (!ESave.HasSave(0))
        {
            // Create a save if it doesn't exist
            ESave.CreateSave(0).OnComplete(saveState =>
            {
                this.saveState = saveState;
            });
        }
        else
        {
            // Load the save if it exists
            ESave.Load(0).OnComplete(saveState =>
            {
                this.saveState = saveState;

                // Load the character stats
                Load();
            });
        }

        characterStats.BindWithMain();
    }

    private void OnApplicationQuit()
    {
        // Save when exited play mode or the game
        Save();
        
        // Recommended to terminate ESave when its no longer being used
        ESave.Terminate();
    }

    /// <summary>
    /// Saves character data.
    /// </summary>
    private void Save()
    {
        // Create savable character stats data
        var savableData = characterStats.CreateSavableData();

        // Add to save
        saveState.AddData(dataID, savableData).OnComplete(success =>
        {
            // Save the state
            ESave.Save(saveState);
        });
    }

    /// <summary>
    /// Loads character data if it exists.
    /// </summary>
    private void Load()
    {
        if (saveState.HasData(dataID))
        {
            // Get the data
            saveState.GetData<SavableProfile>(dataID).OnComplete(savableData =>
            {
                // Set the data
                characterStats.SetData(savableData);
            });
        }
    }
}
```
