2014-02-27 32 views
0

我有一個菜單屏幕,有一個選項按鈕。當我點擊這個按鈕時,它將加載options.lua文件非常好。Love2d - 我如何卸載一個文件.lua

love.filesystem.load("options.lua")() love.load() 

在選項屏幕上我想添加一個返回按鈕返回到主菜單。在我的頭上這樣做,我需要卸載options.lua文件。

function love.mousepressed(x, y) 
    if x > 275 and x < 320 and y > 305 and y < 325 then 

    end 
end 

回答

0

沒有辦法「卸載」該文件;你只能否定它的加載效果。您在該片段中執行options.lua的內容,如果它有類似a = 5的內容,要撤消此更改,則需要在執行options.lua中的代碼之前保存a的值,然後再恢復該值。

像這樣的事情可能會爲a工作:

local default = {} 
default.a = a -- save the current value of a (if any) 
love.filesystem.load("options.lua")() love.load() 
function love.mousepressed(x, y) 
    if x > 275 and x < 320 and y > 305 and y < 325 then 
     a = default.a 
    end 
end 

你可以通過任何其他值(例如,必須要還原名稱列表)。如果需要,可以將所有值保存在全局表中,稍後通過迭代pairs(_G)來恢復它們。如果您處理表格而不是簡單的值,則需要使用deep copy來保存和恢復值。

+0

所以我加入前3行代碼的按鈕,在打開的頁面options.lua在main.lua其餘的選項。 lua在後面的按鈕。但我得到一個「試圖索引全球'默認'(一個零值)」錯誤 – Ross

+0

你需要顯示更多的代碼,然後... –

1

雖然保羅的回答是一個選項,你可以不同的方法處理這個: 您正在考慮這一點的方式,將你造成很多麻煩,因爲在Lua,和許多其他語言加載文件並不意味着作爲在可變時間運行代碼的方式。雖然這可能會讓你改變更多的代碼,但是可以考慮爲GUI狀態創建一個變量,並且如果該變量具有特定的值,那麼只繪製你想要的變量。例如:

添加到您的love.load功能:

function love.load() 
     -- Set the gui state to the defualt (the main menu) 
     guistate = "menu" -- You probably should use an integer, am using a string for the purpose of clarity. 
    end 

這又在同一個地方:

function love.mousepressed(x, y) 
    if x > 275 and x < 320 and y > 305 and y < 325 then 
     -- The back button is pressed, change the state to the menu. 
     guistate = "menu" 
    end 
end 

添加到您的love.draw功能:

function love.draw() 
    if guistate = "menu" then 
    -- Draw the stuff for your main menu 
    elseif guistate = "options" then 
    -- Draw the stuff for your options menu. 
    end 
end 

額外

也看看這些GUI庫,如果你有興趣: Love FramesQuickie