As expected, this code:
s = "bar"
bar = function() print(s) end
_G[s]()
outputs:
bar
but either this:
s = "bar"
foo = {
bar = function() print(s) end,
_G["foo." .. s]()
}
or that:
s = "bar"
foo = {
bar = function() print(s) end
}
_G["foo." .. s]()
outputs:
try to call the field '?' (nil value)
stack trace:
test.lua: 4: in the main piece
[C] :?
How to call a non-global function from a string variable?
source
share