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