Hello, it seems like this shouldn't be too difficult, but I've been looking through the documentation and couldn't find quite what I was looking for. Can you tell me the proper way to have a variable's value carry over between scenes in Unity?
Specifically, I have a boolean variable called "hasSword" which I created in the DialogueSystem UI and has a default value of False. When my Town scene starts the hero gets a small quest to fetch the sword. The hero finds the sword in a different scene, the House scene, and the boolean gets set to True via a LuaTrigger component. When I return to the original Town scene the variable is no longer set, so the quest is not able to be completed. If I keep the sword and the trigger in the Town scene and don't switch scenes everything works fine. So, what is the proper way to keep that variable's value across scenes?
Thanks in advance for your time and apologies if I am overlooking something simple!
Gene
Proper way to have variables carry over in to different scenes? [SOLVED]
-
- Posts: 8
- Joined: Tue May 10, 2016 3:21 pm
Proper way to have variables carry over in to different scenes? [SOLVED]
Last edited by VolcanoBean on Tue Nov 29, 2016 10:30 pm, edited 1 time in total.
Re: Proper way to have variables carry over in to different scenes?
Hi Gene,
This reply assumes you're talking about variables defined in your dialogue database -- for example, on the Variables tab of the Dialogue Editor window. If you're talking about a different type of variable, please let me know.
Runtime variable values are stored in Lua. Each Dialogue Manager maintains its own instance of Lua.
By default, the Dialogue Manager persists across scene changes (because its Don't Destroy On Load checkbox is ticked by default). This means variable values should also persist across scene changes. This is normally the way you want to leave it configured.
Also by default, the Dialogue Manager ensures that only one copy exists in a scene (because its Allow Only One Instance checkbox is ticked by default). This allows you to add a "test" Dialogue Manager to every scene so you can playtest that scene in the Unity editor without having to come in from the main menu scene. But when you do come in from the main menu scene, the main menu scene's Dialogue Manager persists and gets rid of the test Dialogue Manager. This ensures that there's only one Dialogue Manager and that it has the current state of all variables.
If you've changed these checkboxes and are using a new Dialogue Manager for every scene, you'll need to manually save the old scene's Lua data before leaving the old scene, and then apply it to the new scene after entering the new scene. To save the old scene's data:
To apply it to the new scene:
BTW, you can check the values of variables at runtime by using the Dialogue Editor's Watches tab or by adding a Lua Console component to the scene.
This reply assumes you're talking about variables defined in your dialogue database -- for example, on the Variables tab of the Dialogue Editor window. If you're talking about a different type of variable, please let me know.
Runtime variable values are stored in Lua. Each Dialogue Manager maintains its own instance of Lua.
By default, the Dialogue Manager persists across scene changes (because its Don't Destroy On Load checkbox is ticked by default). This means variable values should also persist across scene changes. This is normally the way you want to leave it configured.
Also by default, the Dialogue Manager ensures that only one copy exists in a scene (because its Allow Only One Instance checkbox is ticked by default). This allows you to add a "test" Dialogue Manager to every scene so you can playtest that scene in the Unity editor without having to come in from the main menu scene. But when you do come in from the main menu scene, the main menu scene's Dialogue Manager persists and gets rid of the test Dialogue Manager. This ensures that there's only one Dialogue Manager and that it has the current state of all variables.
If you've changed these checkboxes and are using a new Dialogue Manager for every scene, you'll need to manually save the old scene's Lua data before leaving the old scene, and then apply it to the new scene after entering the new scene. To save the old scene's data:
Code: Select all
string s = PersistentDataManager.GetSaveData();
Code: Select all
PersistentDataManager.ApplySaveData(s);
-
- Posts: 8
- Joined: Tue May 10, 2016 3:21 pm
Re: Proper way to have variables carry over in to different scenes?
You are correct, I am referring to a variable I created in the dialogue database. Thank you for all the info. I will check it out and see if this clears things up.
Thanks so much!
Gene
Thanks so much!
Gene
-
- Posts: 8
- Joined: Tue May 10, 2016 3:21 pm
Re: Proper way to have variables carry over in to different scenes?
That cleared up so many things I didn't understand! I had 'Don't Destroy On Load' unchecked and being able to use the Watch tab is so helpful. Thank you again for your help with this!
Gene
Gene
Re: Proper way to have variables carry over in to different scenes? [SOLVED]
Happy to help!
BTW, if you ever need to check variables in a build (vs. the Unity editor), you can add a Lua Console to your scene.
BTW, if you ever need to check variables in a build (vs. the Unity editor), you can add a Lua Console to your scene.