Best Way to do Highscores?

I'm at the stage in my game where I'm adding a highscores system to it. I have a game over screen where I plan to save the score to the database and I then want to access and display it in my highscores screen. I've heard Ice is good, but I'm not sure how to use it.

What do you guys suggest and/or could you teach me how to use Ice? :P

Are you only planning on displaying one highscore or multiples?

The Ice page in sample code actually has a number of examples already :)

Peach

I'd say look at JSON. To describe it briefly, JSON is a really simple way to save or load a table, and it's native in Corona. To be exact, JSON is just a way to convert a table into a text string, or the other way around, and besides that all you need is the file IO functions to load or save this text file.

I'm at work now but I did this yesterday and can share code later tonight if you need it.

I'm not sure which one to use! Ice is simple but JSON is included with Corona so I don't need extra files. :/

Could I see a JSON example? It might narrow down my decision.

Real quick (from work :-)

This is what I put in main.lua on startup:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-- SETUP HIGHSCORES !!!
require("json")
local path = system.pathForFile( "highscores.txt", system.DocumentsDirectory )
 
-- io.open opens a file at path. returns nil if no file found
local file = io.open( path, "r" )
 
if file then
        print("Highscore File Found!")
        -- read all contents of file into a string
        local contents = file:read( "*a" )
        highscoreTable = json.decode(contents)
        print(highscoreTable[1])
        io.close( file )
else
        -- create file because it doesn't exist yet
        print("Creating New Highscore File!")
        file = io.open( path, "w" )
        highscoreTable = {10000, 9000, 8000, 7000, 6000, 5000, 4000, 3000, 2000, 1000}
        local highscoreTableJSON = json.encode(highscoreTable)
        file:write(highscoreTableJSON)
        io.close( file )
end

In the else statement, what do all of those numbers in the brackets do?

Also, I had an error in the code when I put it in the first time and now I can't find the file to delete it and create a newer, updated one. Where did the highscores.txt save to?

Check the text in the terminal when you start the code in the simulator. This tells you where the temp data for the project is on your hard-drive.

What error did you get? Make sure you start with an 'empty' project, meaning: don't start with a self-made highscores.txt file - the code takes care of this!

The numbers in the brackets are just 10 startup scores, from 10000 to 1000. You could also put zeros instead for a really empty start.

To be completely clear: the numbers between brackets are a table containing 10 entries. JSON converts the table to a text file so it can be saved - and all of this is in the 'else' part meaning: if there is no file called highscores.txt present (only the first time the app is started) write a table to disk containing these values: {10000, 9000, 8000, ... }

The second time your app starts, it will see this freshly created file right away and won't have to write a new one.

So, the main.lua code just does this:

if there is a file called highscores.txt then
read that file into the variable highscoreTable
else (so, if that file doesn't exist yet - the first time the app is started)
write our own empty file containing these ten default values: 10000, 9000, ...
end

I just realized something that may be a problem. My score is saved as a $0.00 because in my game you collect coins. Is there a way I can adapt the code above to support a dollar/cents value?

The problem won't be in the main.lua code but in the second part, because that's where the score is treated as a number (as it should be, because you want to determine which place in the top-10 scores the new score will take).

But honestly, please tell me you keep track of the score as a number, and not as a string! Just use number, and add the dollar sign in the displaying of the score.

So, what exactly was the problem you get?

I guess that I don't have the problem because I format the number into a string when it's displayed.

textMoney.text = string.format("$%.02f", storyboard.money)

I added the code and there aren't any errors, so that's a good thing. Now, how would I display the scores from the table in my highscores screen?

views:1854 update:2012/1/1 13:29:50
corona forums © 2003-2011