How could I save multiple Highscores?

I’m currently making a game with multiple levels, and I need to keep a high score for each one. I used the following thread to save a single high score,

https://coronalabs.com/blog/2013/12/10/tutorial-howtosavescores/#comment-643947

but I’m not sure how I’d apply the same set of code to multiple variables. Thanks in advance!

It  should help you Tutorial: Saving and loading Lua tables with JSON

I wrote a library (one of many out there, I’m sure) to help with filing operations. With this, you can keep a table of tables for scores and write it to the documents (or any other) folder in one line:

https://gist.github.com/HoraceBury/d8d2fa3382f82a7c7faa

For example, this code would load a scores file, if it’s there, add or update the high score for level 10 and save it again:

-- load the io library local iolib = require("iolib") -- initialise the scores table local scores = {} -- load the scores file if it exists in the documents folder if (iolib.docExists( "scores.json" )) then scores = iolib.wrDocs( "scores.json" ) end -- create the level 10 table inside the scores table, if necessary scores[10] = scores[10] or { highscore=0 } -- update the level 10 high score value scores[10].highscore = 132 -- save the scores table to the documents folder iolib.wrDocs( "scores.json", scores )

Thank you for this, although I put the iolib code into my game as a iolib.lua file, and tried to execute the above code, but I keep getting an error saying ‘docExists’ is a nil value. Am I doing something wrong?

Actually, I got this to work. Thanks!

I would imagine you’re missing the ‘iolib.’ from in front of the ‘docExists()’ call, but good to know you got the saving working.

It  should help you Tutorial: Saving and loading Lua tables with JSON

I wrote a library (one of many out there, I’m sure) to help with filing operations. With this, you can keep a table of tables for scores and write it to the documents (or any other) folder in one line:

https://gist.github.com/HoraceBury/d8d2fa3382f82a7c7faa

For example, this code would load a scores file, if it’s there, add or update the high score for level 10 and save it again:

-- load the io library local iolib = require("iolib") -- initialise the scores table local scores = {} -- load the scores file if it exists in the documents folder if (iolib.docExists( "scores.json" )) then scores = iolib.wrDocs( "scores.json" ) end -- create the level 10 table inside the scores table, if necessary scores[10] = scores[10] or { highscore=0 } -- update the level 10 high score value scores[10].highscore = 132 -- save the scores table to the documents folder iolib.wrDocs( "scores.json", scores )

Thank you for this, although I put the iolib code into my game as a iolib.lua file, and tried to execute the above code, but I keep getting an error saying ‘docExists’ is a nil value. Am I doing something wrong?

Actually, I got this to work. Thanks!

I would imagine you’re missing the ‘iolib.’ from in front of the ‘docExists()’ call, but good to know you got the saving working.