I am using Rob Miracle's loadsave module for my Corona SDK game
I have this little Enquiry on it
If I save a json table on mydata.lua
M={}
M.highScore = 0
M.levels=1
loadsave.saveTable(M,"settings.json")
return M
Now if in the game.lua...I do this....
function gameOver
If gamewin == false then
mydata.level = mydata.level + 1
gamewin = true
loadsave.saveTable(mydata,"settings.json")
end
Now if I do this will the loadsave module overwrite the whole json file and hence remove the high score parameter from there?
Please help
Yes. From the source of saveTable
definition:
function saveTable(t, filename)
local path = system.pathForFile( filename, system.DocumentsDirectory)
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
As you can see, the function uses io.open(path, "w")
to write to file. Since, io.open
creates an entirely new file when used with the write mode (w
parameter), the older file would be overwritten.
You can instead load the contents from the json file first, before writing over with new values:
function gameOver()
local mydata = loadsave.loadTable "settings.json"
if gamewin == false then
mydata.level = mydata.level + 1
gamewin = true
.
.
.
loadsave.saveTable(mydata,"settings.json")
end