- Home /
Sorting Error
static function AddScore(name : String, score : int){
var newScore : int;
var newName : String;
var oldScore : int;
var oldName : String;
newScore = score;
newName = name;
for(var i=0;i<5;i++)
{
if(PlayerPrefs.HasKey(i+"HScore"))
{
if(PlayerPrefs.GetInt(i+"HScore")<newScore)
{
// new score is higher than the stored score
oldScore = PlayerPrefs.GetInt(i+"HScore");
oldName = PlayerPrefs.GetString(i+"HScoreName");
PlayerPrefs.SetInt(i+"HScore",newScore);
PlayerPrefs.SetString(i+"HScoreName",newName);
newScore = oldScore;
newName = oldName;
}
}
else
{
PlayerPrefs.SetInt(i+"HScore",newScore);
PlayerPrefs.SetString(i+"HScoreName",newName);
newScore = 0;
newName = "";
}
}
}
I used the code above and I feel like it goes in to a loop uncontrolled. Here is an example of what happens. If the player gets a new score that is higher then the highest score then it wipes out the old score and replaces it with the highest.
So: Dan : 75 Dan : 50 Dan : 25 Dan : 25 Dan : 25
Becomes: Dan : 100 Dan : 100 Dan : 100 Dan : 100 Dan : 100
I'd recommend using ArrayPrefs2 ins$$anonymous$$d of doing this. Normally I'd link to the wiki, but it seems to have been hacked (again). Here is the script anyway; you might be able to read the instructions by using Google's cached version. Anyway it's used like PlayerPrefs, except it reads and writes arrays of various types.
I think this should work. I didn't test it.
The only thing i can think of which produces this behaviour is that you execute AddScore multiple times. Each time the next smaller highscore will be kicked out. So after 5 calls all are the same.