Inventool Documentation
  • Inventool Documentation
  • ๐Ÿ•น๏ธQuick Start
    • Installation
    • Start Creating
  • ๐Ÿ’กGeneral
    • Running the Demo
    • Menu Options
    • UI Updates
    • UI Customizations
      • uGUI
      • UI Toolkit
    • Custom Item Drops
  • ๐ŸŽ“Tutorials
    • Demo Walkthrough
  • โœ๏ธEditors
    • Inventool
      • Inventory
      • Equipment
      • Items
      • Currencies
      • Crafting
      • Dismantling
      • Enchanting
      • Stats
        • Stat IDs
        • Attributes
      • Settings
    • Item Type Manager
    • Localization Editor
    • Stats Editor
    • Merchants
      • Shopkeeper
      • Craftsman
      • Enchanter
    • Storage
    • Loot
      • Loot Box
      • Item Pouch
      • Currency Pouch
    • Components
      • Initializer
      • UI
        • uGUI
          • Inventool Window
          • Split UI
            • Inventory Window UGUI
            • Equipment Window UGUI
            • Key Items Window UGUI
            • Crafting Window UGUI
            • Enchanting Window UGUI
            • Storage Window UGUI
            • Shop Window UGUI
            • Selector UGUI
          • Action Menu UGUI
          • Hover Details UGUI
          • Confirm Prompt UGUI
          • Quantity Prompt UGUI
          • Character Viewer Element UGUI
        • UI Toolkit
          • Inventool Window
          • Split UI
            • Inventory Window
            • Equipment Window
            • Key Items Window
            • Crafting Window
            • Enchanting Window
            • Storage Window
            • Shop Window
          • Action Menu
          • Hover Details
          • Confirm Prompt
          • Quantity Prompt
        • Character Viewer
      • Overworld Merchant
      • Storage Keeper
      • Item Drop
      • Item Spawner
      • Input
        • Cross Input Support
        • Cross Input Support UGUI
          • Target Selectable
      • Character Stats
  • ๐Ÿ“„Scripting API
    • Initialization
    • Inventory
    • Equipment
      • Equipment Slot
    • Items
      • Item
      • Item Type
      • Item Stack
      • Item Drop
      • Item Spawner
      • Loot Box
      • Item Pouch
    • Currencies
      • Currency Identity
      • Currency
        • Value
      • Currency Pouch
    • Crafting
      • Craft
      • Crafter
    • Enchanting
      • Enchantment
    • Stats
      • Stat Identity
      • Attribute
      • Stat
        • Stat Value
        • Stat Scaling Source
        • Effectiveness
      • Scaling Value
        • Numeric Value
          • Value
        • Scaling Source
      • Stat Profile
      • Character Stats
    • Storing
      • Storage
      • Storage Keeper
    • Settings
    • Merchants
      • Shopkeeper
      • Craftsman
      • Enchanter
      • Overworld Merchant
    • UI
      • uGUI
        • Inventool Window UGUI
        • Split UI
          • Draggable Window UGUI
            • Inventory Window UGUI
            • Equipment Window UGUI
            • Key Items Window UGUI
            • Crafting Window UGUI
            • Enchanting Window UGUI
            • Storage Window UGUI
            • Shop Window UGUI
          • Selector UGUI
        • Action Menu UGUI
        • Hover Details UGUI
        • Confirm Prompt UGUI
        • Quantity Prompt UGUI
        • Draggable Element UGUI
          • Equipment Slot Element UGUI
          • Item Slot Element UGUI
          • Item Stack Element UGUI
        • Action Menu Option UGUI
        • Currency Element UGUI
        • Inventory Filter UGUI
        • Inventory Slot UGUI
        • Key Item Element UGUI
        • Shop Item Element UGUI
        • Storage Currency Element UGUI
        • Stat Element UGUI
      • UI Toolkit
        • Inventool Window
        • Split UI
          • Draggable Window
            • Inventory Window
            • Equipment Window
            • Key Items Window
            • Crafting Window
            • Enchanting Window
            • Storage Window
            • Shop Window
        • Action Menu
        • Hover Details
        • Confirm Prompt
        • Quantity Prompt
        • Item Elements
          • Item Stack Element
          • Equipment Slot Element
          • Item Slot Element
          • Shop Item Element
      • Action Menu Option
      • Character Viewer
    • Events
    • Sounds
    • Saving & Loading
      • Inventory & Equipment
      • Storage
      • Character Stats
    • Input
      • Cross Input Support
      • Cross Input Support UGUI
    • Localization
      • Localization Settings
      • Localizer
  • ๐Ÿ› ๏ธSupport
    • Getting Help
  • ๐Ÿ“šChangelogs
    • Latest Releases
    • Future Plans
  • โญRate Me?
Powered by GitBook
On this page
  • Saving
  • Loading
  • Examples
  • With ESave Free
  • With ESave Pro
  1. Scripting API
  2. Saving & Loading

Character Stats

Saving and loading CharacterStats.

PreviousStorageNextInput

Last updated 7 days ago

The Character Stats class simply works with the Stat Profile 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.

SavableProfile mySavableData = myCharacterStats.CreateSavableData();

Loading

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

myCharacterStats.SetData(mySavableData);

Examples

The code below is how you can save and load character stats data with . 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

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

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();
    }

    /// <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);
            });
        }
    }
}
๐Ÿ“„
ESave