Page 1 of 1

Variable not saved

Posted: Mon Aug 21, 2023 11:44 am
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.

Re: Variable not saved

Posted: Mon Aug 21, 2023 12:05 pm
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.

Re: Variable not saved

Posted: Thu Sep 14, 2023 6:02 pm
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

Re: Variable not saved

Posted: Thu Sep 14, 2023 6:15 pm
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

Re: Variable not saved

Posted: Thu Sep 14, 2023 8:11 pm
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!

Re: Variable not saved

Posted: Thu Sep 14, 2023 8:42 pm
by Tony Li
Glad to help!