3

In my lua script I need to call a function which takes an arbritary number of arguments with, well, an arbitrary number of arguments…

I am building up my arguments as a table as I cant know how many arguments there will be.

Sample code:

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 pretty new to lua, in PHP e. g. I would use call_user_func_array – is there something similiar in lua?

3 Answers 3

12

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

1
  • Thanks! That worked. I´ll accept this answer as soon as the system lets me. ;-)
    – Max
    May 22, 2011 at 8:56
3

The long answer can be found on the Lua Users' Wiki.

This covers everything including trailing nil arguments.

0

Just pass the table as the argument. No need to split it up into single arguments, just have the function loop through the table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.