Introduction
As your game scales, you’ll probably want to link levels together to create bigger worlds, and give the player a sense of progression. By default in Unity data will be lost when loading a new scene (unless specified otherwise). That’s when save and load mechanics come into play. This page covers the various tools the engine provides to save and persist data, and load it when needed.
Automatic Save
Some systems in the engine handle their own data persistence, so you don’t have to worry about them.
- achievements
- sound settings
- inventories
All of these systems rely on the MMSaveLoadManager to save and load their data. Inventories will require that you mark them as persistent, and that you trigger a load event, but aside from that, the rest is automatic. You can learn more about these specifics in the Inventory Engine’s documentation. 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 Characters
In your game, maybe you’ll want your character to start fresh at the start of every scene, but maybe you’ll want it to persist and be in the exact same state it left the previous scene in. This will prevent its various components to reset their states. For this, you can simply add the CharacterPersistency ability to your character. As your character moves from scene to scene, its components will remain the same, as will these components’ values. It will also be automatically passed to the new scene’s LevelManager, so nothing to worry about there either!
Saving Progress across scenes
Perhaps you’ll also be interested in preserving your game’s progress. But what exactly constitutes progress? It varies greatly depending on your specific game. You may wish to keep track of various elements such as scores, collected stars, visited levels, and more. Due to these diverse requirements from game to game, the engine does not provide a universal save system, recognizing that each game has distinct needs in terms of saving data. However, you can check an example of progress management in the Deadline demo scene(s). Within it, the DeadlineProgressManager class assumes the responsibility of saving and loading progress across levels. It’s a great starting point to build your own progress management system.
If you wish to do so, you have two options: you can either copy this progress manager into your own implementation, or extend it if your requirements closely align. The Deadline Progress Manager employs serializable classes to store data in a file for future retrieval. It listens for LevelComplete events, and upon receiving one, it records the saved level, unlocks, remaining lives, and collected stars (including their types). When combined with the inventory and achievements systems, which handle their own save and load mechanisms, you’ll have a comprehensive solution to preserve all aspects of progress in your game.
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.