Lua - executing a function stored in a table

I managed to save the functions in a table. But now I have no idea how to call them. There will be about 100 calls in the final table, so if possible, I would like to call them as in a foreach loop. Thanks!

Here's 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 

Aa and function definitions:

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

EDIT:

I changed the functions to have a bit more context. Basically, they now return strings. And what I was hoping for is that at the end of the function call I will have a table (ideally a level table) containing all of these rows.

+6
source share
3 answers

Using what you guys answered and commented on, I was able to come up with the following code as a solution:

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

Now asd contains the row of areas I need. Although ideally I wanted to have access to data like:

 asd[1][1] 

access to it, for example:

 asd["scene0"][1] 

To get the data area will be enough. I just need to get around the keys.

Thanks guys.

+4
source

Short answer: to call a function (reference) stored in an array, you simply add (parameters) , as usual:

 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 

In fact, you can simplify this for

 local a = {myfunc = function(a,b,c) return a,b,c end} print(a.myfunc(3,4,5)) -- prints 3,4,5 

Long answer: you do not describe what the expected results are, but what you wrote will most likely not do what you expect from it. Take this snippet:

 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 are referring to the on_scene and talk functions, but you are not โ€œstoringโ€ these functions in a table (since you explicitly referred to them in your question, I assume that this is these functions). In fact, you call these functions and save the return values โ€‹โ€‹(they return nil ), so when this fragment is executed, you get the "index index no" error, because you are trying to save nil with nil as an index.

If you want to call the function that you saved in game_level_hints.levels["level0"] , just do game_level_hints.levels["level0"]()

+6
source

It is not clear what you are trying to do. Inside an anonymous function, you return a table that uses on_scene return value as keys. But your on_scene nothing. The same goes for talk .

I assume that you wanted on_scene and talk called when each level is called in the game_level_hints table.

If so, you can do this:

 local maxlevel = 99 for i = 0, maxlevel do game_level_hints.levels["level" .. i] = function() on_scene("scene" .. i) talk("hint" .. i) end end -- ... for levelname, levelfunc in pairs(game_level_hints.levels) do levelfunc() end 
+1
source

Source: https://habr.com/ru/post/947422/


All Articles