Saving Score for Multiple Levels

RESOLVED.  Thank you for all the input.  I have it working now.

Sorry it’s taken me so long to get back to you on this, not been near my computer for a while…

The json.lua is a a standard library that is included as part of Corona.  The code above goes in whichever file in your project that it is needed (eg. game.lua ).  You need to require json at the top of the file to load the module, and then the loadtable and savetable are just two functions that you call as needed, so at game over or level over you check to see if it is a new highscore and then call the savetable function as I mentioned above.

I hope that makes it a bit easier to understand.

To add to what @Appletreeman said… If you have a json.lua file in your project, you’re likely going to run into problems down the road.  We provide json.lua as part of our core.  Just do: 

local json = require(“json”)

and you should be all set.

Thank you all for your help.

I figured out everything and it works fine but I have another question. When I exit the app and re-open it, then the score goes back to the initial value, not the last one it was supposed to save…

How do I keep my score in memory even when the app is closed and reset?

Thanks

highScores = {“level1”=0, “level2”=0, “level3”=0}

how can i load a single value from this table. If I only want to load level3’s value, then how can I do it?

pls reply with code…

Why not use an array instead of a key-value pair table:

highScores = { 0, 0, 0 }

print( highScores[1] )

Then you can just use a number for the level to index the table.

currentLevel = 3

highScores[currentLevel] = 100

Let’s assume:  highScores = {200, 300, 50, 125}

Then the player replays level3 and gets a score of 225; since it is higher I want to save the 225  and overwrite the 50. 

  1. To overwrite the 50 would I use the following?

currentLevel = 3

highScores[currentLevel] = 225

 2) Would this impact the scores I previously saved for levels 1, 2 and 4?

 

 3) If I wanted to look at the files in myproject Sandbox:

     a) Would I be able to retreive and view my saved scores in Sandbox? 

      b) If so, would it list all of the scores or just the last one saved? 

  1. Should the array be saved as a seperate module?

Thanks

Lori

I’m not using JSON, I was using the method in Rob’s tutorial for Displaying and Saving Score. 

  1. If you only want it to overwrite when the new score is higher, you need to add an if statement before you actually overwrite the value:

    currentLevel = 3 currentScore = 255 if currentScore > highScores[currentLevel] then highScores[currentLevel] = 225 end 

This way, you only overwrite the value when the new score is higher.

  1. The method you’ve suggested would leave the other level data unaffected. As long as you are specifying a number in the square brackets (or a variable that represents a number) then you will only affect the data in that table entry.

  2. It depends on exactly what you choose to save. If you overwrite and then save your highScores table after you add a new high score then you will see that table.

  3. I always save the array/table into a JSON file, just because I know they work well on all platforms. I’m not sure what format Rob’s save function uses, I would presume it’s JSON or SQL.

Thanks for the quick response - it was very helpful.

RESOLVED.  Thank you for all the input.  I have it working now.

highScores = {“level1”=0, “level2”=0, “level3”=0}

how can i load a single value from this table. If I only want to load level3’s value, then how can I do it?

pls reply with code…

Why not use an array instead of a key-value pair table:

highScores = { 0, 0, 0 }

print( highScores[1] )

Then you can just use a number for the level to index the table.

currentLevel = 3

highScores[currentLevel] = 100

Let’s assume:  highScores = {200, 300, 50, 125}

Then the player replays level3 and gets a score of 225; since it is higher I want to save the 225  and overwrite the 50. 

  1. To overwrite the 50 would I use the following?

currentLevel = 3

highScores[currentLevel] = 225

 2) Would this impact the scores I previously saved for levels 1, 2 and 4?

 

 3) If I wanted to look at the files in myproject Sandbox:

     a) Would I be able to retreive and view my saved scores in Sandbox? 

      b) If so, would it list all of the scores or just the last one saved? 

  1. Should the array be saved as a seperate module?

Thanks

Lori

I’m not using JSON, I was using the method in Rob’s tutorial for Displaying and Saving Score. 

  1. If you only want it to overwrite when the new score is higher, you need to add an if statement before you actually overwrite the value:

    currentLevel = 3 currentScore = 255 if currentScore > highScores[currentLevel] then highScores[currentLevel] = 225 end 

This way, you only overwrite the value when the new score is higher.

  1. The method you’ve suggested would leave the other level data unaffected. As long as you are specifying a number in the square brackets (or a variable that represents a number) then you will only affect the data in that table entry.

  2. It depends on exactly what you choose to save. If you overwrite and then save your highScores table after you add a new high score then you will see that table.

  3. I always save the array/table into a JSON file, just because I know they work well on all platforms. I’m not sure what format Rob’s save function uses, I would presume it’s JSON or SQL.

Thanks for the quick response - it was very helpful.