保存和加载变量的最佳方法是什么?

时间:2016-03-15 13:53:51

标签: lua corona

我有一堆变量,如"升级购买","金额"等等 我想要最有效的方法来保存这些变量并在开始游戏时加载它们,因为你甚至从未退出游戏(每一件事都保持不变)。 因此,所有设置和变量保持不变,直到您重置游戏。

我问,因为我认为这是一个非常重要的部分,我想从最好的技术开始。

您有什么建议,如何在游戏中实现?

2 个答案:

答案 0 :(得分:1)

您可以将它们存储为表格字段,然后使用many options for serializers之一序列化表格。另请参阅Lua中的编程中的Serialization chapter

答案 1 :(得分:0)

有两种方法可以轻松完成:

使用保存到DocumentDirectory的简单文本文件:

local filePath = system.pathForFile( "data.txt", system.DocumentsDirectory )
local file = io.open( filePath, "r" )
if file then
    -- read all contents of file into a string
    local contents = file:read( "*a" )

    print( "Contents of " .. filePath )
    print( contents )

    io.close( file )

    local t = display.newText( "Contents of ", 5, 80, nil, 16 );
    t:setFillColor( 1, 1, 136/255 );
    local t = display.newText( filePath, 5, 100, nil, 10 );
    t:setFillColor( 1, 1, 136/255 );

    local ylast = 130
    for line in io.lines(filePath) do  
        local t = display.newText( line, 15, ylast, nil, 14 );
        t:setFillColor( 1, 1, 1 );  
        ylast = ylast + 20
    end

else
    print( "Creating file..." )

    -- create file b/c it doesn't exist yet
    file = io.open( filePath, "w" )
    file:write( "Feed me data!\n" )
    local numbers = {1,2,3,4,5,6,7,8,9}
    file:write( numbers[1], numbers[2], numbers[3], "\n" )
    for _,v in ipairs( numbers ) do
        file:write( v, " " )
    end
    file:write( "\nNo more data\n" )
    io.close( file )

    local t = display.newText( "Created file at:", 5, 80, nil, 16 );
    t:setFillColor( 1, 1, 136/255 );
    local t = display.newText( filePath, 5, 100, nil, 10 );
    t:setFillColor( 1, 1, 136/255 );
    local t = display.newText( "Run app again to test file read.", 5, 130, nil, 12 );
    t:setFillColor( 1, 1, 136/255 );

    -- This removes the file just created
    -- os.remove( filePath )
end

2)使用保存到DocumentDirectory的sqlite文件:

--Include sqlite
require "sqlite3"
--Open data.db.  If the file doesn't exist it will be created
local path = system.pathForFile("data.db", system.DocumentsDirectory)
db = sqlite3.open( path )   

--Handle the applicationExit event to close the db
local function onSystemEvent( event )
        if( event.type == "applicationExit" ) then              
            db:close()
        end
end

--Setup the table if it doesn't exist
local tablesetup = [[CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, content, content2);]]
print(tablesetup)
db:exec( tablesetup )

--Add rows with a auto index in 'id'. You don't need to specify a set of values because we're populating all of them
local testvalue = {}
testvalue[1] = 'Hello'
testvalue[2] = 'World'
testvalue[3] = 'Lua'
local tablefill =[[INSERT INTO test VALUES (NULL, ']]..testvalue[1]..[[',']]..testvalue[2]..[['); ]]
local tablefill2 =[[INSERT INTO test VALUES (NULL, ']]..testvalue[2]..[[',']]..testvalue[1]..[['); ]]
local tablefill3 =[[INSERT INTO test VALUES (NULL, ']]..testvalue[1]..[[',']]..testvalue[3]..[['); ]]
db:exec( tablefill )
db:exec( tablefill2 )
db:exec( tablefill3 )

--print the sqlite version to the terminal
print( "version " .. sqlite3.version() )

--print all the table contents
for row in db:nrows("SELECT * FROM test") do
  local text = row.content.." "..row.content2
  local t = display.newText(text, 20, 120 + (20 * row.id), native.systemFont, 16)
  t:setFillColor(1,0,1)
end

您还可以使用基于云的移动设备实际上是相同的。