Example
Saving and loading Inventool data with ESave.
ESave Free
ESave Pro
using Esper.ESave;
using Esper.Inventool;
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 (shop inventory,
// 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 loaded save state reference.
/// </summary>
private SaveState saveState;
/// <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";
private void Start()
{
// Initialize ESave if required
if (!ESave.IsInitialized)
{
ESave.Initialize();
}
// 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 state was not loaded
if (saveState == null)
{
return;
}
// Convert all to savable
var savableInventory = playerInventory.ToSavable();
var savableEquipment = playerEquipment.ToSavable();
var savableRecipes = recipeCatalog.ToSavable();
var savableStats = playerStats.ToSavable();
// Add all objects in one call
saveState.AddData((inventoryId, savableInventory), (equipmentId, savableEquipment), (recipesId, savableRecipes), (statsId, savableStats)).OnComplete(success =>
{
// Officially save (usually this should only be called when the player decides to save)
if (success)
{
ESave.Save(saveState);
}
});
}
/// <summary>
/// Loads and applies the data.
/// </summary>
public void Load()
{
// Ensure a save state exists
int saveId = 0;
if (!ESave.HasSave(saveId))
{
// If not, create it
ESave.CreateSave(saveId);
}
// Load the save
ESave.Load(saveId).OnComplete(saveState =>
{
this.saveState = saveState;
// Apply save data to objects only if they exist
if (saveState.HasData(inventoryId))
{
saveState.GetData<SavableInventory>(inventoryId).OnComplete(savableInventory =>
{
playerInventory.FromSavable(savableInventory);
});
}
if (saveState.HasData(equipmentId))
{
saveState.GetData<SavableSlotList>(equipmentId).OnComplete(savableEquipment =>
{
playerEquipment.FromSavable(savableEquipment);
});
}
if (saveState.HasData(recipesId))
{
saveState.GetData<SavableObjectList<Recipe>>(recipesId).OnComplete(savableRecipes =>
{
recipeCatalog.FromSavable(savableRecipes);
});
}
if (saveState.HasData(statsId))
{
saveState.GetData<SavableProfile>(statsId).OnComplete(savableStats =>
{
playerStats.FromSavable(savableStats);
});
}
});
}
}Last updated