For the final feature in our flat file system, we add the loading functionality.
Now that we have created a way to save information to a flat file, we need to create a way to load that information. To do this, we will use a similar process as the one that we used to save the information. Let's add our final function to the script:
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()); int totgold = Convert.ToInt32(sr.ReadLine()); int curgold = Convert.ToInt32(sr.ReadLine()); int level = Convert.ToInt32(sr.ReadLine()); int rwon = Convert.ToInt32(sr.ReadLine()); int rlost = Convert.ToInt32(sr.ReadLine()); float pkdr = Convert.ToSingle(sr.ReadLine()); float pwlr = Convert.ToSingle(sr.ReadLine()); float ptime = Convert.ToSingle(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); } }
To load our data from the file, we will use StreamReader
. StreamReader
is very similar to StreamWriter
, except that it loads data from a file instead of writing to a file. We pass the directory and filename that we want to load into our StreamReader
class. Next, we read each line using the ReadLine
function within the StreamReader
class. For every line that we read, we can assign it to a variable, therefore loading the data.
3.15.26.221