How to use ellipsis (...) as parameters?

Hey! So I know there is a way to allow an infinite number of arguments to a function, and that’s using the (...) syntax.
I do not know, however, how to do it in any capacity. I’ve never really tried, and I can’t find a good article on it.

Could I get an explanation or a link please?

Thank you!

3 Likes
local function hahaha(...)
    local a, b, c = ...
    
    print(a, b, c)
end


hahaha(2, 4, 8, 9) -- 2 4 8

Looks like it is a real thing, it works. It is not really as used as much nor mentioned frequently. I should really use it more frequently.

12 Likes

Would you know if there’s possibly a way to combine all of the arguments into a table regardless of how many are given?

You can use the table.pack function and pass the tuple to create a table for indexing specific arguments:

local Args = table.pack(...) -- or {...}
print(Args[1]) -- The first arg of the tuple
7 Likes
local args = {...};

That would do the trick.

6 Likes