Lua : Dynamicly calling a function with arguments.
Posted
by Tipx
on Stack Overflow
See other posts from Stack Overflow
or by Tipx
Published on 2010-03-14T05:40:07Z
Indexed on
2010/03/14
5:45 UTC
Read the original article
Hit count: 318
Using Lua, I'm trying to dynamicly call a function with parameters. What I want to have it done is I send a string to be parsed in a way that :
- 1st argument is a class instance "Handle"
- 2nd is the function to be called
- All that is left are arguments
"modules" is a a table like { string= } split() is a simple parser that returns a table with indexed strings
function Dynamic(msg)
local args = split(msg, " ")
module = args[1]
table.remove(args, 1)
if module then
module = modules[module]
command = args[1]
table.remove(args, 1)
if command then
if not args then
module[command]()
else
module[command](unpack(args)) -- Reference 1
end
else
-- Function doesnt exist
end
else
-- Module doesnt exist
end
end
When I try this with "ignore remove bob", by "Reference 1", it tries to call "remove" on the instance associated with "ignore" in modules, and gives the argument "bob", contained in a table (with a single value).
However, on the other side of the call, the remove function does not receive the argument. I even tried to replace the "Reference 1" line with
module[command]("bob")
but I get the same result.
© Stack Overflow or respective owner