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 ![]()
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);
}
}
}
