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
  • 📄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
          • Value
        • Effectiveness
      • Stat Profile
    • 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
    • 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

Storage

Saving and loading individual storages.

PreviousInventory & EquipmentNextInput

Last updated 26 days ago

Saving

There are 4 things that require saving here: items, enchantments (applied to items), and currencies. You can use myStorage.CreateSavableData to get an object that contains all of this data. This method will return a InventoolSavableData struct.

InventoolSavableData mySavableData = myStorage.CreateSavableData();

Loading

To load you will need to provide your own method of loading. Use the Storage.SetLoader and provide a method that will load a InventoolSavableData object. Inventool will call this to load a storage when necessary. The method should accept a string parameter that represents the ID of the storage.

Storage.SetLoader(MyStorageLoadingMethod);

Examples

The code below is how you can save and load storage 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 Esper.Inventool.Storing;
using UnityEngine;

public class StorageSavingAndLoadingExample : MonoBehaviour
{
    private SaveFile saveFile;

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

        // Saving when the window is closed
        Storage.onStorageClosed.AddListener(SaveStorage);

        // Setting the loader—the LoadStorage method is used by Inventool to load all storages
        Storage.SetLoader(LoadStorage);
    }

    /// <summary>
    /// Saves a storage.
    /// </summary>
    /// <param name="storage">The storage.</param>
    private void SaveStorage(Storage storage)
    {
        saveFile.AddOrUpdateData(storage.id, storage.CreateSavableData());
        saveFile.Save();
    }

    /// <summary>
    /// Loads and returns saved storage data.
    /// </summary>
    /// <param name="id">The storage ID.</param>
    /// <returns>Loaded storage data.</returns>
    private InventoolSavableData LoadStorage(string id)
    {
        var storageData = saveFile.GetData<InventoolSavableData>(id);
        return storageData;
    }
}

With ESave Pro

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

using Esper.ESave;
using Esper.Inventool.DataManagement;
using Esper.Inventool.Storing;
using System.Threading.Tasks;
using UnityEngine;

public class StorageSavingAndLoadingExample : MonoBehaviour
{
    /// <summary>
    /// The save state.
    /// </summary>
    private SaveState saveState;

    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 =>
            {
                // Load the save
                ESave.Load(0).OnComplete(saveState =>
                {
                    this.saveState = saveState;
                    SetSaveLoadMethod();
                });
            });
        }
        else
        {
            // Load the save if it exists
            ESave.Load(0).OnComplete(saveState =>
            {
                this.saveState = saveState;
                SetSaveLoadMethod();
            });
        }
    }

    /// <summary>
    /// Sets the necessary saving and loading methods.
    /// </summary>
    private void SetSaveLoadMethod()
    {
        // Saving when the window is closed
        Storage.onStorageClosed.AddListener(SaveStorage);

        // Setting the loader—the LoadStorage method is used by Inventool to load all storages
        Storage.SetLoaderAsync(LoadStorage);
    }

    /// <summary>
    /// Saves a storage.
    /// </summary>
    /// <param name="storage">The storage.</param>
    private void SaveStorage(Storage storage)
    {
        saveState.AddData(storage.id, storage.CreateSavableData()).OnComplete(success =>
        {
            ESave.Save(saveState);
        });
    }

    /// <summary>
    /// Loads and returns saved storage data.
    /// </summary>
    /// <param name="id">The storage ID.</param>
    /// <returns>Loaded storage data.</returns>
    private async Task<InventoolSavableData> LoadStorage(string id)
    {
        return await saveState.GetData<InventoolSavableData>(id).task;
    }
}
📄
ESave