Call a function with a variable argument length

In my lua script, I need to call a function that takes a conditional number of arguments with, well, an arbitrary number of arguments ...

I create my arguments as a table, because I can not know how many arguments will be.

Code example:

local result = call.someFunc();
local arguments = {}

for k,v in pairs(result) do
    table.insert(arguments, v.name)
end

-- here I would like to somehow pass the whole table and each item in the table
-- is then passed as a single argument to "someOtherFunc"
call.someOtherFunc(arguments[1], arguments[2], arguments[3] ....) 

I am new to lua, in PHP e. d. I would use call_user_func_array - is there something similar in lua?

+3
source share
3 answers

foo(unpack(arguments))equivalent to foo(arguments[1], arguments[2], ...).

+12
source

A long answer can be found on the Lua user wiki.

This covers everything, including the final arguments nil.

+3
source

. , .

0

All Articles