Actually had some code on a remote backup server, this saves the scores but you can easily expand.
Im saving 4 scores, thats all in this. Ignore the console.returnScore() as thats a .lua require thats controls a control in the game which displays and tracks the scores. This is a required files as well and I call loadScore and saveScore from a different bit of the app
[code]
– File Score - fileScore.lua
– Version 1
module(…, package.seeall)
– Load Score
function loadScore()
local path = system.pathForFile( “scores.txt”, system.DocumentsDirectory )
local file = io.open( path, “r” )
local returnScore
– Load File
if file then
local loadedScores
loadedScores = file:read( “*a” )
local json = Json.Decode( loadedScores )
io.close( file )
if json ~= nil then
returnScores = { json[“loadedScores1”] , json[“loadedScores2”] , json[“loadedScores3”] , json[“loadedScores4”] }
else
returnScores = { 0 , 0 , 0 , 0 }
end
else
returnScores = { 0 , 0 , 0 , 0 }
end
return returnScores[1],returnScores[2],returnScores[3],returnScores[4]
end
– Save Score
function saveScore()
– Load Previous Scores
local loadedScores = {}
loadedScores[1],loadedScores[2],loadedScores[3],loadedScores[4] = fileScore.loadScore()
– Get Current Score
local globalScore = console.returnScore()
table.insert(loadedScores,globalScore)
– Sort Scores
table.sort(loadedScores, function(a,b) return a>b end)
– Make Save String
local saveScores = {
[“loadedScores1”] = loadedScores[1],
[“loadedScores2”] = loadedScores[2],
[“loadedScores3”] = loadedScores[3],
[“loadedScores4”] = loadedScores[4]
}
local json = Json.Encode(saveScores)
– Save File
local path = system.pathForFile( “scores.txt”, system.DocumentsDirectory )
local file = io.open( path, “w+” )
file:write( json )
io.close( file )
end
[/code] [import]uid: 5354 topic_id: 1962 reply_id: 5811[/import]