Here is the question on this bit of code. How come if i do just transform.position.x ect. it works. However when i try to reference to a scripts player object .postion.x ect. it give me a error. i figure they both would return a float.
Now for my next question. (see code below) how can I save a players position using this way of saving data? I understand that it can’t save players transform or even vector 3’s but I’ve heard it can be done in some roundabout way. I can’t seem to find any documentation on it. I also understand that it only saves serialize data form a class the user has made serializable. The code down below save / load health and mana.
public void Save() {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data.curnetHealth = health;
data.curentMana = mana;
bf.Serialize(file, data);
file.Close();
}
public void Load() {
if (File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
health = data.curnetHealth;
mana = data.curentMana;
}
What your looking for is a Surrogate for the Binary Formatter. This is just basically an extension you write to tell the Binary Formatter how to handle Vector3. You could follow the same pattern and write a Surrogate for Quaternions
Here is a thread in which I explain how to do this:
If this feels a little daunting you could create your own class that has all the data you want to save about a player. Then just take every Vector3, and Quaternion and spell them out as floats like this:
public class PlayerSave
{
public float posX;
public float posY;
public float posZ;
public float rotX;
public float rotY;
public float rotZ;
}
// Some Code on your PlayerController
void SavePlayer()
{
PlayerSave playerSave = new PlayerSave();
playerSave.posX = transform.position.x;
playerSave.posY = transform.position.y;
playerSave.posZ = transform.position.z;
Vector3 eulerAngles = transform.rotation.eulerAngles;
playerSave.rotX = eulerAngles.x;
playerSave.rotY = eulerAngles.y;
playerSave.rotZ = eulerAngles.z;
// Binary Formatter code goes here to format playerSave
}
Though the nice thing about using a surrogate is you write it once and you have it to serialize Vector3 for every project forever. The second approach requires your to rewrite the code every time.
Well, my save / load script is on my game manager gameObject that as a public static reference of its self. This way my player script can access it without having to findObject. Vice versa for the player script. The code here looks like it have to be on the player gameobject to get the transform.position. I was trying to pass in the players position to the save / load script with out the player being attached. Like how the player script access the health, mana. However, when I tried accessing the floats posX, Y, Z on the save / load script to put them into player transform they didn’t work. I hope im making scene here. coding is still new to me. im more of a 3d modeler and animator then doing the coding.
Surrogates are the way to go i think . YOu can probably just copy the code I wrote directly from the other thread to make it so Vector3 can be used in a binary formatter.