- Home /
Highscores PlayerPrefs Sorting Issue
I need to make this class make a high scores table and make it so that when a new score is enter it goes ahead of the old one but I'm having a lot of trying trouble to figure it out, can someone help? using UnityEngine; using System.Collections; using System.Collections.Generic;
public class SaveLoad
{
public static string name;
static List<HighscoreHolder> arr_score = new List<HighscoreHolder>();
public static void Save ()
{
Load(); // Load current highscore list for comparison
if (GameManager.score > score[4].playerScore)// If you get a high score
{
name = score[5].playerName; //input player name into temp highscore
score[5].playerScore = GameManager.score;
//Sort array by score
arr_score.Add(new HighscoreHolder { playerName = name, playerScore = GameManager.score });
arr_score.Sort(s1,s2) => s2.arr_score == s1.arr_score ? 0 : (s2.arr_score < s1.arr_score ? 1 : -1));
while(arr_score.Count >= 6)
arr_score.RemoveAt(5);
//Put Score array top 5 back into player prefs
for (int i = 0; i < 5; i++)
{
PlayerPrefs.SetString("Name" + i, score[i].playerName);
PlayerPrefs.SetInt("Score" + i, score[i].playerScore);
}
}
}
public static void Load ()
{
score = new HighscoreHolder[6];
for (int i = 0; i < 5; i++)
{
score[i].playerName = PlayerPrefs.GetString("Name" + i);
score[i].playerScore = PlayerPrefs.GetInt("Score" + i);
}
}
public static void OnGUI ()
{
SaveLoad.Load();
GUI.Label(new Rect(Screen.width/2.25f, Screen.height/2.5f, 50, 20), score[0].playerName + ": " + score[0].playerScore);
GUI.Label(new Rect(Screen.width/2.25f, Screen.height/2.5f + Screen.height / 10, 50, 20), score[1].playerName + ": " + score[1].playerScore);
GUI.Label(new Rect(Screen.width/2.25f, Screen.height/2.5f + Screen.height * 2 / 10, 50, 20), score[2].playerName + ": " + score[2].playerScore);
GUI.Label(new Rect(Screen.width/2.25f, Screen.height/2.5f + Screen.height * 3 / 10, 50, 20), score[3].playerName + ": " + score[3].playerScore);
GUI.Label(new Rect(Screen.width/2.25f, Screen.height/2.5f + Screen.height * 4/ 10, 50, 20), score[4].playerName + ": " + score[4].playerScore);
}
}
Answer by whydoidoit · Mar 10, 2014 at 01:37 PM
So use a List and insert the new score, sort it and remove the 6th element if there is one.
using System.Collections.Generic;
...
static List<HighscoreHolder> score = new List<HighscoreHolder>();
...
score.Add(new HightscoreHolder { name = currentPlayerName, score=GameManager.score });
score.Sort((s1,s2)=>s2.score == s1.score ? 0 : (s2.score > s1.score ? 1 : -1));
while(score.Count >= 6)
score.RemoveAt(5);
I was looking at Lists before but i was having trouble using them in unity it doesn't seem to give me any code help for it and then it sort of accepts it sort of doesn't "Assets/Scripts/SaveLoad.cs(22,44): error CS1525: Unexpected symbol `=>'"
Lists work fine in Unity if you include System.Collections.Generic. Not sure why you Lambda isn't working - can you post exactly what you have?
Just updated the code onto the question, thanks for the help :)
Woops totally noticed where i stuffed up sorry i missed a ( :(
Answer by AnXgotta · Mar 10, 2014 at 12:42 PM
Here is some seudo-code to point you in the right direction
// Get array of current leader board
// Check if new HighScore is greater than lowest highScore on leader board
// if yes
// find index of new highScore in the leader board
// you could do this one of many ways (loop and check for example)
// bump other highScores down the leader board from determined index
// add new highScore to its index on leader board
// save new high scores
// if no
// do nothing
I think where most of my problem lays is with how to (bump other highScores down the leader board from deter$$anonymous$$ed index) push doesn't work and i'm not sure how to sort a 2D array
I see, sorting an array containing your 'HighscoreHolder' object is the problem. I would look at 'whydoidoit' answer. Implementing your own sorting mechanism using a List is a very straightforward way to do this.