Chapter 7. Creating Save and Load Systems

Saving data within a game is very important, which can be seen in just about every game out there. There is all kinds of data that you might want to keep track of, not only for yourself, but also for the player. The player's inventory, enemies' position, the player's statistics, and a lot more can be saved and loaded from a file that you create. In Unity, there are several ways to save data that you can choose from. Earlier in this book, we already went over how to use PlayerPrefs to save and load data. In this chapter, you will learn how to use XML and custom flat files, and then create a way to activate your saving and loading processes.

In this chapter, you will:

  • 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

Saving data with flat files

The first and more common way to save data (that we will go over) is using the flat file system. In this style, you can save a normal text file with data from your game and load it later on. We will also discuss how to customize our file so that we can have our own extension added to it. For our flat file system, we will save the player's position as well as our statistics that we created earlier in this book. To start off, we will need to create a new C# script and name it FLAT_Save_System.

Adding the required variables

Before we start adding our variables and other code, we need to make sure that we have all the required using statements. Add these using statements to your code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using System.Text;

For this script, we will only need to create a few public variables; add these to your script:

public string sFileName;
public string sDirectory;
  
public GameObject Player;

The first string will be the file that we will save and load from will be called, the name should also include the extension. The next string is the directory that we will save to and load from—for testing purposes and using the Desktop directory so that we can easily find it later on. Our final variable, GameObject, is our player.

Time to save our file

Now, we will add the function that will allow us to save our flat file. To save our file, we will use the code that we didn't discuss in this book before, as you can see from our new using statements. Add this function to your script:

void WriteToFile(string file = "")
{
  if(file != "")
    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(PlayerPrefs.GetInt("PlayerCurrentGold").ToString());
    sw.WriteLine(PlayerPrefs.GetInt("PlayerGoldSpent").ToString());
    sw.WriteLine(PlayerPrefs.GetInt("PlayerLevel").ToString());
    sw.WriteLine(PlayerPrefs.GetInt("PlayerRoundsWon").ToString());
    sw.WriteLine(PlayerPrefs.GetInt("PlayerRoundsLost").ToString());
    sw.WriteLine(PlayerPrefs.GetFloat("PlayerKDR").ToString());
    sw.WriteLine(PlayerPrefs.GetFloat("PlayerWLR").ToString());
    sw.WriteLine(PlayerPrefs.GetFloat("PlayerTimePlayed").ToString());
    sw.WriteLine(Player.transform.position.x.ToString());
    sw.WriteLine(Player.transform.position.y.ToString());
    sw.WriteLine(Player.transform.position.z.ToString());
  }
}

This function takes in a string. We have set the string to a blank value; this will allow us to call the function with or without sending the value. To start off the function, we check whether the file has a value; if it does, then we will use it as our new filename. If it does not, then we will continue to use the filename we previously set in our public variable.

After this, we check whether our file already exists. If it does, we run a function that will delete that file; we will create this function later on in this chapter. We delete the file so that we don't have any issues with duplicate files or incorrect filenames being made.

Next, we start the process of creating and saving our file. To do this, we use a StreamWriter type. StreamWriter allows us to write data to a file. We use a basic instance of StreamWriter, but the class also has other options that can expand upon how you write your data.

To use StreamWriter, we set the path that we want to write to, or in this case, the stream, and then add lines to that stream that will be written to our file. To add lines to our file, we call the native WriteLine function from the StreamWriter class. Within this call, we pass the variable that we want to save. For this instance, we grab PlayerPrefs we had set earlier as well as the player's transform position.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.166.149