Lua Variables Persistence?

Announcements, support questions, and discussion for the Dialogue System.
VoodooDetective
Posts: 222
Joined: Wed Jan 22, 2020 10:48 pm

Lua Variables Persistence?

Post 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.
VoodooDetective
Posts: 222
Joined: Wed Jan 22, 2020 10:48 pm

Re: Lua Variables Persistence?

Post by VoodooDetective »

Do I need to save the game before switching scenes?
VoodooDetective
Posts: 222
Joined: Wed Jan 22, 2020 10:48 pm

Re: Lua Variables Persistence?

Post 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).
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Lua Variables Persistence?

Post 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);
VoodooDetective
Posts: 222
Joined: Wed Jan 22, 2020 10:48 pm

Re: Lua Variables Persistence?

Post 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()?
VoodooDetective
Posts: 222
Joined: Wed Jan 22, 2020 10:48 pm

Re: Lua Variables Persistence?

Post 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?
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Lua Variables Persistence?

Post 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.
VoodooDetective
Posts: 222
Joined: Wed Jan 22, 2020 10:48 pm

Re: Lua Variables Persistence?

Post 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.
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Lua Variables Persistence?

Post 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.
}
VoodooDetective
Posts: 222
Joined: Wed Jan 22, 2020 10:48 pm

Re: Lua Variables Persistence?

Post by VoodooDetective »

Ok yeah, maybe something like and event, OnSaveSystemLoadComplete or something.

Thanks for the suggestion!
Post Reply