Save/Load variables

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
thegamerguynz
Posts: 12
Joined: Mon Sep 19, 2016 8:34 pm

Save/Load variables

Post by thegamerguynz »

Hi guys,
I'm trying to get my head around the save/load scripting, all's going well except for one issue,
i need to store a list of inventory variables on save and restore them on load. how do i do this?

thanks,
Mike.
User avatar
Tony Li
Posts: 22062
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save/Load variables

Post by Tony Li »

Hi Mike,

Since I'm not sure where you're at in the process, I'll start from the top. Please feel free to skip down to where it's relevant for your situation.

If your inventory variables are defined in the Variables section of your dialogue database, they're automatically included in the save system's saved game data.

To tell the Dialogue System to record the current state of the game and return it as a string, call PersistentDataManager.GetSaveData():

Code: Select all

string s = PersistentDataManager.GetSaveData();
You can then save that string somewhere. The FeatureDemo.cs example scene (as well as the optional GameSaver component) save it to PlayerPrefs. If you're using the Menu Framework on the Dialogue System Extras page, you can enable its SaveToDisk script to save games to encrypted files on local disk.

To restore that data back into the Dialogue System, call PersistentDataManager.ApplySaveData(s):

Code: Select all

PersistentDataManager.ApplySaveData(s); 
You'll typically want to call GetSaveData() when saving a game and when leaving a scene that the player can return to. This records the state of the scene so you can restore it to that state when the player returns. If you're using the LevelManager component to switch scenes, it automatically takes care of saving and restoring the state info when changing scenes, as well as loading the correct scene when loading saved games.

By default, GetSaveData() only saves specific information from the Lua environment. This minimizes the size of saved games. You can tell the Dialogue System to include or exclude some commonly-used categories of data by expanding the Dialogue Manager's Persistent Data Settings foldout.

If you've defined new tables (arrays) in the Lua environment, such as an Inventory[] table, you can follow this example to register a PersistentDataManager.GetCustomSaveData() delegate.

If your data is elsewhere, such as in a C# script's variables, you'll want to write a persistent data component. You can find a starter template script in Scripts/Templates. Just copy it, uncomment, and customize. The Persistent Position Data component is a classic example of a persistent data component. When you call GetSaveData(), it records the GameObject's position in the Lua Actor[] table. When you call ApplySaveData(), it retrieves the position from the Actor[] table and moves the GameObject there. The third party integrations also use this technique heavily to save third party data.
Post Reply