Hey Nina,
Sorry for not answering earlier, rude of me. I just kept digging. I can not even tell you how off-track front he course I got in order to find the answer to the question.
My goal was to:
- Simply save score in PlayerPrefs for different players
- in the end of some minigames sort the scores (With the score sticking to the names.)
- From the sorting pull out the “Winner Name” and “Winner Score” and place them in two different textboxes.
Now I might have gone off the rails with this but for me it seemed to be absolutely impossible to do. My solution in the end, after learning alot but screaming even more, was to create a SaveLoadSystem.
I don’t know if your interested but I will paste the code. Through my system some extra strengths gets added like the fact that I can store by ID so I can store information per gameID. If I default the GameID at Save to 0 then I store a players score together. There is some bloated code that I still don’t get to work like
public static void SavePerson(String personName, int score, int gameId = 0, bool useAdditiveScoring = false)
The additiveScoring really isn’t needed there if the game isn’t of id 0, atleast not for my game.
Anyhow below is the SaveLoadSystem:
public static class SaveLoadSystem
{
public static void SavePerson(String personName, int score, int gameId = 0, bool useAdditiveScoring = false)
{
Dictionary<string, int> scorelist = LoadData(gameId);
if (scorelist.ContainsKey(personName))
{
if (useAdditiveScoring)
{
int oldScore = scorelist[personName];
score += oldScore;
}
scorelist[personName] = score;
}
else
{
scorelist.Add(personName, score);
}
SaveData(scorelist, gameId);
Debug.Log($"Saved person {personName} with a score of {score}.");
}
public static int GetPlayerScore(string playerName, int gameId = 0)
{
Dictionary<string, int> scorelist = LoadData(gameId);
if (scorelist.ContainsKey(playerName))
{
return scorelist[playerName];
}
return -1;
}
public static void RemovePlayer(string playerName, int gameId = 0)
{
Dictionary<string, int> scorelist = LoadData(gameId);
if (scorelist.ContainsKey(playerName))
{
scorelist.Remove(playerName);
SaveData(scorelist, gameId);
}
else
{
Debug.Log("Player " + playerName + " does not exist for this game!");
}
}
public static void RemoveAllData(int gameId = 0)
{
PlayerPrefs.DeleteKey("ScoreListData" + gameId);
}
public static Dictionary<string, int> LoadData(int gameId = 0, bool sorted = true)
{
Dictionary<string, int> scorelist = new Dictionary<string, int>();
if (PlayerPrefs.HasKey("ScoreListData" + gameId))
{
string scoreListData = PlayerPrefs.GetString("ScoreListData" + gameId);
string[] dataPairs = scoreListData.Split('|');
foreach (string dataPair in dataPairs)
{
string[] data = dataPair.Split(';');
if (data.Length != 2)
{
break;
}
int score;
if (int.TryParse(data[1], out score))
{
scorelist.Add(data[0], score);
}
else
{
Debug.LogError("Save Load System: An error has occurred loading the save data. Integer could not be parsed correctly");
}
}
}
else
{
Debug.LogWarning("Save Load System: Data was loaded but there was no data found. Something is wrong if you see this message more than once.");
}
if (sorted)
{
Dictionary<string, int> sortedScoreList = new Dictionary<string, int>();
var sortedDict = from entry in scorelist orderby entry.Value descending select entry;
foreach (KeyValuePair<string, int> entry in sortedDict)
{
sortedScoreList.Add(entry.Key, entry.Value);
}
return sortedScoreList;
}
return scorelist;
}
private static void SaveData(Dictionary<string, int> scoreList, int gameId)
{
string savedString = "";
foreach (KeyValuePair<string, int> entry in scoreList)
{
savedString += entry.Key + ";" + entry.Value + "|";
}
PlayerPrefs.SetString("ScoreListData" + gameId, savedString);
}
}
Below is an example of how to use it.
public class SaveLoadTest : MonoBehaviour
{
Dictionary<string, int> scoreList = new Dictionary<string, int>();
Dictionary<string, int> scoreList2 = new Dictionary<string, int>();
List<Text> nameTextList = new List<Text>();
List<Text> scoreTextList = new List<Text>();
void Start()
{
SaveLoadSystem.RemoveAllData();
SaveLoadSystem.SavePerson("Jack", 12);
SaveLoadSystem.SavePerson("TestPerson", 4);
SaveLoadSystem.SavePerson("Emily", 10);
SaveLoadSystem.SavePerson("Emily", 15, 2);
scoreList = SaveLoadSystem.LoadData(0, false);
scoreList2 = SaveLoadSystem.LoadData(2);
foreach (KeyValuePair<string, int> entry in scoreList)
{
Debug.Log(entry.Key + " has a score of " + entry.Value);
}
Debug.Log("-------");
scoreList = SaveLoadSystem.LoadData(0, true);
foreach (KeyValuePair<string, int> entry in scoreList)
{
Debug.Log(entry.Key + " has a score of " + entry.Value);
}
for (int i = 0; i < nameTextList.Count; i++)
{
nameTextList[i].text = scoreList.ElementAt(i).Key;
scoreTextList[i].text = scoreList.ElementAt(i).Value.ToString();
}
}
}
It still lacks some and there is code that I could make a lot better but this is just the result of 1 week of sadness, confusion, full blown crying and adding more to some code that I thought would be simple.
Hope it makes any sense:)
I will now like Hans and Grettel try to find my way back to the course