所以这是我写的一些代码:
local fileFunc = {createFolder, openObj, deleteObj, createTxt, theMenu}
setmetatable(fileFunc, mt)
function fileSys()
local fileAction, code
print("The File System")
print("You are currently at "..current_window)
while true do
print("1 Create a new Folder\n2 Open an object\n3 Delete an Object\n4 Create a new text file\n5 Other options")
fileAction = userInInt()
code = fileFunc[fileAction]()
if code > 3 then invRet("fileSys()", code) end
if code == 1 then return 0
else return code end
end
end
我认为通过使用元__index
方法,不会有错误,但它仍然会抛出attempt to call field ?
错误。我猜它仍然会抛出错误,所以有没有办法可以使用它来捕捉它pcall()
mt
看起来像这样:
local mt = { __index = invalid }
并且无效:
function invalid()
print("Invalid operand, please try again.")
end
仅当用户输入未在表中列出的操作数时才会引发此错误 ( input > #fileFunc
)