I am making a RPG in Unity. Still quite new so I followed their tutorial on creating save/load functionality.
In this tutorial they use 2 variables, and for that it works great. But I want to create a large game, with sandboxing. Which means Ill need to save the position of everything in my world, on top of that I want a lot of player stats, unlockables, quests, npc progress, and much more.
So far I have a working implementation for saving/loading my players initial stats, and that alone has turned in to a lot:
DataManager
public class DataManager : MonoBehaviour {
private string saveLocation = "/data.dat";
public static DataManager manager;
//PLAYER
public int maxHealth;
public int currentHealth;
public int currentLevel;
public int currentExp;
public int [] toLevelUp;
void Awake(){
if (manager == null) {
DontDestroyOnLoad (gameObject);
manager = this;
} else if (manager != this) {
Destroy (gameObject);
}
}
public void Save(){
Debug.Log ("Saving");
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + saveLocation);
PlayerData playerData = new PlayerData (maxHealth, currentHealth, currentLevel, currentExp, toLevelUp);
bf.Serialize (file, playerData);
file.Close ();
}
public void Load(){
Debug.Log ("Loading");
if (File.Exists (Application.persistentDataPath + saveLocation)) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + saveLocation, FileMode.Open);
PlayerData playerData = (PlayerData)bf.Deserialize (file);
file.Close ();
maxHealth = playerData.maxHealth;
currentHealth = playerData.currentHealth;
currentLevel = playerData.currentLevel;
currentExp = playerData.currentExp;
toLevelUp = playerData.toLevelUp;
}
}
}
PlayerData
[System.Serializable]
public class PlayerData {
public int maxHealth;
public int currentHealth;
public int currentLevel;
public int currentExp;
public int [] toLevelUp;
public PlayerData(int maxHealth, int currentHealth, int currentLevel, int currentExp, int [] toLevelUp){
this.maxHealth = maxHealth;
this.currentHealth = currentHealth;
this.currentLevel = currentLevel;
this.currentExp = currentExp;
this.toLevelUp = toLevelUp;
}
}
This does not feel very scaleable at all. Someone with experience implementing save/load for a lot of variables with any advice/code samples? There must be some better way?
EDIT:
New DataManager:
public class DataManager : MonoBehaviour {
private string saveLocation = "/data.dat";
public static DataManager manager;
//PLAYER
public PlayerData playerData = new PlayerData ();
void Awake(){
if (manager == null) {
DontDestroyOnLoad (gameObject);
manager = this;
} else if (manager != this) {
Destroy (gameObject);
}
}
public void Save(){
Debug.Log ("Saving");
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + saveLocation);
bf.Serialize (file, playerData);
file.Close ();
}
public void Load(){
Debug.Log ("Loading");
if (File.Exists (Application.persistentDataPath + saveLocation)) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + saveLocation, FileMode.Open);
PlayerData pd = (PlayerData)bf.Deserialize (file);
file.Close ();
playerData = pd;
}
}
}