Inventory & Equipment
Saving and loading the inventory and equipped items.
Saving
There are 4 things that require saving here: items, enchantments (applied to items), equipment, and currencies. You can use Inventool.CreateSavableData
to get an object that contains all of this data. This method will return a InventoolSavableData
struct.
InventoolSavableData mySavableData = Inventool.CreateSavableData();
Loading
Use Inventool.SetData
and provide a InventoolSavableData
object as the parameter to load data. This will set the inventory items, equipment, and currencies.
Inventool.SetData(mySavableData);
Examples
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.
With ESave Free
using Esper.ESave;
using Esper.Inventool;
using Esper.Inventool.DataManagement;
using UnityEngine;
public class InventoolSavingExample : MonoBehaviour
{
/// <summary>
/// The ID used to save and load the data.
/// </summary>
private const string dataID = "Inventool";
/// <summary>
/// The save file.
/// </summary>
private SaveFile saveFile;
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 Inventool data.
/// </summary>
private void Save()
{
var savableData = Inventool.CreateSavableData();
saveFile.AddOrUpdateData(dataID, savableData);
saveFile.Save();
}
/// <summary>
/// Loads Inventool data if it exists.
/// </summary>
private void Load()
{
if (saveFile.HasData(dataID))
{
var savableData = saveFile.GetData<InventoolSavableData>(dataID);
Inventool.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 InventoolSavingExample : MonoBehaviour
{
/// <summary>
/// The ID used to save and load the data.
/// </summary>
private const string dataID = "Inventool";
/// <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 =>
{
this.saveState = saveState;
});
}
else
{
// Load the save if it exists
ESave.Load(0).OnComplete(saveState =>
{
this.saveState = saveState;
// Load the inventory
Load();
});
}
}
private void OnApplicationQuit()
{
// Save when exited play mode or the game
Save();
}
/// <summary>
/// Saves Inventool data.
/// </summary>
private void Save()
{
// Create savable inventory data
var savableData = Inventool.CreateSavableData();
// Add to save
saveState.AddData(dataID, savableData).OnComplete(success =>
{
// Save the state
ESave.Save(saveState);
});
}
/// <summary>
/// Loads Inventool data if it exists.
/// </summary>
private void Load()
{
if (saveState.HasData(dataID))
{
// Get the inventory data
saveState.GetData<InventoolSavableData>(dataID).OnComplete(savableData =>
{
// Set to inventory
Inventool.SetData(savableData);
});
}
}
}
Last updated