Saving custom global data

Whenever Adventure Creator saves and loads games, it checks to see if certain "script hooks" are present, and calls them if they are. These hooks can be used to save and load custom data your game might need. Note that this is an advanced topic, so knowledge of scripting interfaces is assumed!

While saving scene data is a separate process, these hooks are intended for global data, which exists outside of a scene - for example, in an asset file. In this tutorial, we’ll create a simple example that involves a person’s name being saved.

In our save game file, this name will be temporarily stored as a Global Variable, so the first step is to create one that can be used by our hook. Since a name is a string, we’ll create a string Variable. Go to the Variables Manager, and click Create new Global variable.

Set this new variable’s Label to SaveText, and it’s Type to String. It’s initial value is irrelevant, but make a note of it’s ID number. In this case, it’s 3:

Now let’s create our script hooks. Create a new C# script file called MyName, and have it import the AC namespace:

using UnityEngine;
using System.Collections;
using AC;

public class MyName : MonoBehaviour
{

}

Declare a public string called myOwnName. In practice, this might reference a custom asset, but this example will allow us to view and edit this easily in the Inspector. We'll also need an integer varID to store the ID number of the Global Variable above.

using UnityEngine;
using System.Collections;
using AC;

public class MyName : MonoBehaviour
{

	public string myOwnName;
	public int varID;


}

We'll write this script variable's value to our Global Variable when we save, and retrieve it from there when we load. To do this, we'll make use of the OnBeforeSaving and OnAfterLoading custom events. For more on custom events, see this tutorial.

using UnityEngine;
using System.Collections;
using AC;

public class MyName : MonoBehaviour
{

	public string myOwnName;
	public int varID;

	private void OnEnable ()
	{
		EventManager.OnBeforeSaving += BeforeSave;
		EventManager.OnFinishLoading += AfterLoad;
	}

	private void OnDisable ()
	{
		EventManager.OnBeforeSaving -= BeforeSave;
		EventManager.OnFinishLoading -= AfterLoad;
	}

	private void BeforeSave (int saveID)
	{}

	private void AfterLoad (int saveID)
	{}

}

In BeforeSave, we'll write the value of myOwnName to the AC Global Variable:

GlobalVariables.SetStringValue (varID, myOwnName);

And in AfterLoad, we'll update the value of myOwnName from the AC Global Variable:

myOwnName = GlobalVariables.GetStringValue (varID);

(See the Scripting Guide for a list of functions available when accessing global variables.)

And the complete script:

using UnityEngine;
using System.Collections;
using AC;

public class MyName : MonoBehaviour
{

	public string myOwnName;
	public int varID;


	private void OnEnable ()
	{
		EventManager.OnBeforeSaving += BeforeSave;
		EventManager.OnFinishLoading += AfterLoad;
	}


	private void OnDisable ()
	{
		EventManager.OnBeforeSaving -= BeforeSave;
		EventManager.OnFinishLoading -= AfterLoad;
	}


	private void BeforeSave (int saveID)
	{
		GlobalVariables.SetStringValue (varID, myOwnName);
	}


	private void AfterLoad (int saveID)
	{
		myOwnName = GlobalVariables.GetStringValue (varID);
	}

}

We now just need to ensure that this script is present in all of our scenes. There are a few ways to do this:

  • Make it a prefab and place an instance of the prefab in each scene
  • Update the script to mark it with DontDestroyOnLoad
  • Attach it to AC's PersistentEngine, which automatically survives scene changes

To do the latter, find the PersistentEngine prefab in Assets/AdventureCreator/Resources. Add the MyName component to the bottom of it’s component stack:

Our myOwnName string will now be stored and retrieved in save games. You can test this by entering a name into the Inspector’s exposed field, saving, changing the name, and loading back. Note that the PersistentEngine prefab is carried over scene changes - meaning that any variables stored in our script will survive a scene change.