Presentation is loading. Please wait.

Presentation is loading. Please wait.

Save and Load Systems Richard Gesick.

Similar presentations


Presentation on theme: "Save and Load Systems Richard Gesick."— Presentation transcript:

1 Save and Load Systems Richard Gesick

2 Objectives Save data to a flat file Load data from a flat file
Customize our flat file Save data to an XML file Load data from an XML file Implement a checkpoint-based system Implement an anywhere/anytime saving system

3 flat/text files StreamWriter for writing files
StreamReader for reading files Same basic commands as in Console’s ReadLine and WriteLine Need using System.IO; and using System.Text; Application.dataPath Application.persistentdataPath

4 Saving a text file void WriteToFile(string file = "") {
if(file != "") //good or even necessary? sFileName = file; if(File.Exists(sDirectory + sFileName)) DeleteFile(sFileName); } using(StreamWriter sw = new StreamWriter(sDirectory + sFileName)) sw.WriteLine(PlayerPrefs.GetInt("PlayerKills").ToString()); sw.WriteLine(PlayerPrefs.GetInt("PlayerDeaths").ToString()); sw.WriteLine(PlayerPrefs.GetInt("PlayerTotalGold").ToString()); sw.WriteLine(Player.transform.position.x.ToString()); sw.WriteLine(Player.transform.position.y.ToString()); sw.WriteLine(Player.transform.position.z.ToString());

5 Reading flat/text files
void ReadFile(string file = "") { if(file != "") sFileName = file; using(StreamReader sr = new StreamReader(sDirectory + sFileName)) int kills = Convert.ToInt32(sr.ReadLine()); int deaths = Convert.ToInt32(sr.ReadLine()); … float x = Convert.ToSingle(sr.ReadLine()); float y = Convert.ToSingle(sr.ReadLine()); float z = Convert.ToSingle(sr.ReadLine()); Player.transform.position = new Vector3(x, y, z); }

6 Using streamreader/streamwriter
Creates an instance of StreamReader to read from a file. The using statement also closes the StreamReader. using (StreamReader sr = new StreamReader("TestFile.txt")) But does not incorporate exception handling. Still need to include try & catch blocks to handle errors

7 Handling file not found and other file errors
If you have a reset: consider having the catch call the reset method, call the write file method for saving the data and then call the read method again. Be careful because this could be an infinite loop if there are more errors. Other options?

8 XML save/load system Create a player file <pData> <xPos></xPos> <yPos></yPos> <zPos></zPos> <xRot></xRot> <yRot></yRot> <zRot></zRot> <xScale></xScale> <yScale></yScale> <zScale></zScale> </pData> pData is the root node and the anchor for the data and other nodes The order is not important as long as they are within the root

9 XML save/load system Create an enemy file <eData> <enemy> <name></name> <xPos></xPos> <yPos></yPos> <zPos></zPos> <xRot></xRot> <yRot></yRot> <zRot></zRot> <xScale></xScale> <yScale></yScale> <zScale></zScale> </enemy> </eData> eData is the root node and the anchor for the data and other nodes The enemy node holds the rest of the nodes that we save to and load from almost like a class or object. You can consider the enemy to be a class and the child nodes within it as the class's properties. The reason we are doing this is to save multiple enemies to our XML file, and they will each have their own data.

10 XML files Be sure to add the following using statements
using System.Xml; using System.Xml.Serialization; using System.IO; using System.Text;

11 Within the class Create several new variables
XmlDocument xPlayer = new XmlDocument(); XmlDocument xEnemy = new XmlDocument(); public string pFileName = ""; public string eFileName = ""; public GameObject Player; public GameObject[] Enemies; private string[] playerXMLs; private string[] enemyXMLs;

12 Initial loading Loading the files into the xml docs in start():
playerXMLs = System.IO.File.ReadAllLines (Application.dataPath + "/XML/" + pFileName); xPlayer.LoadXml( GetXMLString( playerXMLs)); enemyXMLs = System.IO.File.ReadAllLines (Application.dataPath + "/XML/" + eFileName); xEnemy.LoadXml( GetXMLString (enemyXMLs)); string GetXMLString (string[] XMLs) { string XMLString = ""; for (int i = 0; i < XMLs.Length; i++) XMLString += XMLs[i]; } return XMLString;

13 Saving the Player void SavePlayer() { if (Player != null) XmlNode root = xPlayer.FirstChild; foreach (XmlNode node in root.ChildNodes) switch (node.Name) case "xPos": node.InnerText = Player.transform.position.x.ToString(); break; case "yPos": node.InnerText = Player.transform.position.y.ToString(); } } xPlayer.Save(Application.dataPath + "/XML/" + pFileName); }

14 Saving the enemy void SaveEnemies() { xEnemy.RemoveAll(); XmlNode eRoot = xEnemy.CreateNode(XmlNodeType.Element, "eData", ""); string[] nodes = { "name", "xPos", "yPos", "zPos", "xRot", "yRot", "zRot", "xScale", "yScale", "zScale" }; for (int e = 0; e < Enemies.Length; e++) if (Enemies[e] != null) XmlNode eBase = xEnemy.CreateNode(XmlNodeType.Element, "enemy", ""); for (int n = 0; n < nodes.Length; n++) XmlNode newNode = xEnemy.CreateNode(XmlNodeType.Element, nodes[n], ""); eBase.AppendChild(newNode); }

15 Saving the enemy 2 foreach (XmlNode node in eBase.ChildNodes) { switch (node.Name) case "name": node.InnerText = Enemies[e].name; break; case "xPos": node.InnerText = Enemies[e].transform.position.x.ToString(); … } eRoot.AppendChild(eBase); xEnemy.AppendChild(eRoot); xEnemy.Save(Application.dataPath + "/XML/" + eFileName);

16 Loading the player Declare and initialize all the player variables then: if (Player != null) { XmlNode root = xPlayer.FirstChild; foreach (XmlNode node in root.ChildNodes) switch (node.Name) case "xPos": xPos = Convert.ToSingle(node.InnerText); break; } Player.transform.position = new Vector3(xPos, yPos, zPos); Player.transform.rotation = new Quaternion(xRot, yRot, zRot, 0.00f); Player.transform.localScale = new Vector3(xScale, yScale, zScale);

17 Loading the enemies Declare and initialize all the enemy variables then: for (int e = 0; e < Enemies.Length; e++) { if (Enemies[e] != null) XmlNode eData = xEnemy.FirstChild; XmlNode enemy = eData.ChildNodes[e]; if (enemy.Name == "enemy")

18 Loading the enemies 2 { switch (eNode.Name) case "name":
foreach (XmlNode eNode in enemy.ChildNodes) { switch (eNode.Name) case "name": name = eNode.InnerText; break; case "xPos": xPos = Convert.ToSingle(eNode.InnerText); . . . } Enemies[e].name = name; Enemies[e].transform.localPosition = new Vector3(xPos, yPos, zPos); Enemies[e].transform.localRotation = new Quaternion(xRot, yRot, zRot, 0.00f); Enemies[e].transform.localScale = new Vector3(xScale, yScale, zScale); } } } }

19 Save handler - checkpoint
Checkpoints are typically areas in the game world on reaching which the game will save the player's data void OnTriggerEnter(Collider other) { if(other.tag == "SavePoint") Camera.main.SendMessage("WriteToFile"); Destroy(other.gameObject); } take note that this function also destroys the trigger object so that the player can't reactivate the checkpoint

20 Save handler on demand To allow the player to save their data anywhere they want and anytime they want, add an option for them to call it. This can be done by adding to the menu a key or a button, or both, that the player presses. void Update() { if(Input.GetKeyUp(KeyCode.F1)) Camera.main.SendMessage("SaveEnemies"); } if(Input.GetKeyUp(KeyCode.F2)) Camera.main.SendMessage("LoadEnemies");


Download ppt "Save and Load Systems Richard Gesick."

Similar presentations


Ads by Google