Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call a Lua function just by writing its name (without parentheses)

I'm looking forward to using a variable like "asdf" instead of writing the name function to check its return (which changes now and then). That's why "asdf" variable should update its value everytime we use (call) it

Is there any way to do this in Lua, please?

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())

thanks

like image 996
user2308704 Avatar asked Dec 07 '22 07:12

user2308704


1 Answers

--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 :
Solution without parenthesis

--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 ...
like image 84
Egor Skriptunoff Avatar answered May 20 '23 11:05

Egor Skriptunoff