public static saveInformation loadData() { string path = Application.persistentDataPath + "/player.info"; if (File.Exists(path)) //if it exists, go to the path and open file, deserialize it and save it as a saveinformation { BinaryFormatter formatter = new BinaryFormatter(); FileStream stream = new FileStream(path, FileMode.Open); //opens file if (stream.Length != 0) { saveInformation info = formatter.Deserialize(stream) as saveInformation; //casting the data into a saveinformation object stream.Close(); //don't forget to close the streamreader return(info); } else { stream.Close(); //need this in 2 places to avoid errors return(null); } } else //temp solution: bundle project with file included that has starting position and seed count etc { //if it doesn't exist or it's their first time playing, might have to adjust this later Debug.LogError("Save file not found"); return(null); } }
public void loadInfo() //unpacks player info and sets various variables equal to what they should be { saveInformation stats = saveSystem.loadData(); if (stats != null) //make sure they HAVE a save file or you can break the game if you want { if (follow != null) { follow.isMale = stats.male; } seedArray = stats.seeds; produceArray = stats.produce; money = stats.money; InteractionController.inTown = stats.town; Vector3 playerPosition = new Vector3(stats.position[0], stats.position[1], stats.position[2]); GameObject.FindGameObjectWithTag("Player").gameObject.transform.position = playerPosition; int index = 0; foreach (int num in seedArray) //setting all seeds to where they should be { if (seedText[index] != null) { seedText[index].text = seedText[index].text.TrimEnd(numbers) + seedArray[index]; //removing number of seeds from both seeds and produce, then adding new number to end of string index++; } } index = 0; foreach (int num in produceArray) //adjusting produce text { if (produceText[index] != null) { produceText[index].text = produceText[index].text.TrimEnd(numbers) + produceArray[index]; index++; } } if (moneyText != null) { moneyText.text = moneyText.text.TrimEnd(numbers) + money; } } else //I had this, and then I removed it for no reason, don't get rid of this. If the user is a new player it gives them 3 seeds a piece { for (int i = 0; i < seedArray.Length; i++) { seedArray[i] = 3; } foreach (int num in seedArray) //setting all seeds to where they should be { int index = 0; if (seedText[index] != null) { seedText[index].text = seedText[index].text.TrimEnd(numbers) + seedArray[index]; //removing number of seeds from both seeds and produce, then adding new number to end of string index++; } } } }
public static void savePlayer(followPlayer camera, playerController controller, playerInventory inventory, interactionController interaction) { BinaryFormatter formatter = new BinaryFormatter(); string path = Application.persistentDataPath + "/player.info"; //creating path and filename, extension doesn't matter, persistent path is universal across OS FileStream stream = new FileStream(path, FileMode.Create); //creates file at path given saveInformation information = new saveInformation(camera, controller, inventory, interaction); //creating the save information formatter.Serialize(stream, information); //turning information into binary stream.Close(); }