负载保存模块-Corona SDK [英] Loadsave module - Corona SDK

查看:79
本文介绍了负载保存模块-Corona SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的Corona SDK游戏使用Rob Miracle的loadsave模块



我对此有疑问



如果我在mydata.lua上保存一个json表

  M = {} 
M.highScore = 0
M.levels = 1

loadsave.saveTable(M, settings.json)

return M

现在,如果在游戏中。lua...我这样做....

  function gameOver 
如果gamewin == false,则
mydata.level = mydata.level + 1

gamewin = true

loadsave.saveTable(mydata, settings.json)

end

现在,如果执行此操作,loadsave模块将覆盖整个json文件,因此从那里删除高分参数?



请帮助

解决方案

是。来自 saveTable 定义的来源

 函数saveTable(t,文件名)
本地路径= system.pathForFile(文件名,system.DocumentsDirectory)
本地文件= io.open(路径, w)
if文件则
本地内容= json.encode(t)
file:write(目录)
io.close(文件)
返回true
else
返回false
结束
结束

如您所见,该函数使用 io.open(path, w)写入文件。因为 io.open 与写模式( w 参数)一起使用时会创建一个全新的文件,将会被覆盖。

  function gameOver()
local mydata = loadsave.loadTable settings.json
如果gamewin == false,则
mydata.level = mydata .level + 1
gamewin =真



loadsave.saveTable(mydata, settings.json)
结束


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

这篇关于负载保存模块-Corona SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆