我期待使用像“asdf”这样的变量,而不是编写名称函数来检查它的返回(它会不时变化).这就是为什么“asdf”变量应该在我们每次使用(调用)它时更新它的值 请问在Lua有什么办法吗? as
请问在Lua有什么办法吗?
asdf == getFunction() --we define it here
(...) --some code
if asdf < 10 then ... --here we call the variable (so it should get/update again the result of getFunction())
谢谢
--we define it here local asdf = function () return getFunction() end --some code (...) --here we call the variable --(so it should get/update again the result of getFunction()) if asdf() < 10 then ...
UPD:
没有括号的解决方案
--we define it here
asdf = nil
setmetatable(_G, {__index =
function(t, k)
if k == 'asdf' then
return getFunction()
end
end
})
--some code
(...)
--here we call the variable
--(so it should get/update again the result of getFunction())
if asdf < 10 then ...
