使用Lua,我试图动态地调用带参数的函数。 我想发出一个字符串的方式,解析:Lua:动态调用带参数的函数
- 第一个参数是一个类的实例“处理”
- 第二点是被称为
- 所有剩下的参数的函数
“模块”是像{ string=<instance of a class> }
分裂()是一个简单的解析器返回表与索引字符串AA表。
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
当我尝试这与“忽略删除鲍勃”,由“参考文献1”,它试图呼吁与模块“忽略”相关的实例“删除”,并给出了说法“鲍勃”,包含在一个表中(具有单个值)。
但是,在调用的另一端,remove函数不接收参数。我甚至试图用
module[command]("bob")
替代“但是我得到了同样的结果。
这里是其它功能,不接受的说法"bob"
:
function TIF_Ignore:remove(name)
print(name) -- Reference 2
TIF_Ignore:rawremove(name)
TIF_Ignore:rawremovetmp(name)
print(title.. name.. " is not being ignored.")
end
我的代码中增加了“参考文献2”时,我试图找出什么是错的。当我做“忽略删除bob”,或者当我用“参考1”中的“bob”替换“unpack(args)”时,“remove”中的变量“name”仍然是零。
你的代码看起来不错。 “另一方”是什么样的。 – 2010-03-14 06:43:53
功能'split'看起来像什么,因为它不是标准Lua的一部分。 – 2010-03-14 07:01:32