I’m diving into this saving and loading state for the first time and I don’t quite understand it due to the various types of saving and loading options.
Does anyone know of a simple assets package that can do what I’m trying to do as described below?
What I’m looking for is a simple way to save and load a game state that saves this info to a file on the harddrive. I’m not interested in using PlayerPrefs to save this info to the registry.
All I want to do is save how many lives the player has and which level they are on. That is all. A very simple save state. I want to be able to have this information stored in a file then from the menu when I click Continue it will start the player off from the level they were last on that we stored in the save state and with the amount of lives too.
If you know of an asset package available I can use for this from the asset store please let me know. If this is something you could also do easily yourself, I wouldn’t mind pass a few $$$ your way for the effort.
You can use the same system for writing to and reading from file as in any other C# program - the File.Write and File.Read methods works perfectly fine.
Unity provides a variable named Application.persistentDataPath, which will give you a directory to save data to. Here’s a small example:
public class Player {
public int numLives;
public string currentLevel;
/// <summary>
/// Path to the save file.
/// On Windows, it will be:
/// C:/Users/(User)/AppData/LocalLow/(Company)/(Game)/PlayerData
/// Where Company and Game is what you set it to in the player settings
/// </summary>
private static string path {
get {
return Application.persistentDataPath + "/PlayerData";
}
}
public static void SaveToFile(Player p) {
//Very simple system - each piece of data gets on line
string[] data = {
p.numLives.ToString(),
p.currentLevel
};
//Writes to file. Will overwrite any exisiting data!
File.WriteAllLines(path, data);
}
public static Player ReadFromFile() {
//Reads from file. Will fail if the file doesn't exist!
string[] data = File.ReadAllLines(path);
//read from the file
Player loadedPlayer = new Player();
loadedPlayer.numLives = int.Parse(data[0]);
loadedPlayer.currentLevel = data[1];
return loadedPlayer;
}
}
You can test it like this:
public class TestScript : MonoBehaviour {
void Start() {
Player player = new Player {
numLives = 5,
currentLevel = "IceLevel"
};
Player.SaveToFile(player);
Player loadedPlayer = Player.ReadFromFile();
Debug.Log(loadedPlayer.numLives);
Debug.Log(loadedPlayer.currentLevel);
}
}
That should be enough to get you going. Note that the code doesn’t handle problems like the file not existing yet when you load, but File has methods to check for those kinds of things.
A lot of developers will use a solution like JSON or YAML to save their data. Both are formats that turn your objects into text, and turns text into objects. They have a bunch of advantages - you don’t have to think about things like parsing integers from strings properly - but there’s a bit of setup that’s necessary. If this is your first time, I’d recommend just writing and reading text from a file, as that’s one step less to wrap your head around.
You can take a look at my free package SerializeHelper:
It is aimed at teaching the basics of saving and loading scene data. In your case, if all you want to do is save and load player lives and current level number, then ignore the ObjectIdentifier script, add both variables to the SaveGame class, and assign it in the SaveGame and LoadGame functions in SaveLoadMenu.