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.
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
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?
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.
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.
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
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.
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)
if scoreToAdd > lowestScore then
-- remove last and lowest score
table.remove(highscores, #highscores)
-- add new score
table.insert(highscores, scoreToAdd)
end
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?
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:
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)
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:
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?
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.