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? [import]uid: 103624 topic_id: 19648 reply_id: 319648[/import]
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. [import]uid: 70134 topic_id: 19648 reply_id: 75955[/import]
-- 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
And this is what I put on gameover:
-- check against highScore !!!
if score \< highscoreTable[10] then
-- not in the list so do nothing!
else
-- somewhere in the list!
local tempTable = {}
local counter = 0
local highscoreEntered = false
for i = 1, 10 do
if (score \> highscoreTable[i]) and (highscoreEntered == false) then
tempTable[i] = score
counter = counter + 1
highscoreEntered = true
else
tempTable[i] = highscoreTable[i-counter]
end
end
for i = 1, 10 do
highscoreTable[i] = tempTable[i]
end
end
-- save highScore to file!!!
local path = system.pathForFile( "highscores.txt", system.DocumentsDirectory )
print("Saving Highscore File!")
file = io.open( path, "w+" )
local highscoreTableJSON = json.encode(highscoreTable)
file:write(highscoreTableJSON)
io.close( file )
Basically I save the 10 highest scores. Go over this and see how that looks, for now. More info later if needed.
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? [import]uid: 103624 topic_id: 19648 reply_id: 76002[/import]
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! [import]uid: 70134 topic_id: 19648 reply_id: 76005[/import]
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. [import]uid: 70134 topic_id: 19648 reply_id: 76007[/import]
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. [import]uid: 70134 topic_id: 19648 reply_id: 76008[/import]
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 [import]uid: 70134 topic_id: 19648 reply_id: 76009[/import]
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? [import]uid: 103624 topic_id: 19648 reply_id: 76056[/import]
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? [import]uid: 70134 topic_id: 19648 reply_id: 76080[/import]
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? [import]uid: 103624 topic_id: 19648 reply_id: 76085[/import]
My highscores only update on restart. For example, if I get a highscore and then check the highscores page, it won’t show it because it hasn’t the decoding of the highscores table hasn’t been updated.
What should I do? Should I just copy and paste the code in my main.lua and remove the else statment, or is there an easier way? [import]uid: 103624 topic_id: 19648 reply_id: 76451[/import]