Lua - 执行存储在表中的函数 [英] Lua - Execute a Function Stored in a Table

查看:151
本文介绍了Lua - 执行存储在表中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够将函数存储到表中。但是现在我不知道如何调用它们。决赛桌将有大约100个电话,所以如果可能的话,我想像在foreach循环中一样调用它们。感谢!

I was able to store functions into a table. But now I have no idea of how to invoke them. The final table will have about 100 calls, so if possible, I'd like to invoke them as if in a foreach loop. Thanks!

以下是表格的定义:

Here is how the table was defined:

game_level_hints = game_level_hints or {}
game_level_hints.levels = {}
game_level_hints.levels["level0"] = function()
    return
    {
        [on_scene("scene0")] =
        {
            talk("hint0"),
            talk("hint1"),
            talk("hint2")
        },
        [on_scene("scene1")] =
        {
            talk("hint0"),
            talk("hint1"),
            talk("hint2")
        }
    }
end

Aaand函数定义:

Aaand the function definitions:

function on_scene(sceneId)
    -- some code
    return sceneId
end

function talk(areaId)
    -- some code
    return areaId
end

编辑:

我修改了函数,使它们有更多的上下文。基本上,他们现在返回字符串。 我希望发生的事情是,在调用函数结束时,我将有一个包含所有这些字符串的表(最好是levels表)。

I modified the functions so they'll have a little more context. Basically, they return strings now. And what I was hoping to happen is that at then end of invoking the functions, I'll have a table (ideally the levels table) containing all these strings.

推荐答案

asd = game_level_hints.levels["level0"]()

现在, asd 包含我需要的区域字符串。尽管理想情况下,我打算能够访问如下数据:

Now, asd contains the area strings I need. Although ideally, I intended to be able to access the data like:

asd[1][1]

访问它:

accessing it like:

asd["scene0"][1]

检索区域数据就足够了。我只需要解决按键问题。

to retrieve the area data would suffice. I'll just have to work around the keys.

谢谢,伙计们。

这篇关于Lua - 执行存储在表中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆