in game.lua i have
local composer = require( "composer" ) local loadsave = require( "loadsave" ) local score = require( "score" ) -- here you declare all your local variables: local score = 0 local highscore = 0 local scoreText = score.init( { fontSize = 40, x = 130, y = 130, maxDigits = 7, leadingZeros = false }) local function onCollision(event) if event.phase == "began" then print "collision happened" composer.gotoScene( "gameover",{ time=400, params=score } ) score.save() end -- here i have your code local scoreTable = { highScore = 0 } loadsave.saveTable( scoreTable, "scoreTable.json" ) local currentScore = score.get() local scorer = loadsave.loadTable( "scoreTable.json") if currentScore \> scorer.highScore then scorer.highScore = currentScore loadsave.saveTable( scorer, "scoreTable.json" ) end
and in score.lua i have
local M = {} M.score = 0 -- Set the score to 0 initially function M.init( options ) local customOptions = options or {} local opt = {} opt.fontSize = customOptions.fontSize or 24 opt.font = customOptions.font or native.systemFont opt.x = customOptions.x or display.contentCenterX opt.y = customOptions.y or opt.fontSize\*0.5 opt.maxDigits = customOptions.maxDigits or 6 opt.leadingZeros = customOptions.leadingZeros or false local prefix = "" if ( opt.leadingZeros ) then prefix = "0" end M.format = "%" .. prefix .. opt.maxDigits .. "d" -- Create the score display object M.scoreText = display.newText( string.format( M.format, 0 ), opt.x, opt.y, opt.font, opt.fontSize ) return M.scoreText end -- return M function M.set( value ) M.score = tonumber(value) M.scoreText.text = string.format( M.format, M.score ) end function M.get() return M.score end function M.add( amount ) M.score = M.score + tonumber(amount) M.scoreText.text = string.format( M.format, M.score ) end -- return M function M.save() local saved = system.setPreferences( "app", { currentScore=M.score } ) if ( saved == false ) then print( "ERROR: could not save score" ) end end function M.load() local score = system.getPreference( "app", "currentScore", "number" ) if ( score ) then return tonumber(score) else print( "ERROR: could not load score (score may not exist in storage)" ) end end return M
Could this may be because i have all this happening before scene:create,show,hide and destroy?
New Discovery: After tweaking a little, I find that moving your entire code before the collision loads the previous value in the scoreTable.json. So what I’ve tried now is making your code into 3 parts.
-- so here i have what saves the previous score local scoreTable1 = { Score = 0 } loadsave.saveTable( scoreTable1, "scoreTable1.json" ) local currentScore = score.get() local scorer1 = loadsave.loadTable( "scoreTable1.json") if currentScore \> scorer1.Score then scorer1.Score = currentScore loadsave.saveTable( scorer1, "scoreTable1.json" ) end local function onCollision(event) if event.phase == "began" then print "collision happened" composer.gotoScene( "gameover",{ time=400, params=score } ) score.save() end -- here I have what saves the current score local scoreTable2 = { Score = 0 } loadsave.saveTable( scoreTable2, "scoreTable2.json" ) local currentScore = score.get() local scorer2 = loadsave.loadTable( "scoreTable2.json") if currentScore \> scorer2.Score then scorer2.Score = currentScore -- save scores to JSON as scoreTable.json loadsave.saveTable( scorer2, "scoreTable2.json" ) end -- here is what determines the high score local scoreTable3 = { highScore = 0 } loadsave.saveTable( scoreTable3, "scoreTable3.json" ) local currentScore = score.get() local scorer3 = loadsave.loadTable( "scoreTable3.json") if scorer2.Score \> scorer1.Score then scorer3.highScore = scorer2.Score loadsave.saveTable( scorer3, "scoreTable3.json" ) elseif scorer2.Score \< scorer1.Score then scorer3.highScore = scorer1.Score loadsave.saveTable( scorer3, "scoreTable3.json" ) end
So this works but it only works for a few tries.
Say the score reads 3 first, then these will read
scorer1 = 0
scorer2 = 3
highscore = 3
now if I play again and the score reads 1
scorer1 = 3
scorer2 = 1
highscore = 3
now again and the score reads 2
scorer1 = 1
scorer2 = 2
highscore = 2
So should I try this method and it probably has something more that’s needed or needed to be taken away, or will it only ever be efficient for the first 2 games?