x


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

more ▼

asked Aug 01 '12 at 01:13 AM

Danboe2009 gravatar image

Danboe2009
3 1 3

I'd recommend using ArrayPrefs2 instead 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.

Aug 01 '12 at 02:01 AM Eric5h5

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.

Aug 01 '12 at 02:19 AM Bunny83
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1952
x296
x274
x40
x37

asked: Aug 01 '12 at 01:13 AM

Seen: 222 times

Last Updated: Aug 01 '12 at 02:19 AM