Saving high scores and retrieving them

Hey guys.

I’m trying to store a list of highscore values using Json. The highscores are in a Class dedicated to it with a name and a score variable. I store that Class list after converting it to a Json string.

string json = JsonUtility.ToJson(highScores); // Convert highScore object to Json
PlayerPrefs.SetString("highScores", json);
PlayerPrefs.Save(); // Save Json obj
Debug.Log(PlayerPrefs.GetString("highScores")); // Get Json Obj

This works perfectly when I just test it with some random score values assigned to the class variables. Though I have no idea of going beyond that. Like, how to convert the retrieved string back to a HighScore object, so I can display them in a grid (creating a grid part I know), how to store each highscore of every player who plays the game in the HighScores class list.
Is this the right way to do it anyway? Or is there a simpler or more efficient method? Any tutorials you know featuring how to do full high score list save and load (cuz I can’t find any yet)?

Thanks for any input.

Well, the simple answer is when you want to go the other way, you need to reverse what you are doing. In this case, load your playerpref, then convert it back from the json string into the class. How you save them is sort of up to you depending on how you want to display them. Do you need to have everyones scores together or just on their own? That sort of thing.

Thanks.

I store the name and highscore in the HighScore class like, { name=“JonDoe”, score=50 }. I made a of this class, and that list is what I’m saving with Json as a string. Every player name and score needed to be stored, saved, retrieved, and displayed. So, when I save a new player and score, how not to overwrite the current save data, and just append to it?

When you retrieve this data as a string at the start, you will cast it back into a list of HighScore. So you just add onto that highscore list and then convert it back into a string and save it back out. Yes, it overwrites, but it’s just all the same data + whatever new score you added.

Thanks for the help (Y).

I dumped Json thing and just saved the names and scores as separate strings. And now I retrieve and recreate the highscore list assigning the saved scores and names. It’s working now, Thanks!