Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua - Execute a Function Stored in a Table

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 the function definitions:

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

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

EDIT:

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.

like image 534
brain56 Avatar asked Jun 17 '13 01:06

brain56


People also ask

What is Lua table?

The Lua table is a built-in data structure is in Lua programming, which is used to create an array and dictionary in Lua. This is a guide to Lua Table. Here we discuss the Introduction, syntax, How to Work of table structure in Lua programming and example with code implementation respectively.

What is string format in Lua?

Lua uses tables in all representations including representation of packages. When we access a method string.format, it means, we are accessing the format function available in the string package. Tables are called objects and they are neither values nor variables. Lua uses a constructor expression {} to create an empty table.

Is it possible to use self in 4A Lua functions?

4 Lua functions use "self" in source but no metamethod allows to use them 0 Optimized Lua Table Searching Hot Network Questions How many red herrings is too many red herrings?

What is garbage collection in Lua programming?

When there are no reference to a table, then garbage collection in Lua takes care of cleaning up process to make these unreferenced memory to be reused again. An example is shown below for explaining the above mentioned features of tables. There are in built functions for table manipulation and they are listed in the following table.


2 Answers

Short answer: to call a function (reference) stored in an array, you just add (parameters), as you'd normally do:

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 to

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 don't describe what your expected results are, but what you wrote is likely not to do what you expect it to do. Take this fragment:

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.

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

like image 186
Paul Kulchenko Avatar answered Sep 20 '22 01:09

Paul Kulchenko


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

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

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.

Thanks, guys.

like image 44
brain56 Avatar answered Sep 21 '22 01:09

brain56