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

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

我之所以问这个问题,是因为我认为这是一个巨大而且非常重要的部分,我想要用最好的技术开始。

你有什么建议和如何在我的游戏中实现呢?

点赞
用户1442917
用户1442917

你可以将它们存储为表字段,然后使用其中一个许多序列化选项之一对表进行序列化。另请参阅《Lua 编程》中的序列化章节

2016-03-15 15:47:58
用户3041972
用户3041972

有两种相当容易的方法:

1)使用保存在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

您也可以使用基于云的,这与更具移动性的几乎相同。

2016-03-18 05:14:28