Some problems with Rob Miracle's json saving method

@borrrz - Do you think there is a problem with Rob’s code (which hundreds of folks have used w/o issues) or something else is going on?

I’m not asking to be confrontational, but I do want to point out, the problem is likely in the way you’re using it.

PS - Kudos by the way for posting your code and the code you used from Rob.  That is awesome.

PPS - I probably misread the title, I’m guessing you meant 'having some problems using Rob’s …"  

I’ll think about this a little bit more and offer some solutions shortly.

I heavily modified your code (may contain typos).

local scoreTable local score = 0 -- local highScoreLabel = display.newText( score, 220, 243, native.systemFont, 24 ) -- --- local function saveScoreData() loadsave.saveTable( scoreTable, "score.json", system.DocumentsDirectory) end -- local function restoreScoreData() scoreTable = loadsave.loadTable( "score.json", system.DocumentsDirectory) or { highScore = 0 } end -- local function checkForhighScore() if( score \> scoreTable.highScore ) then scoreTable.highScore = score highScoreLabel.text = tostring(score) saveScoreData() end end -- local function dumpScoreData() loadsave.printTable(scoreTable) end restoreScoreData()

Let me suggest, that if you wanted to you could also store/restore your scores using SSK2 persist:
https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/persist/
 
This is a ‘arbitrary data storage’ system that also has auto-caching and delayed writes to avoid performance hits writes for cases just like this, where the score might be changing very frequently.  
 
Tip: Writing to storage many times per second can slow down your game, so updating the score table saved ‘on disk’ like you’re doing can have negative side-effects.
 
Assuming you have SSK2 installed in your project, using persist would look like this:
 
main.lua

ssk.persist.setDefault( "score.json", "highScore", 0 )

your code (elsewhere):

local score = 0 -- localize persist calls to speed up code local pGet = ssk.persist.get local pSet = ssk.persist.set -- local highScoreLabel = display.newText( score, 220, 243, native.systemFont, 24 ) -- local function checkForhighScore() if( score \> pGet( "score.json", "highScore" ) ) then pSet( "score.json", "highScore" , score ) highScoreLabel.text = tostring(score) end end -- local function dumpScoreData() print("High score: ", pGet( "score.json", "highScore" ) ) end

@roaminggamer

Sorry, I use google translator to communicate, because I started to learn English not so long ago.

Yes, the problem is that I’m using it incorrectly. Thank you for the solutions provided, as soon as I test them, I will write about the result)

@roaminggamer - I liked your SSK2 utility, it’s amazing.

Maybe I explained it wrong the first time.

I would like this line to write a high score:

local highScoreLabel = display.newText( score, 220, 243, native.systemFont, 24 )

I installed SSK2, inserted your code, but in the file “score.json” the value does not change: {“defaults”:{“highScore”:0}}.

I tried to use EGO before that. Everything worked perfectly, but I do not like the fact that a high score was shown after the application was restarted.

I hope you can help me fix this)

There are many ways to save data.  If I’m being honest, my loadsave code, as happy as I am with it’s success is overkill if you are only saving one value. If you have much more complex save data, it’s great. Take a Lua table, save a JSON file with one line of code. Load it back in when the app starts and you have a table of settings.

But since I created that, we’ve added new API’s that make setting storage and retrieval a bit easier.  Look at:

system.setPreferences() and system.getPreferences()

https://docs.coronalabs.com/api/library/system/setPreferences.html

https://docs.coronalabs.com/api/library/system/getPreference.html

But I don’t think storage of the value seems to be the main issue as much as it is tracking the current score value, the all time high score value, displaying the current score and displaying the all time high score.

For instance, you don’t need to store the current score. If your using modules for different scenes like using Composer, you may need to pass the current score value between your game scene and your game over/high score scene. You only need to store your all time high score. You need to test to see if your latest current score is greater than the all time high score and then store the high score if it’s higher.

The loadsave code is hard to mess up if you have a Lua table of values to save.

Rob

Please be aware, I didn’t answer with code using Rob’s solution because I like to answer with code I’m familiar with.

His code is great and will perfectly suit your needs / solve your problem.  

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/03/highScore.zip

That said, I’m going to give you a working example that shows you how to track ‘high’ score from run to run using SSK2 and persist.

You can modify my example to use Rob’s code easy-peasy  OK?

https://www.youtube.com/watch?v=Pc7eT4MZGPg&feature=youtu.be

@Rob Miracle - Yes, I read this, but for now it’s quite difficult for me. Thank you for responding)

@roaminggamer - Wow, thank you so much for such a detailed response)

I’ll try it tomorrow, I already have 4:25 a.m. and I need to sleep)

@roaminggamer - My enemy is my inattention.

I skipped this line:

checkForNewHighScore()

Thank you) I could not solve this without your help :slight_smile:

Hi,

I’m putting this here in case anyone in the future needs it https://marketplace.coronalabs.com/plugin/score-keeper

-dev