Jump to content




calling variables from string names


  • You cannot reply to this topic
2 replies to this topic

#1 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 09 June 2013 - 10:13 AM

How can I call a variable from a string name? I mean like if I enter the name of a variable it returns the value.

#2 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 09 June 2013 - 10:26 AM

Solution #1:
_G[name]()
Where _G is the table the variable is member of. If the variable is local, you can't do this, unless you put it into a local table. If it's an ordinary function like fs.open, it's part of _G, a global table provided by Lua. name is a name like "fs.open" without parentheses.

Solution #2:
I think you want your program to work like the interactive Lua prompt. In that case you should loadstring the input. So...
local func = loadstring(name)
if not func then func = loadstring("return " .. name) end
if func then func() end
name is a name like "fs.open()" with parentheses.

#3 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 09 June 2013 - 10:32 AM

thanks! That's all I needed.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users