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, 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.