Hello! Probably a real noob question, but I’d rather learn the best practice before investing too much time on the “wrong ways”
I’m wanting to have multiple player/save slots in my game, and I’ve been debating about either an array to store the various stats (name, score, hi-score, level, etc) or to use multiple C# scripts (classes?) to store them (ex: Player1.name, Player2.name assuming the Player1 script has public variable (name, etc).
I could see how an array would lend itself to ease of programming if each player is assigned a row (i.e. playerArray[i,4] = playerName; where i is the player slot). But I also like the notion of a more “verbal” approach (player1.name = playerName;)
Hopefully I didn’t confuse you all, lol. I’m learning so much each day, and certainly appreciate all your help!
Usually in object oriented programming you want things to be objects.
Create a Player class, instantiate it with the desired values. For more characters, use an array.
If values can change, store and load those instances when needed.
If this is not just about saving the character but instead entire save games you should look at a more elaborate tutorial about these things first. You are never the first one to have a problem, and usually on common topics (even uncommon ones), you find tons of tutorials.
Thank you Yoreki. I have found a tutorial on actual “game saves” that I am going to look at. In the meantime, I wanted to make sure the routine storing of in-game values were being handled optimally. After that, I’ll figure out how to make them persist.
I’ve had a little success in the past understanding classes/objects, but in my short time with Unity, I was under the impression that Unity wasn’t purely Object-Oriented, and was “different”. Again, I’m not seasoned enough to know how or if its different, lol.
Actually you usually use both ^^. Though only one class that represents a player and an array of those player instances.
[System.Serializable]
public class PlayerSaveSlot
{
public string name;
public int score;
// whatever you need to remember per player slot
}
Now you can simply create an array of that class.
public PlayerSaveSlot[] saves;
Now it depends on how you want / need to use that. Since you mentioned “saving” it usually means that the data should persist even when you restart your game. In that case it’s best to pack all the relevant data into one general save class that is serialized. Something like that:
[System.Serializable]
public class SaveData
{
public List<PlayerSaveSlot> saves = new List<PlayerSaveSlot>();
// any additional general information that should be saved as well
}
That way you would simply create an instance of that SaveData class and store your information in that instance. When you need to actually save the data you can serialize that instance to JSON or whatever and save that data.
Keep in mind that you have to actually create instances of the PlayerSaveSlot class that you store in the array or List before you can use it.
public class GameData : MonoBehaviour
{
public SaveData saveData = new SaveData();
void Start()
{
Load();
}
public void Save()
{
string data = JsonUtility.ToJson(saveData);
PlayerPrefs.SetString("saveData", data);
}
public void Load()
{
if (PlayerPrefs.HasKey("saveData"))
{
string data = PlayerPrefs.GetString("saveData", "");
saveData = JsonUtility.FromJson<SaveData>(data);
}
}
}