Lua:动态调用带有参数的函数
使用 Lua,我尝试动态调用带参数的函数。 我想发送一个要以以下方式解析的字符串:
- 第一个参数是类实例“Handle”
- 第二个是要调用的函数
- 剩下的都是参数
“modules”是一个像 { string=< 一样的表;类的实例> }
split() 是一个简单的解析器,它返回带有索引字符串的表。
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 函数不接收参数。我什至尝试将“参考1”行替换为,
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”。当我执行“忽略删除鲍勃”时,或者当我将“参考1”上的“解包(参数)”替换为“鲍勃”时,“删除”中的变量“名称”仍然为零。
Using Lua, I'm trying to dynamically call a function with parameters.
I want to 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=<instance of a class> }
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.
Here is the other function, which does not receive the argument "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
I added "Reference 2" in my code when I was trying to figure out what was wrong. When I do "ignore remove bob", or when I replace the "unpack(args)" with "bob" on "Reference 1", the variable "name" in "remove" is still nil.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
声明
function TIF_Ignore:remove(name)
等同于function TIF_Ignore.remove(self, name)
。请注意第一种情况中冒号的使用,它添加了额外的隐藏参数来模拟 OOP 和类。调用该函数的方式是传递“bob”作为self
参数,而不是name
。要解决此问题,您可以将
remove
设置为“静态函数”,如下所示:function TIF_Ignore.remove(name)
。但是,您还必须以类似的方式更改rawremove
和rawremovetmp
声明和调用站点。另一个(更简单的)选项是不从args
表中删除module
,它应该是传递的第一个参数。The declaration
function TIF_Ignore:remove(name)
is equivalent tofunction TIF_Ignore.remove(self, name)
. Note the use of colon in the first case, it adds extra hidden parameter to emulate OOP and classes. The way you call the function, you pass "bob" as theself
parameter instead ofname
.To fix this you can make
remove
"static function" like this:function TIF_Ignore.remove(name)
. However, you'll also have to changerawremove
andrawremovetmp
in a similar fashion, both declaration and calling site. The other (easier) option is not to removemodule
from theargs
table, it should be the first parameter passed.如果要调用使用冒号
:
语法定义的函数,则必须向其传递一个附加参数,即它所期望的表。因为您给出的特定示例不使用self
,所以您可以切换到点.
语法,但如果您需要完整的通用性,请查看下面的代码:还修复了一些小问题:
本地
。args
始终为非零。modules[module]
可能会失败。table.remove
返回删除的元素,在空表上调用它是可以的。If you want to call a function defined with the colon
:
syntax, you have to pass it an additional argument, namely the table it is expecting. Because the particular example you give does not useself
, you could switch to the dot.
syntax, but in case you need the full generality, look to the code below:I have also fixed a number of minor problems:
local
.args
is always non-nil.modules[module]
might fail.table.remove
returns the element removed, and it's OK to call it on an empty table.