Lua:动态调用带有参数的函数

发布于 2024-08-25 19:04:18 字数 1306 浏览 7 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故笙诉离歌 2024-09-01 19:04:18

声明function TIF_Ignore:remove(name) 等同于function TIF_Ignore.remove(self, name)。请注意第一种情况中冒号的使用,它添加了额外的隐藏参数来模拟 OOP 和类。调用该函数的方式是传递“bob”作为 self 参数,而不是 name

要解决此问题,您可以将 remove 设置为“静态函数”,如下所示:function TIF_Ignore.remove(name)。但是,您还必须以类似的方式更改 rawremoverawremovetmp 声明和调用站点。另一个(更简单的)选项是不从 args 表中删除 module,它应该是传递的第一个参数。

The declaration function TIF_Ignore:remove(name) is equivalent to function 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 the self parameter instead of name.

To fix this you can make remove "static function" like this: function TIF_Ignore.remove(name). However, you'll also have to change rawremove and rawremovetmp in a similar fashion, both declaration and calling site. The other (easier) option is not to remove module from the args table, it should be the first parameter passed.

划一舟意中人 2024-09-01 19:04:18

如果要调用使用冒号 : 语法定义的函数,则必须向其传递一个附加参数,即它所期望的表。因为您给出的特定示例不使用 self,所以您可以切换到点 . 语法,但如果您需要完整的通用性,请查看下面的代码

function Dynamic(msg)
    local args   = split(msg, " ")
    local module = table.remove(args, 1)
    if module and modules[module] then
        module = modules[module]
        local command = table.remove(args, 1)
        if command then
            local command = module[command]
            command(module, unpack(args))
        else
            -- Function doesnt exist
        end
    else
        -- Module doesnt exist
    end
end

:还修复了一些小问题:

  • 变量应该是本地
  • 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 use self, you could switch to the dot . syntax, but in case you need the full generality, look to the code below:

function Dynamic(msg)
    local args   = split(msg, " ")
    local module = table.remove(args, 1)
    if module and modules[module] then
        module = modules[module]
        local command = table.remove(args, 1)
        if command then
            local command = module[command]
            command(module, unpack(args))
        else
            -- Function doesnt exist
        end
    else
        -- Module doesnt exist
    end
end

I have also fixed a number of minor problems:

  • Variables should be local.
  • args is always non-nil.
  • Lookup modules[module] might fail.
  • table.remove returns the element removed, and it's OK to call it on an empty table.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文