Page 1 of 2
Lua Variables Persistence?
Posted: Tue Apr 21, 2020 7:27 pm
by VoodooDetective
I just realized that when I'm setting a variable, it's not persisting between scenes:
Code: Select all
DialogueLua.SetVariable(LuaConstants.CemeteryWrothFamilyOrder, 1); // original value -1
Then later in another scene:
Code: Select all
int familyOrder = DialogueLua.GetVariable(LuaConstants.CemeteryWrothFamilyOrder).asInt; --> returns -1
familyOrder will be set to its original value instead of the value from SetVariable. I just want to make sure I'm not expecting cross-scene persistence when that's not a thing. I'm sure it is, I just wanted to sanity check.
Re: Lua Variables Persistence?
Posted: Tue Apr 21, 2020 7:48 pm
by VoodooDetective
Do I need to save the game before switching scenes?
Re: Lua Variables Persistence?
Posted: Tue Apr 21, 2020 8:26 pm
by VoodooDetective
Did some more testing and found that:
Code: Select all
DialogueLua.SetVariable(LuaConstants.CemeteryWrothFamilyOrder, 1);
variableTable = Lua.Run("return Variable").AsTable;
variableTable will contain the correct value.
BUT then as the scene changes, in PersistentDataManager, when it's called again on line 314:
Code: Select all
LuaTableWrapper variableTable = Lua.Run("return Variable").AsTable;
variableTable will have the uninitialized value (-1) instead of what I set it to (1).
Re: Lua Variables Persistence?
Posted: Tue Apr 21, 2020 8:48 pm
by Tony Li
Hi,
On your Dialogue Manager, are the Don't Destroy On Load and Allow Only One Instance checkboxes ticked? (The default is for them to be ticked.)
If not, you'll need to save and load. If you're using the save system, add a Dialogue System Saver component to the Dialogue Manager, and change scenes using SaveSystem.LoadScene(). If you're not using the save system, use PersistentDataManager.GetSaveData() and ApplySaveData():
Code: Select all
string s= PersistentDataManager.GetSaveData();
SceneManager.LoadScene("New Scene");
PersistentDataManager.ApplySaveData(s);
Re: Lua Variables Persistence?
Posted: Tue Apr 21, 2020 9:05 pm
by VoodooDetective
I've got my Dialogue Manager setup with:
- Don't Destroy On Load
- Allow Only One Instance
- Dialogue System Saver
I'm using SaveSystem.LoadScene() to change scenes.
I did some more testing and the behavior is actually stranger than I first thought. The variable is dropped two frames after after when it's set. I'm printing it out in update and after two lookups where it IS the correct value, it goes back to the uninitialized state.
Am I not allowed to SetVariable in Start()?
Re: Lua Variables Persistence?
Posted: Tue Apr 21, 2020 9:26 pm
by VoodooDetective
OK so if i set the variable several frames after update the value sticks around as it should. I turned on Info logging and saw this guy:
Code: Select all
Dialogue System: Resetting Lua environment.
UnityEngine.Debug:Log(Object)
PixelCrushers.DialogueSystem.PersistentDataManager:ApplySaveData(String, DatabaseResetOptions) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/Save System/PersistentDataManager.cs:242)
PixelCrushers.DialogueSystem.DialogueSystemSaver:ApplyData(String) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/Save System/DialogueSystemSaver.cs:70)
PixelCrushers.SaveSystem:ApplySavedGameData(SavedGameData) (at Assets/Plugins/Pixel Crushers/Common/Scripts/Save System/SaveSystem.cs:585)
PixelCrushers.<LoadSceneCoroutine>d__94:MoveNext() (at Assets/Plugins/Pixel Crushers/Common/Scripts/Save System/SaveSystem.cs:694)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr) (at /Users/builduser/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:187)
So it seems like when the scene loads, it's destroying any changes made soon after load?
Re: Lua Variables Persistence?
Posted: Tue Apr 21, 2020 9:34 pm
by Tony Li
Yes. This allows components that initialize in Start() to initialize themselves before the save system applies their saved states. You can reduce the number of frames (even down to zero) by inspect the Save System component and setting Frames To Wait Before Apply Data.
Re: Lua Variables Persistence?
Posted: Tue Apr 21, 2020 9:39 pm
by VoodooDetective
Oh shoot haha. So if I want to initialize an object based on save game data, I should wait that number of frames before trying?
I'm super glad I found this out early! Thanks for explaining that, I must have missed it.
Re: Lua Variables Persistence?
Posted: Tue Apr 21, 2020 9:47 pm
by Tony Li
Exactly. If you want to initialize based on Dialogue System internal data (variables, quests, etc.), you could always make a subclass of DialogueSystemSaver and override the ApplyData() method:
Code: Select all
public override void ApplyData(string s)
{
base.ApplyData(s);
// Initialize your data here.
}
Re: Lua Variables Persistence?
Posted: Tue Apr 21, 2020 10:01 pm
by VoodooDetective
Ok yeah, maybe something like and event, OnSaveSystemLoadComplete or something.
Thanks for the suggestion!