Page 1 of 1

Get Variables in Unity Start() method

Posted: Sun Jul 30, 2023 5:55 pm
by Poliman
Hi,

I use "SaveSystem.LoadFromSlot(1);"
and in the loaded scene want to do operations on DS Variables in Unity Start() method.

The variable is for ex. "MyVar" with initial to false.
in the game MyVar is changed to true with
DialogueLua.SetVariable("MyVar", true);
then saved, when next time game is DS Loaded and in the loaded scene from save, checking MyVar in Start() with
DialogueLua.GetVariable("MyVar").asBool > get false
But with afterwards method inside game gets true.
So my guess is in Start() variables are not Loaded from save, but some script updates it later.

Am I right? if so what is the workaround?

Re: System hangs on conversation START

Posted: Sun Jul 30, 2023 8:30 pm
by Tony Li
Hi,

Have you set up the full save system? The Dialogue System Saver will restore the DS variable values as soon as the saved game is loaded.

Other savers, however, may not be restored yet when the Start() method runs. To know when all of the savers have been restored, hook into the C# event SaveSystem.saveDataApplied. Example:

Code: Select all

SaveSystem.saveDataApplied += OnSaveDataApplied;
SaveSystem.LoadFromSlot(1);
...
void OnSaveDataApplied()
{
    SaveSystem.saveDataApplied -= OnSaveDataApplied; //[EDIT: Fixed typo]
    // At this point I know that all saved data has been restored.
}

Re: Get Variables in Unity Start() method

Posted: Mon Jul 31, 2023 5:17 am
by Poliman
Well I think all is setup according to help

I use PlayerPrefs data storer

Debug log

_ in Start () GetVariable:False _
_ end of Start method _
_ Update 1 frame _
_ OnSaveDataApplied _
_ in OnSaveDataApplied GetVariable:True _

So after one frame event is called, is it ok then and i cannot set it up to make it work in Start()?

Next qtn why do You use "SaveSystem.saveDataApplied += OnSaveDataApplied;" inside OnSaveDataApplied?

Re: Get Variables in Unity Start() method

Posted: Mon Jul 31, 2023 7:53 am
by Tony Li
Hi,

sorry, "+=" was a typo. I fixed it to "-=" to unhook from SaveSystem.saveDataApplied.

If your Save System component's Frames To Wait Before Apply Data is 1, then waiting 1 frame may work. However, it's better to use SaveSystem.saveDataApplied.