Example

Saving and loading Inventool data with ESave.

The code below is how you can save and load Inventool's data with ESave. 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.

circle-exclamation

The example scripts below save and load the player's inventory, equipment, recipe catalog, stats, hotbar, and a shopkeeper's inventory.

ESave Free

using Esper.ESave;
using Esper.Inventool;
using Esper.Inventool.Blocks.UGUI;
using Esper.Inventool.CraftingSystem;
using Esper.Inventool.DataManagement;
using UnityEngine;

public class InventoolSavingExample : MonoBehaviour
{
    // Start by getting references to each object that requires saving. You may need more than what's listed here
    // (storage inventory, stats of other characters, etc.). Each field here is set through the inspector.

    /// <summary>
    /// The player's inventory.
    /// </summary>
    public Inventory playerInventory;

    /// <summary>
    /// The player's equipment.
    /// </summary>
    public Equipment playerEquipment;

    /// <summary>
    /// The player's recipe catalog.
    /// </summary>
    public RecipeCatalog recipeCatalog;

    /// <summary>
    /// The player's stats.
    /// </summary>
    public CharacterStats playerStats;

    /// <summary>
    /// The hotbar block.
    /// </summary>
    public HotbarBlock hotbar;

    /// <summary>
    /// The shopkeepers inventory.
    /// </summary>
    public Inventory shopInventory;

    /// <summary>
    /// The save file reference.
    /// </summary>
    private SaveFile saveFile;

    /// <summary>
    /// Player inventory ID used for saving and loading.
    /// </summary>
    private const string inventoryId = "Inventory";

    /// <summary>
    /// Player equipment ID used for saving and loading.
    /// </summary>
    private const string equipmentId = "Equipment";

    /// <summary>
    /// Recipes catalog ID used for saving and loading.
    /// </summary>
    private const string recipesId = "Recipes";

    /// <summary>
    /// Character stats ID used for saving and loading.
    /// </summary>
    private const string statsId = "Stats";

    /// <summary>
    /// Hotbar ID used for saving and loading.
    /// </summary>
    private const string hotbarId = "Hotbar";

    /// <summary>
    /// Shopkeeper inventory ID used for saving and loading.
    /// </summary>
    private const string shopId = "Shop";

    private void Start()
    {
        // Get the save file
        saveFile = GetComponent<SaveFileSetup>().GetSaveFile();

        // Loading on start for testing purposes (you may want to load at a different time)
        Load();
    }

    private void OnApplicationQuit()
    {
        // Saving on application quit for testing purposes (you may want to save at a different time)
        Save();
    }

    /// <summary>
    /// Saves the data.
    /// </summary>
    public void Save()
    {
        // Do nothing if a save file was not set up
        if (saveFile == null)
        {
            return;
        }

        // Convert all to savable
        var savableInventory = playerInventory.ToSavable();
        var savableEquipment = playerEquipment.ToSavable();
        var savableRecipes = recipeCatalog.ToSavable();
        var savableStats = playerStats.ToSavable();
        var savableHotbar = hotbar.ToSavable();
        var savableShop = shopInventory.ToSavable();

        // Add all objects
        saveFile.AddOrUpdateData(inventoryId, savableInventory);
        saveFile.AddOrUpdateData(equipmentId, savableEquipment);
        saveFile.AddOrUpdateData(recipesId, savableRecipes);
        saveFile.AddOrUpdateData(statsId, savableStats);
        saveFile.AddOrUpdateData(hotbarId, savableHotbar);
        saveFile.AddOrUpdateData(shopId, savableShop);

        // Officially write the data into the save file
        saveFile.Save();
    }

    /// <summary>
    /// Loads and applies the data.
    /// </summary>
    public void Load()
    {
        // Do nothing if a save file was not set up
        if (saveFile == null)
        {
            return;
        }

        if (saveFile.HasData(inventoryId))
        {
            var savableInventory = saveFile.GetData<SavableInventory>(inventoryId);
            playerInventory.FromSavable(savableInventory);
        }

        if (saveFile.HasData(equipmentId))
        {
            var savableEquipment = saveFile.GetData<SavableSlotList>(equipmentId);
            playerEquipment.FromSavable(savableEquipment);
        }

        if (saveFile.HasData(recipesId))
        {
            var savableRecipes = saveFile.GetData<SavableObjectList<Recipe>>(recipesId);
            recipeCatalog.FromSavable(savableRecipes);
        }

        if (saveFile.HasData(statsId))
        {
            var savableStats = saveFile.GetData<SavableProfile>(statsId);
            playerStats.FromSavable(savableStats);
        }

        if (saveFile.HasData(hotbarId))
        {
            var savableHotbar = saveFile.GetData<SavableSlotList>(hotbarId);
            hotbar.FromSavable(savableHotbar);
        }

        if (saveFile.HasData(shopId))
        {
            var savableShop = saveFile.GetData<SavableInventory>(shopId);
            shopInventory.FromSavable(savableShop);
        }
    }
}

ESave Pro

Last updated