Quick Link: Save System Quick Start Steps
A saved game contains:
Saving consists of two operations:
Loading consists of two similar operations:
The Dialogue System provides built-in support for saving the savegame string to PlayerPrefs. Other save destinations are up to you.
GameObjects can use the Save System to record scene changes into the Lua environment. When the player leaves a scene and returns to it later, the GameObjects can retrieve their current state from the Lua environment. The mechanism for doing this is a persistent data script that implements two methods, one to store state data in Lua and another to retrieve state data from Lua.
There is no "one size fits all" solution to saving games, since every game is different.
The steps below describe in general how to set up the Save System.
If you use the LoadLevel() sequencer command, it will use the LevelManager if present.
To change scenes in a script call:
LevelManager levelManager = DialogueManager.Instance.GetComponent<LevelManager>(); levelManager.LoadLevel(s);
You'll need to adjust the first line if you've put the LevelManager component on a different GameObject. This will record the current scene's persistent data, load the new scene, and apply the saved data to the scene. LevelManager calls PersistentDataManager.LevelWillBeUnloaded()
for you before leaving the old the scene, to properly handle GameObjects that use Increment On Destroy.
For some important internal details, see Level Manager Details.
If you don't want to use a LevelManager, then before changing to a new scene, call:
PersistentDataManager.Record(); PersistentDataManager.LevelWillBeUnloaded();
The first line tells all persistent data components in the current scene to record their states into the Lua environment.
The second line tells the components that the current scene is about to be unloaded. Some persistent data components perform special actions when they receive the "OnDestroy" message. Since GameObjects are also destroyed when unloading a level, we want the components to ignore the next "OnDestroy" message.
Then load your new scene.
After loading the new scene, call:
PersistentDataManager.Apply();
This tells all persistent data components in the new scene to retrieve their previously-saved states from the Lua environment. NOTE: After loading a level, you will usually want to wait one frame before calling PersistentDataManager.Apply() to allow GameObjects in the new level to complete their Start() methods first.
If you've added a LevelManager, to load a game in a script call:
LevelManager levelManager = DialogueManager.Instance.GetComponent<LevelManager>(); levelManager.LoadGame(s);
where 's' is a string containing your saved game data.
You'll need to adjust the first line if you've put the LevelManager component on a different GameObject.
For some important internal details, see Level Manager Details.
To save your game manually, call:
string s = PersistentDataManager.GetSaveData();
This implicitly calls PersistentDataManager.Record()
and then returns a string s
that represents the contents of the Lua environment. What you do with the string is up to you. The example scenes save it to PlayerPrefs. You could also write this to a local file or send it to a web-hosted MySQL database, but your project is responsible for implementing that part.
To load your game (without LevelManager), call:
PersistentDataManager.ApplySaveData(s);
where s
is the saved game data string.
<< Save System | Saved Game Data >>