开发者

call a function with variable length of arguments

In my 开发者_StackOverflow社区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?


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


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

This covers everything including trailing nil arguments.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜