Saving Data - How to know when a writing data process has ended?

Hello everybody!:slight_smile:
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)?

Thanks in advance!!

How do you perform the save operation? (post relevant code snippets)
Unless you save asynchronously you needn’t worry about this.

Thanks!:slight_smile:

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.

private void OnTriggerEnter2D(Collider2D collision)
    {        
        if (collision.gameObject.tag == "Ninja")
        {
            StartCoroutine("ExitToMap");           
        }
    }

    IEnumerator ExitToMap()
    {
        yield return new WaitForSecondsRealtime(1);
        SceneManager.LoadScene("Map");
    }

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:

public void SaveNinjaData()
    {
        if (chosenSlotNum == 1)
        {
            string ninjaData_Contents = JsonUtility.ToJson(ninjaData, true);
            System.IO.File.WriteAllText(ninjaData_Path1, ninjaData_Contents);
        }

        if (chosenSlotNum == 2)
        {
            string ninjaData_Contents2 = JsonUtility.ToJson(ninjaData, true);
            System.IO.File.WriteAllText(ninjaData_Path2, ninjaData_Contents2);
        }

        if (chosenSlotNum == 3)
        {
            string ninjaData_Contents3 = JsonUtility.ToJson(ninjaData, true);
            System.IO.File.WriteAllText(ninjaData_Path3, ninjaData_Contents3);
        }
    }

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. :wink:

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.

Calling LoadScene after saving would do just fine!
Thank you very much, you helped me a lot!:slight_smile: