Skip to main content

Saving and Loading Variables

EasyTalk provides an easy way to save and load variable values of a Dialogue during runtime if you want to maintain the Dialogue state across game restarts.

Saving Variable Values

To save variable values, just call the method SaveVariableValues() on the Dialogue Controller you want to save variable values for.

//To save local (dialogue-specific) variables
myDialogueController.SaveVariableValues();

//To save global variables
myDialogueController.SaveGlobalVariableValues();

You can provide a file name prefix to append to the JSON file which will be saved:

string playthroughID = " 12345";

//To save local (dialog-specific) variables
myDialogueController.SaveVariableValues(playthroughID);

//To save global variables
myDialogueController.SaveGlobalVariableValues(playthroughID);

In the above example, if the controller is set to use a Dialogue asset called 'mainStory', the JSON file for saved variable values will be '12345_mainStory.json'.

By default, variable values will be saved to a JSON file, but alternatively, you can save to PlayerPrefs instead by passing true as the second parameter to the SaveVariableValues method:

//Variable states are saved to PlayerPrefs instead of a JSON file.
string playthroughID = " 12345";

//To save local (dialog-specific) variables
myDialogueController.SaveVariableValues(playthroughID, true);

//To save global variables
myDialogueController.SaveGlobalVariableValues(playthroughID, true);

Loading Variable Values

Similar to saving variable values, you can load them using the LoadVariableValues method:

//To load local (dialogue-specific) variables
myDialogueController.LoadVariableValues();

//To load global variables
myDialogueController.LoadGlobalVariableValues();

You can also provide a prefix:

string playthroughID = " 12345";

//To load local (dialogue-specific) variables
myDialogueController.LoadVariableValues(playthroughID);

//To load global variables
myDialogueController.LoadGlobalVariableValues(playthroughID);

And you can load from PlayerPrefs instead of a JSON file:

string playthroughID = " 12345";

//To load local (dialogue-specific) variables
myDialogueController.LoadVariableValues(playthroughID, true);

//To load global variables
myDialogueController.LoadGlobalVariableValues(playthroughID, true);