Loadsave module - Corona SDK

主宰稳场 提交于 2019-12-25 14:21:29

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/32552832/loadsave-module-corona-sdk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!