Creating multiple highscores?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
AlexCalv
Prole
Posts: 42
Joined: Fri Aug 08, 2014 7:12 pm

Creating multiple highscores?

Post by AlexCalv » Fri Aug 08, 2014 7:32 pm

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()

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

Re: Creating multiple highscores?

Post by Plu » Fri Aug 08, 2014 7:55 pm

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

Read about tables here: http://www.lua.org/pil/2.5.html

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

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

Re: Creating multiple highscores?

Post by AlexCalv » Fri Aug 08, 2014 8:59 pm

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.

User avatar
Ikroth
Citizen
Posts: 72
Joined: Thu Jul 18, 2013 4:44 am

Re: Creating multiple highscores?

Post by Ikroth » Sat Aug 09, 2014 4:44 am

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


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

Re: Creating multiple highscores?

Post by AlexCalv » Sat Aug 09, 2014 4:50 am

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.

User avatar
Ikroth
Citizen
Posts: 72
Joined: Thu Jul 18, 2013 4:44 am

Re: Creating multiple highscores?

Post by Ikroth » Sat Aug 09, 2014 5:09 am

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)

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

if scoreToAdd > lowestScore then
  -- add score
  table.insert(highscores, scoreToAdd)
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


if scoreToAdd > lowestScore then
  -- remove last and lowest score
   table.remove(highscores, #highscores)

  -- add new score
  table.insert(highscores, scoreToAdd)
end


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

Re: Creating multiple highscores?

Post by AlexCalv » Sat Aug 09, 2014 7:04 am

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?

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

Re: Creating multiple highscores?

Post by Plu » Sat Aug 09, 2014 7:16 am

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)

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

Re: Creating multiple highscores?

Post by AlexCalv » Sat Aug 09, 2014 7:26 am

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?
Image

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

Re: Creating multiple highscores?

Post by AlexCalv » Tue Aug 12, 2014 6:49 am

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.

Post Reply

Who is online

Users browsing this forum: Google [Bot], ivan, PGUp and 2 guests