This page describes how you can save and load things in the Corgi Engine.

Introduction

Once your game starts to scale, you’ll likely have more than one scene. In Unity, data is lost (unless specified otherwise) when going from one scene to the other. So you’ll likely want to save things. This page covers the various ways you can do so in the engine, and covers the parts where it’s already done for you.

Automatic save in the engine

Some systems inside the engine already handle their own save and load, so you don’t have to worry about them :

  • achievements
  • sound settings
  • inventories

All of these use the MMSaveLoadManager class to do so. You can of course check out their code to see how they do it, if you want to build similar systems. And if you want to clear the save files associated to these systems, you can use Unity’s Tool menu, then go to MoreMountains > Reset All Achievements, Reset All Progress, or Reset All Saved Inventories.

Persistent Character

Sometimes you just want a Character that persists across scenes, without resetting its state, health, components, values, etc. For this, you can simply add the CharacterPersistency ability to your character. When it’ll travel from scene to scene, it’ll keep all its attributes, and all the changes you’ve made to it, and it’ll be passed to the new scene’s LevelManager to be used as the scene’s playable character.

Saving progress

Maybe you’ll also want to save progress. What actually is progress? That’s probably very unique to your game. Maybe you’ll want to keep track of score, collected mushrooms, amount of visited levels, etc. For this reason there is no universal save system in the engine, as all games will by nature have different needs when it comes to saving stuff. That said, you’ll find an example of progress management in the RetroAdventure folder. In it, the RetroProgressManager class is responsible for saving and loading progress across levels, and you can of course look at how it works to build your own system.

You can either copy this one into your own progress manager, or extend it if your needs are close enough. The Retro Adventure Progress Manager uses serializable classes to save data to file and read it later. It waits for LevelComplete events, and when it gets one, it saves what level’s been saved, which ones are unlocked, how many lives are left to the player, and how many stars (and which ones) were collected. When combined with the inventory and achievements systems (which handle their own save/load mechanisms), you’ve got a complete solution to save all progress in your game.

Adding a level to RetroAdventure

  1. in Unity 6000.0.23f1 (or higher), create a new project and import Corgi Engine v9.1 via the Package Manager
  2. duplicate RetroAdventure3, name it RetroAdventure6
  3. add that newly created scene to the build settings
  4. open RetroAdventureLevelSelection, under the Content node, duplicate the RetroAdventureLevel5 node, change its ItemTitle’s text to “Level6”, on its RetroAdventureLevel comp, change scene name to “RetroAdventure6”
  5. on the RetroAdventureProgressManager (in that scene and on the prefab), add a 6th scene, set its name to “RetroAdventure6”
  6. press play, go through the levels
  7. at the end of lvl 5 you get redirected to the level selection (that’s normal, we didn’t change the exit scene in that level)
  8. play level 6, collect some stars in it, return to level selection, enjoy your newly collected stars

Recreating a RetroAdventure-like progress

Creating new scenes

  1. in Unity 6000.0.23f1 (or higher), create a new project and import Corgi Engine v9.1 via the Package Manager
  2. duplicate the MinimalLevel, name it Progress1
  3. open it
  4. select the RetroAdventureProgressManager prefab, change its 5 scene names to Progress1, Progress2, Progress3, Progress4 and Progress5
  5. drag the prefab in your scene
  6. drag a RetroAdventureLevelEnd prefab in your scene, position it at -4,-4.35,0
  7. change its FinishLevel/LevelName to Progress2
  8. select the ButtonLevelSelection’s Background, set its LevelSelector’s Level Name to RetroAdventureLevelSelection
  9. remove the UICamera from the scene, drag a RetroAdventureUICamera into it instead
  10. disable its InventoryCanvas child
  11. save the scene
  12. duplicate it, rename the copies Progress2, Progress3, Progress4 and Progress5
  13. open them each to edit their RetroAdventureLevelEnd and change the destination scenes to n+1, have Progress5’s be RetroAdventureLevelSelection
  14. add all 5 levels to the build settings (file > build settings > drag and drop your new scenes in the list)

Creating a new level selection scene

  1. duplicate the RetroAdventureLevelSelection scene, name it ProgressLevelSelection
  2. for each of the buttons (at the RetroAdventureLevel component level), change the Scene Names to Progress1, 2 etc
  3. save the scene
  4. add it to the build settings

Testing

  1. click the “Tools > More Mountains > Reset all progress” menu item
  2. press play, notice all levels are locked except level 1
  3. click on level 1’s play button
  4. in Progress1, move right to touch the level’s exit
  5. click on the “next level” button
  6. in Progress2, move right to touch the level’s exit
  7. click on the “next level” button
  8. in Progress3, press escape, then Back to level selection
  9. notice all 3 first levels are unlocked

The MMSaveLoadManager

The engine comes with a simple class to handle basic save and load needs. The MMSaveLoadManager is a static class that allows the save and load of objects in a specific folder and file. It will let you save and load any object to a file. You can find examples of it in action in the Inventory and MMAchievementManager classes, for example.

To save an object, simply call :

MMSaveLoadManager.Save(TestObject, FileName+SaveFileExtension, FolderName);

Then to load it :

TestObject = (YourObjectClass)MMSaveLoadManager.Load(typeof(YourObjectClass), FileName + SaveFileExtension, FolderName);

There are also helper methods to delete saves, save folders, etc. Don’t hesitate to check out the class, it’s fully commented, as usual. You can also specify what IMMSaveLoadManagerMethod the system should use. By default it’s binary but you can also pick binary encrypted, json, or json encrypted. You’ll find examples of how to set each of these in the MMSaveLoadTester class

Persisting other things

The Inventory Engine also lets you persist more than just inventories. In fact, it lets you persist any object you want, meaning that when exiting a scene you can save the state of an object, and the next time you come back to it, whether it’s in the same session, or days later in a different session, that object’s state will be loaded and restored.

You can learn more about how to do that in the Inventory Engine’s documentation.