I don’t know JS very well but heres a shot.
// generates a key for playerprefs
static function PlayerPrefKey( var levelName : string, var category : string, var place : int ) : string {
return levelName + "-" + category + "-" place;// gets a string combining the parameters ( "level-category-#" )
}
// gets the top 5 attempts for level.
static function TopAttemptCounts( var levelName ) : int[] {
// make a array of 5 ints
var array : int[] = new int[5];
var place : int = 0;// the current place getting out of player prefs ( start with zero )
for(; place < 5; place ++){
var key : string = PlayerPrefKey( levelName, "attempts", place );// get the key which this level, category, and place would be.
if( !PlayerPrefs.HasKey(key) )
break;// if there is no key for this place, we havnt completed it 5 times and we break out of this loop.
else
array[place] = PlayerPrefs.GetInt(key);// there is a place stored for this category and we set it to the array position.
}
// if place doesnt equal five its because we do not have enough records
if( place < 5 )
System.Array.Resize( ref array, place );// resize our array ( shrink it ) to the amount of places we've read.
// return the array
return array;
}
// records the attempt count to player prefs, sorting existing scores with this new one.
static function RecordAttemptCount( var levelName : string, int attempts ) {
var topAttempts : int[] = TopAttemptCount();// get the existing top attempts.
var placeComparing : int = 0;
var placeWriting : int = 0;
var wrote : boolean =false;
// compare each existing place.
for( ; placeComparing < 5; placeComparing ++)
{
if( topAttempts.Length <= placeComparing )
{
if(!wrote)
// we didnt have 5 records. record this and break out of the function with a return.
PlayerPrefs.SetInt( PlayerPrefKey( levelName, "attempts", placeWriting++), attempts );
// if wrote was true, we already put it in and do not need to write it at this point because it was better.
return;
}else if( topAttempts[placeComparing] > attempts ){// see if the existing place is worse than the attempt count were commiting.
// if it is we've got to write it. before
PlayerPrefs.SetInt( PlayerPrefKey( levelName, "attempts", placeWriting++);
wrote = true;
}
// if we wrote a new score to it we must write scores that were worse afterwards ( we do not need to write scores before it which were better )
if( wrote placeWriting < 5 ){ // we check for < 5 to make sure we don't write 6 scores.
PlayerPrefs.SetInt( PlayerPrefKey( levelName, "attempts", placeWriting++);
}
}
}
static function TopAttemptsCountCurrentLevel() : int[] { return TopAttemptsCount(Application.loadedLevelName); }
static function RecordAttemptCountCurrentLevel( int attempts ) { RecordAttemptCount( Application.loadedLevelName, attempts ); }
you would make those functions public somewhere and call them to either record a new attempt count or get the top 5 ( or less if 5 playthroughs havent happend )
PlayerPrefKey gets a string which is unique to the level, category, and rank/placing.
TopAttemptsCount would return a array of ints, where array[0] would be the best ( lowest ) amount of attempts made for the level.
RecordAttemptsCount would push in a new attempt count into the records, and insert it so that the best would be at 0.
The TopAttemptsCountCurrentLevel and RecordAttemptsCountCurrentLevel just use Application.loadedLevel as the levelName.
Hope this helps and that all compiles ok. Again i don’t know JS very well. But basically you just got to store things in some fashion you can read from them in a way you like.
I didnt do the time part of the scoring, because i don’t know what data type your using besides string and how to compare it. If you can you should probably store the time as either a float or a int ( like converting it to milliseconds, or a float of seconds ). Then you would just copy and paste those two functions, modify them to compare how you’d like and have them use “time” instead of “attempts” for the PlayerPrefKey calls.
edit:
haha thats ushually why i avoid a comments, it looks garbled and unledgable. but paste that into the text editor and it should be more readable.