Get Variables in Unity Start() method

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Poliman
Posts: 4
Joined: Sun Jul 30, 2023 12:05 pm

Get Variables in Unity Start() method

Post 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?
Last edited by Poliman on Mon Jul 31, 2023 1:54 am, edited 1 time in total.
User avatar
Tony Li
Posts: 22119
Joined: Thu Jul 18, 2013 1:27 pm

Re: System hangs on conversation START

Post 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.
}
Poliman
Posts: 4
Joined: Sun Jul 30, 2023 12:05 pm

Re: Get Variables in Unity Start() method

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

Re: Get Variables in Unity Start() method

Post 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.
Post Reply