简短的回答:要调用存储在数组中的函数(引用),您只需添加(parameters)
,就像你通常做的那样:
local function func(a,b,c) return a,b,c end
local a = {myfunc = func}
print(a.myfunc(3,4,5)) -- prints 3,4,5
事实上,你可以将其简化为
local a = {myfunc = function(a,b,c) return a,b,c end}
print(a.myfunc(3,4,5)) -- prints 3,4,5
长答案:你没有描述你的预期结果是什么,但你写的很可能not做你期望它做的事情。拿这个片段:
game_level_hints.levels["level0"] = function()
return
{
[on_scene("scene0")] =
{
talk("hint0"),
}
}
end
[This paragraph no longer applies after the question has been updated] You reference on_scene
and talk
functions, but you don't "store" those functions in the table (since you explicitly referenced them in your question, I presume the question is about these functions). You actually call these functions and store the values they return (they both return nil
), so when this fragment is executed, you get "table index is nil" error as you are trying to store nil
using nil
as the index.
如果你想调用你存储的函数game_level_hints.levels["level0"]
,你就这么做game_level_hints.levels["level0"]()