Hello everybody!
Can you help me with something, please?
I am working on the saving system for a 2D platforming game.
It can save the gameĀ“s variable values in a few different situations, for example when picking a special item, killing a bossā¦
But there is a situation that I think can provoke errors in the future, depending on which machine it is running on. That is when exiting the stage at the same time that it saves the game.
I have scripted a delay of 1-2 seconds before the Scene is loaded, so it gives time to write all the data and it is not lost when loading the Scene, or even corrupted, but i think it might not be enough in certain computers, or maybe it is too much and the Scene could load faster in other computersā¦
Is there any method to know when a data writing has ended (safely)?
Well, in this case, when the character touches the collider, a public variable will be set to true (saveGame = true), so the saving system begins to save the variables:
if (collision.gameObject.tag == "ExitToMap")
{
saveGame = true;
if (collision.GetComponent<ExitToMap_Controller>().stageToUnlock == 1)
{
stage1InPocket = 1;
}
}
Also, a coroutine starts, so the scene loading begins after 1 second.
On another gameobject I have a script with all the variables to be saved (this is the one that reads the public variable saveGame = true to begin the saving operation).
The variables from the public class ninjaData that need to be changed are changed during the gameplay, and when the saving operation begins the following function executes:
This is synchronous I think? Anyway, I would need to know when the operation has finished so I“m sure all the variables have the new value when the next scene is loaded.
Yes this all runs on the main thread. When SaveNinjaData() returns all data has been saved. You can even yield return null in the coroutine to just wait for the next frame, or just call LoadScene after saving. Actually the latter is more advisable, even though it may be rare there could be circumstances where game state changes after saving and before loading scene such that, for example, the Ninja dies and you load the next scene but start with a dead ninja.
These are the kind of potential bugs youāll want to avoid because theyāre difficult to track down, and happen rarely so you never really know what could happen.