## Creating multiple highscores?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
AlexCalv
Prole
Posts: 42
Joined: Fri Aug 08, 2014 7:12 pm

### Creating multiple highscores?

I'm trying to add a way to have multiple highscores in my game, but I can only get one. I also want them to order from greatest to least. This code sets the first highscore, but I have no idea how to check or even make other scores and save them to a file to load.

Code: Select all

function highscore_counter()
if player.score > highscore1 then
highscore1 = player.score
end
end
This is the code I use to save the scores:

Code: Select all

savefile:open("w")
savefile:write("highscore1=" ..highscore1.. "\nhighscore2=" ..highscore2.. "\nhighscore3=" ..highscore3)
savefile:close()

Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

### Re: Creating multiple highscores?

You're going to want to use a table for this. Do you have any experience working with them?

Some quick snippets:

Code: Select all

highscores = { } -- create an empty table
table.insert( highscores, 5000 ) -- insert the number 5000 into the table
table.sort( highscores ) -- sort the values in the table
table.setn( highscores, 10 ) -- limits the length of the table to 10 entries

For saving the highscorelist to a file, look for a table serialization library, there's bound to be some around on the wiki

AlexCalv
Prole
Posts: 42
Joined: Fri Aug 08, 2014 7:12 pm

### Re: Creating multiple highscores?

Yeah I've used tables and know how they work for basic things I think. This sounds simple enough then, I never thought to use a table for this. How would I check what score would be needed for the different scores though? I could do this for the 1st place score, but how would I check if the score can be used as a 2nd or 3rd place in the list? Also, would I just use this then?

Code: Select all

	for i,v in ipairs(highscores) do
love.graphics.printf(highscore1,0,gheight/2-50,gwidth,"center")
end
Edit: Does this forum seem painfully slow for anyone else lately? I've lurked for a while and it's usually pretty speedy. Also what is up with needing to have my posts approved? It's annoying.

Ikroth
Citizen
Posts: 77
Joined: Thu Jul 18, 2013 4:44 am

### Re: Creating multiple highscores?

You'd need to sort the table to figure out which one is 1st, 2nd, etc. As long as you are sorting the table, it's unnecessary to know whether or not a score belongs in 2nd or 3rd place when you are adding it to the highscores.

Code: Select all

highscores = {7500, 2000, 3500, 5000} -- table is unsorted, can't tell which is 1st place or 2nd place
table.sort(highscores) -- table is now sorted

-- highscore[1] will tell you the 1st place score (7500)
-- highscore[2] will tell you the 2nd place score (5000), and so on



AlexCalv
Prole
Posts: 42
Joined: Fri Aug 08, 2014 7:12 pm

### Re: Creating multiple highscores?

Okay, that makes sense now. How would I go about checking if something is worthy of a highscore though? Atm I just check if the player.score is higher than the highscore and set the highscore to equal the player.score and then save the highscore in a file.

Ikroth
Citizen
Posts: 77
Joined: Thu Jul 18, 2013 4:44 am

### Re: Creating multiple highscores?

To figure out if a score is worthy of being added, it needs to be higher than the lowest score.

Code: Select all

highscores = {7500, 2000, 3500, 5000, 6000}}
table.sort(highscores)

lowestScore = highscores[#highscores] -- #highscores = number of highscores (in this case, 5, so it's the last and lowest score)

end


If you wanted to limit the highscores to a specific number, you'd need to remove the old highscore when you insert it. (table.setn is deprecated)

Code: Select all


-- remove last and lowest score
table.remove(highscores, #highscores)

end



AlexCalv
Prole
Posts: 42
Joined: Fri Aug 08, 2014 7:12 pm

### Re: Creating multiple highscores?

I'm very confused as to how I could implement this into my game. I feel like I have to change everything to compensate for the changes. I'll just leave it how I have it currently, in my next game I'll design it with this in mind. One more thing though, how would I go about printing the scores to the screen? And would these be easy to save into a different file?

Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

### Re: Creating multiple highscores?

Ah, didn't know setn was deprecated.

Honestly, you don't need to check where a score belongs. Just add it to the list, sort the list, and then remove the last value. The outcome will always be the correct new highscore list.

As for printing to the screen; you have to loop over the values and output them each:

Code: Select all

for position, score in ipairs( highscores ) do
love.graphics.print( score, 100, 100 + 20 * position ) -- we increase the print-y location with each row so we get a list
end

For saving to a file, you need a serialization library. Those are easy to find and reduce the saving process to about as much work as your current saving setup. (ie; a few lines)

AlexCalv
Prole
Posts: 42
Joined: Fri Aug 08, 2014 7:12 pm

### Re: Creating multiple highscores?

Plu wrote:Ah, didn't know setn was deprecated.

Honestly, you don't need to check where a score belongs. Just add it to the list, sort the list, and then remove the last value. The outcome will always be the correct new highscore list.

As for printing to the screen; you have to loop over the values and output them each:

Code: Select all

for position, score in ipairs( highscores ) do
love.graphics.print( score, 100, 100 + 20 * position ) -- we increase the print-y location with each row so we get a list
end

For saving to a file, you need a serialization library. Those are easy to find and reduce the saving process to about as much work as your current saving setup. (ie; a few lines)
How does the table know what the position and score vars are? We never defined them? The code now seems to work. Partially. It only uses one score slot, and it uses the bottom one first?

AlexCalv
Prole
Posts: 42
Joined: Fri Aug 08, 2014 7:12 pm

### Re: Creating multiple highscores?

Are bumps allowed? I'm very confused on how this all works. Tables are very confusing to me. The only thing I've used them for so far is to make enemies.

### Who is online

Users browsing this forum: Google [Bot] and 10 guests