How to Set Up the Save System

How Saved Games Work

Quick Link: Save System Quick Start Steps

A saved game contains:

  • The current state of the dialogue database (e.g., quest states, which dialogue entries have been visited, etc.)
  • Any data that GameObjects have recorded into the Lua environment by means of "persistent data" components (described further down on this page).

Saving consists of two operations:

  1. Recording the current state of GameObjects into the Lua environment via "persistent data" components, and
  2. Getting the Lua environment as a savegame string, which can be saved in PlayerPrefs or handled however you want, such as writing to a file on disk, or saving to a database.

Loading consists of two similar operations:

  1. Retrieving a savegame string (e.g., from PlayerPrefs, file, or database) and applying it to the Lua environment, and
  2. Applying the Lua environment to the GameObjects via "persistent data" components.

The Dialogue System provides built-in support for saving the savegame string to PlayerPrefs. Other save destinations are up to you.


How Persistent Scenes Work

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.


Save System Quick Start Steps

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.

  1. Add Persistent Data Components to GameObjects in the scene.
  2. Add the Level Manager component to your Dialogue Manager GameObject.
  3. If you plan to use the Game Saver component, add it to any appropriate GameObject. The Game Saver provides a way to save and load without scripting.

How To Change Scenes

Change Scenes with Level Manager

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.

Change Scenes with PersistentDataManager

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.

How to Save and Load Games

Save and Load with LevelManager

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.

Manual Save/Load Method with PersistentDataManager

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