加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

仅通过写一个名字来调用Lua函数(没有括号)

发布时间:2020-12-15 00:16:55 所属栏目:大数据 来源:网络整理
导读:我期待使用像“asdf”这样的变量,而不是编写名称函数来检查它的返回(它会不时变化).这就是为什么“asdf”变量应该在我们每次使用(调用)它时更新它的值 请问在Lua有什么办法吗? asdf == getFunction() --we define it here (...) --some code if asdf 10 the
我期待使用像“asdf”这样的变量,而不是编写名称函数来检查它的返回(它会不时变化).这就是为什么“asdf”变量应该在我们每次使用(调用)它时更新它的值

请问在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 ...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读