This page explains how the Inventory Engine saves and loads inventory data so it persists across sessions.

Save and Load

The Inventory Engine comes with save and load mechanisms, so you can have persistent inventories across scenes, but also across sessions. When saving, the InventoryEngine will generate (or overwrite) a file on disk for each of your inventories in your scene. It uses MoreMountains’ SaveLoadManager, which will handle file location depending on your device for you, so it works on PC, but also on mobile without effort.

Requesting save and load

For inventories to actually save and load data, they’ll need to receive a save or load request. If you’re using the Corgi Engine or TopDown Engine, this is already built-in so you don’t have to worry about it. If you look at the PixelRogue demos provided with the engine, you’ll see examples of these requests.

Load (which will read the saved file if it exists and fill the inventory with the saved content) is triggered by the InventoryDemoGameManager class, on Start(). In your game, you can request a load from anywhere in your scripts, using the following line :

MMEventManager.TriggerEvent(new MMGameEvent("Load"));

As you can see, the InventoryEngine uses MoreMountains’ Event Manager class to propagate and catch events throughout the various classes. What this line does is trigger a Game Event called “Load”. All classes can decide to listen for this event, and when it’s received, do an action. In this case, the Inventory class listens for Load events, and when received, loads the saved file and fills itself with its contents.

Saving is done just as easily, using the following line :

MMEventManager.TriggerEvent(new MMGameEvent("Save"));

Again, in the Corgi Engine this is built-in. In your game you’ll need to explicitly call it when you deem it necessary. In the PixelRogue demo that comes with the engine, this is called when exiting a room, but you could also decide to have a menu item to actually save, or maybe call that save event when exiting the game.

Deleting saves

You can delete individual saves by calling the ResetSavedInventory() method on a reference of your target inventory :

someInventoryReference.ResetSavedInventory();

And you can delete all inventories at once like so (of course, feel free to change the folder name to match your context) :

string _saveFolderName = "InventoryEngine";
MMSaveLoadManager.DeleteSaveFolder (_saveFolderName);

Handling multiple save files

Maybe in your game you’d like to be able to have multiple save tracks. In this case, you’ll want to make sure you override the save path before saving and loading. Simply specifiying a folder name will do, and these can be anything you want. So for example, if we had two save tracks (A and B), we could do the following :

// set the path to save slot A
Inventory._saveFolderName = "InventoryEngine/SaveA/";
// trigger a save of current inventories on slot A
MMEventManager.TriggerEvent(new MMGameEvent("Save"));

// then later on, in a different session, if Player B is playing, we can load slot B like so :

// set the path to save slot B
Inventory._saveFolderName = "InventoryEngine/SaveB/";
// trigger a load of current inventories on slot B
MMEventManager.TriggerEvent(new MMGameEvent("Load"));