Function 只需编写Lua函数的名称(不带括号)即可调用该函数

Function 只需编写Lua函数的名称(不带括号)即可调用该函数,function,variables,lua,Function,Variables,Lua,我期待使用像“asdf”这样的变量,而不是编写name函数来检查它的返回(有时会发生变化)。这就是为什么“asdf”变量应该在每次使用(调用)它时更新它的值 请问在卢阿有什么办法可以做到这一点吗 asdf == getFunction() --we define it here (...) --some code if asdf < 10 then ... --here we call the variable (so it should get/up

我期待使用像“asdf”这样的变量,而不是编写name函数来检查它的返回(有时会发生变化)。这就是为什么“asdf”变量应该在每次使用(调用)它时更新它的值

请问在卢阿有什么办法可以做到这一点吗

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())
asdf==getFunction()--我们在这里定义它
(…)--一些代码
如果asdf<10,则--这里我们调用变量(因此它应该再次获取/更新getFunction()的结果)
谢谢

——我们在这里定义它
本地asdf=函数()
返回getFunction()
结束
--一些代码
(...)            
--这里我们称之为变量
--(因此它应该再次获取/更新getFunction()的结果)
如果asdf()小于10,则。。。

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 ...
——我们在这里定义它
asdf=零
可设置元表(_G,{u_)=
函数(t,k)
如果k=='asdf',则
返回getFunction()
结束
结束
})
--一些代码
(...)
--这里我们称之为变量
--(因此它应该再次获取/更新getFunction()的结果)
如果asdf<10,则。。。

你这么讨厌括号吗?Lisp有这样一个功能(符号宏)@user2308704从你在评论中指出的到目前为止,你似乎试图解决错误的问题。也许你应该重新考虑你想做的事?答案表明,这样做本身并不是不可能的,但在大多数情况下肯定是不寻常的,应该是不必要的。是的,但这里的问题是,我必须使用括号“asdf()”,不是吗=(@user2308704-你的键盘上的括号有问题吗?耶!一个来自@EgorSkriptunoff的(很好!)答案是作为答案提供的,而不是作为评论!;)在更新版本的lua中,
\u ENV
不是比
\u G
更可取吗?@Julian-
\u G
更兼容
--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 ...