Variable not saved

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Kamotachi
Posts: 44
Joined: Fri Nov 29, 2019 9:03 pm

Variable not saved

Post by Kamotachi »

Hi!
I'm usign the Save system.

My problem is that all the database variables are saved, except one, called "UnlockedStages".

It is strange, before saving I add +1 to this variable in the following way:

if(DialogueLua.GetVariable("UnlockedStage").AsInt < GameManager.Instance.currentStage + 1)
{
DialogueLua.SetVariable("UnlockedStage", (GameManager.Instance.currentStage + 1));
print("You unlocked the level : " + (DialogueLua.GetVariable("UnlockedStage").AsInt));
}

That print shows the correct number on the console. If it was 0, it is now 1. If it was 1, it is now 2. So the addition works.

Then I save with the function saveSystemMethods.SaveSlot(1);. And it works, since the rest of the variables in the database are saved. Even, I've added another print to check if it's saved or not, like this:

public void SaveData()
{
_saveSystemMethods.SaveSlot(1); //Save in Slot 1
print("Saved, now unlockedStage is : " + DialogueLua.GetVariable("UnlockedStage").AsInt);
}

That print shows the correct result in the console . So the current value should have been saved in that variable.

The problem comes next. In the next frame, the UnlockedStage variable is reset to 0. I have verified it by saving the game a second later (with an Invoke), and also looking at the generated file in "PlayerPrefs Saved Game Data", where it shows that everything has been saved. , but UnlockedStages is 0.

It's strange because the console shows the correct number in a print after saving. However it returns to 0.
It really seems like it has to be a mistake I'm making with my code, but nowhere else am I using that variable.
User avatar
Tony Li
Posts: 21676
Joined: Thu Jul 18, 2013 1:27 pm

Re: Variable not saved

Post by Tony Li »

There must be some code somewhere in your project that's resetting it, or a Dialogue System Trigger that's running Lua code, or a conversation's Script method. Try temporarily setting Debug Level to Info. This will log when Lua code is run, which might help you identify what's doing it. It won't catch C# calls to DialogueLua.SetVariable(), though, if that's what's doing it.
Kamotachi
Posts: 44
Joined: Fri Nov 29, 2019 9:03 pm

Re: Variable not saved

Post by Kamotachi »

Hi again!

I thought I had solved it by unchecking the 'Save Across Scene Changes' option in the 'Dialogue System Saver' component, but it seems the issue persists.

I've found out that the 'UnlockedStage' variable from the DataBase resets to 0 after saving the game with _saveSystemMethods.SaveSlot(1); but it's very strange because it depends on the scene where I save the game.

If I save on Start method of "Scene A", save and works nice.
If I save after press a button on "Scene B", the variable "UnlockedStage" goes to 0.
Does this make any sense? I really don't know where to go from here! XD
User avatar
Tony Li
Posts: 21676
Joined: Thu Jul 18, 2013 1:27 pm

Re: Variable not saved

Post by Tony Li »

SaveSystemMethods.SaveSlot() won't change variable values. I'm betting it's happening at some point priot to clicking the button in Scene B.

Keep the Dialogue Editor window's Watches section open with auto-refresh on and a watch on your "UnlockedStage" variable. As you play, watch it to see when the variable changes value.

You can also temporarily set the Dialogue Manager's Other Settings > Debug Level to Info. This will log Dialogue System activity to the Console window. You may see a log indicating when and why the value is changing.

More info about both of these: Video: Logging & Debugging Tips
Kamotachi
Posts: 44
Joined: Fri Nov 29, 2019 9:03 pm

Re: Variable not saved

Post by Kamotachi »

Finally!!! THANK YOU SO MUCH!!!

I had the "SaveAcross Scene Changes" button unchecked, and "Frames To Wait Before Apply Data" set to "1".

All variables were reset and the game was saved immediately afterwards.

Thank you! This is solved!
User avatar
Tony Li
Posts: 21676
Joined: Thu Jul 18, 2013 1:27 pm

Re: Variable not saved

Post by Tony Li »

Glad to help!
Post Reply