Odd Issue with Saving and Loading

Hi all,

Sorry for the huge post, but I am having a very odd issue with my saving and loading and I was hoping that someone may see where I may be going wrong. I just cant spot the issue in the code :frowning:
I will try my best to explain though its a bit confusing.

Here is what the system should be doing. I have 3 sets of floating variables, plr_x, plr_y, plr_z…I break down the players current position into these. My SaveShip function saves these to the serialized file.

When I load the game, it loads these saved x, y, z floats into saved_x, saved_y, saved_z floats and then it kicks off another function that will load the prefab model of the ship and set the ships position to these saved values. In my mind, a freshly loaded game would then have both the plr_ and saved_ floats be the exact same.

Whats currently happening, saving will save the current coordinates properly into the Saved_ floats but then loading seems to load the prev Plr_ floats and not the Saved_ floats…(if that makes sense).

Here is a picture of the before and after. The first Plr_ variables should be saved, the first set of Saved_ floats should be wiped out completely upon loading the same after a save. instead they game is still loaded with those yet they successfully have the saved ones

Now here is the code. The order of things currently is that I have a Data Controller script that has the SavePlayer file. At the end of SavePlayer I kick off the SaveShip() function…likewise, in the LoadPlayer function at the end I kick off the LoadShip() function

public void SaveShip()
    {
        if (!Directory.Exists(Application.persistentDataPath + "/" + gameData.PlayerName + "/" + "Ships"))
        {
            //Create directory if it doesnt already exist
            Directory.CreateDirectory(Application.persistentDataPath + "/" + gameData.PlayerName + "/" + "Ships");
        }
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(Application.persistentDataPath + "/" + gameData.PlayerName + "/" + "Ships" + "/" + shipName + ".dat");
        Ship data = new Ship();

data.ship_x = plr_x;
            data.ship_y = plr_y;
            data.ship_z = plr_z;

bf.Serialize(file, data);
        file.Close();
    }

public void LoadShip()
    {       
        if (File.Exists(Application.persistentDataPath + "/" + gameData.PlayerName + "/" + "Ships" + "/" + shipName + ".dat"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/" + gameData.PlayerName + "/" + "Ships" + "/" + shipName + ".dat", FileMode.Open);
            Ship data = (Ship)bf.Deserialize(file);

saved_x = data.ship_x;
            saved_y = data.ship_y;
            saved_z = data.ship_z;

file.Close();

            Scene currentScene = SceneManager.GetActiveScene();
            string sceneName = currentScene.name;

            LoadshipPrefab();

}
}

And the LoadPrefab function is

public void LoadshipPrefab()
    {
        foreach (Shiptemplate ship in itemdata.ShipTemplates)
        {
            if (ship_ID == ship.ID)
            {
                var prefab = ship.Prefab;
                if (playership != null)
                {
                    Destroy(playership);
                }
                Instantiate(prefab);
                prefab.transform.position = new Vector3(saved_x, saved_y, saved_z);
            }
        }
    }

I don’t see any Debug.Logs in that code. Debug.Log is your best friend; it will check your assumptions and tell you what’s really going on.

So, start with that — log each step, with both what’s happening and what the relevant values are. Then just study the logs. I bet you’ll find that things aren’t happening in the order you think they are, or something like that.

Thanks, I will give that a try this evening and check out the order of things. I woke up this morning thinking that perhaps it is some sort of timing issue with how fast the files are loaded compared to how fast the scene is loaded. The current steps are;

  1. Load Player data from DataController script
    a. At the end of Player Data function is a call to the ShipData to load
    b. At the end of the ShipData it then calls the load prefab function
  2. LoadScene

I need to put in a proper loading screen and loadlevelasync to make sure the player is fully loaded, ship is loaded, and ship is positioned before starting the scene anyways, it may also help or narrow down the issue as well. It just seems strange that it appears the Save is working properly but that loadprefab function is somehow reading the previous save instead of the current save file ( I also checked for multiple save files but there is only one)

Finally got my proper loading scene finished but it didn’t fix the issue. I finally found the error however, and it was a typo in the LoadshipPrefab() function. If you see my post above I was trying to set the position on the prefab variable I was instantiated instead of setting the position on the item I Instantiated. I am surprised it gave me these confusing results instead of producing an error but here the correct code

public void LoadshipPrefab()
    {
        Debug.Log("Loaded Prefab and Position");
        foreach (Shiptemplate ship in itemdata.ShipTemplates)
        {
            if (ship_ID == ship.ID)
            {
                var prefab = ship.Prefab;
                if (playership != null)
                {
                    Destroy(playership);
                }
                var loadedship = Instantiate(prefab);
                Debug.Log("Set Position Cords: x:" + saved_x + " y:" + saved_y + " z:" + saved_z);
                loadedship.transform.position = new Vector3(saved_x, saved_y, saved_z);
            }
        }
    }
1 Like

Good job tracking that down!