2016-03-15 122 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 

您还可以使用基于云的移动设备,实际上具有相同的功能。

相关问题