Page 1 of 1

Working with multiple scenes

Posted: Wed Dec 21, 2022 8:15 pm
by Rasenjon
So, say I have a player prefab that gets instantiated at the start of the first level. This prefab is don't destroy on load, so it carries between scenes. In the first level, I have an object that acts as a save point. The player can interact with it to save or load. This works fine. The problem arises when the player leaves the scene and comes back. So say you leave level 1, enter level 2, then return to level 1. The save point no longer works. I'm still pretty fresh to the concept of saving and loading, so it could very well be an oversight on my part. Also, I'm using the auto save manager with the save event and load event set to NONE, as I only want the player to be able to save at the specified save point. Thanks in advance!

Thought I should add, I'm using ES3AutoSaveMgr.Current.Save and ES3AutoSaveMgr.Current.Load on my save point.

Re: Working with multiple scenes

Posted: Thu Dec 22, 2022 11:59 am
by Joel
Hi there, and thanks for getting in contact.

Auto Save is intended to save GameObjects/Components in specific scenes. As an object marked as DontDestroyOnLoad doesn't belong to any persistent scenes, it wouldn't be possible for Auto Save to save and load it.

In this case you would need to use code. For example instead of calling ES3AutoSaveMgr.Current.Save/Load, you would call your own Save and Load method, each of which might look something like this:

Code: Select all

public void Save()
{
	ES3.Save("playerTransform", player.transform);
	ES3.Save("customVariable", player.GetComponent<MyCustomScript>().myVariable);
	// Etc ...
}

public void Load()
{
	if(ES3.FileExists())
	{
		// Note: ES3.LoadInto loads the data into your existing Transform rather than creating a new one.
		ES3.LoadInto("playerTransform", player.transform);
		player.GetComponent<MyCustomScript>().myVariable = ES3.Load<MyVariableType>("customVariable");
		// Etc ..
	}
}
All the best,
Joel

Re: Working with multiple scenes

Posted: Fri Dec 23, 2022 11:57 pm
by Rasenjon
Thanks for the reply! I managed to get it working :) I've been stuck on another issue though...

So some scenes have treasure chests with a Chest script on each of them, which has a bool called Open to dictate if the chest has been opened or not. I was wondering how I would save the status of that bool for each chest? I've tried using a for loop to cycle through all of the chests in the given scene, and saving the bool value, but I believe the key is being overwritten, as there are multiple instances of the chest script. Hopefully you can understand my explanation, haha

Re: Working with multiple scenes

Posted: Sat Dec 24, 2022 9:21 am
by Joel
Hi there,

You would need to generate a unique ID for each Chest and use this as the key. The easiest way to create a unique ID would be to declare a class variable in your Chest class as follows:

Code: Select all

public string id = System.Guid.NewGuid().ToString();
Alternatively if the Chest script exists in the scene prior to runtime (I.e. it’s not created at runtime or belongs to a prefab which is instantiated at runtime), you could save and load the Chest scripts themselves (or an array/List of them), which will load them by reference and value.

All the best,
Joel

Re: Working with multiple scenes

Posted: Sat Dec 24, 2022 12:54 pm
by Rasenjon
Got it! I think I'm getting the hang of the way all of this works. Thanks for your help! :)