2016-03-15 123 views
0

我有一堆像「購買升級」,「金額」等變量 我想要最有效的方式來保存這些變量,並在開始遊戲時加載它們,因爲你從未退出遊戲(每一個事情保持不變)。 所以,所有的設置和變量保持不變,直到您重置遊戲。保存和加載變量的最佳方式是什麼?

我在問,因爲我認爲這是一個巨大且非常重要的部分,我想從最好的技術開始。

你有什麼建議,我該如何在遊戲中實現?

+1

將這些值保存到文件並在需要時加載它們。查看json以獲取存儲lua表的簡單方法。關於您的問題,請參閱[問]或參加[旅遊] – Piglet

+1

@Piglet:您可以使用* Lua *來存儲Lua表格。使用JSON來存儲Lua表只是......愚蠢的。 –

+0

@NicolBolas,你會不會照顧我,爲什麼這麼傻? – Piglet

回答

1

您可以將它們存儲爲表格字段,然後使用many options for serializers之一對錶格進行序列化。另請參閱Lua編程中的Serialization chapter

+0

所以,基本上,我把我的所有變量放到一個表中並保存表。一旦啓動我的應用程序,我從某種文件加載該表? – FICHEKK

+0

@FICHEKK是的。就如此容易 – Piglet

0

有兩種方法可以很容易做到:

使用保存到您的DocumentDirectory一個簡單的文本文件:使用保存到您的DocumentDirectory一個SQLite文件

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):

--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 

您還可以使用基於雲的移動設備,實際上具有相同的功能。

相關問題