“High score” is always equal to “score”, and the highest value is not saved.
In game.lua
local function isNewHighScore () local isNewScore = score \> highscore if isNewScore then highscore = score end end local t = {} t.number = score loadsave.saveTable(t, "isNewScore.json", system.DocumentsDirectory) newTable = loadsave.loadTable("isNewScore.json", system.DocumentsDirectory) loadsave.printTable(newTable) highcoreText = display.newText( score, 220, 243, native.systemFont, 24 )
In loadsave.lua
local M = {} local json = require("json") local \_defaultLocation = system.DocumentsDirectory local \_realDefaultLocation = \_defaultLocation local \_validLocations = { [system.DocumentsDirectory] = true, [system.CachesDirectory] = true, [system.TemporaryDirectory] = true } function M.saveTable(t, filename, location) if location and (not \_validLocations[location]) then error("Attempted to save a table to an invalid location", 2) elseif not location then location = \_defaultLocation end local path = system.pathForFile( filename, location) local file = io.open(path, "w") if file then local contents = json.encode(t) file:write( contents ) io.close( file ) return true else return false end end function M.loadTable(filename, location) if location and (not \_validLocations[location]) then error("Attempted to load a table from an invalid location", 2) elseif not location then location = \_defaultLocation end local path = system.pathForFile( filename, location) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) return myTable end return nil end function M.changeDefault(location) if location and (not location) then error("Attempted to change the default location to an invalid location", 2) elseif not location then location = \_realDefaultLocation end \_defaultLocation = location return true end function M.print\_r ( t ) local print\_r\_cache={} local function sub\_print\_r(t,indent) if (print\_r\_cache[tostring(t)]) then print(indent.."\*"..tostring(t)) else print\_r\_cache[tostring(t)]=true if (type(t)=="table") then for pos,val in pairs(t) do if (type(val)=="table") then print(indent.."["..pos.."] =\> "..tostring(t).." {") sub\_print\_r(val,indent..string.rep(" ",string.len(pos)+8)) print(indent..string.rep(" ",string.len(pos)+6).."}") elseif (type(val)=="string") then print(indent.."["..pos..'] =\> "'..val..'"') else print(indent.."["..pos.."] =\> "..tostring(val)) end end else print(indent..tostring(t)) end end end if (type(t)=="table") then print(tostring(t).." {") sub\_print\_r(t," ") print("}") else sub\_print\_r(t," ") end print() end M.printTable = M.print\_r return M
newTable = loadsave.loadTable("isNewScore.json", system.DocumentsDirectory) local highscore = newTable.number -- I would use 'highscore' instead of 'number':) ... local highcoreText = display.newText( highscore , 220, 243, native.systemFont, 24 )
In the case high score changed you can use
highcoreText.text = highscore
What is more you can find isNewScore.json by select File->Show Project Sandbox in Simulator. In new window go to Documents folder. So after highscore should be saved you can check what highscore value is.
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()
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:
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 - 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.
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()
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.
newTable = loadsave.loadTable("isNewScore.json", system.DocumentsDirectory) local highscore = newTable.number -- I would use 'highscore' instead of 'number':) ... local highcoreText = display.newText( highscore , 220, 243, native.systemFont, 24 )
In the case high score changed you can use
highcoreText.text = highscore
What is more you can find isNewScore.json by select File->Show Project Sandbox in Simulator. In new window go to Documents folder. So after highscore should be saved you can check what highscore value is.