A question about question pramaters

Hello everyone,

I was looking through scripts and noticed some functions/events having the ... in their parameters, now I searched in the wiki but couldn’t find a good explanation. Can someone explain it to me?

Thank you.

The ... parameter is used to condense all parameters into a single variable. It converts them into an array which you can then access by indexing.

Here’s an example:

-- Normal Function
function addThreeNumbers(a, b, c)
    return a + b + c
end

-- The "..." Function (forgot what the actual name is)
function addThreeNumbers(...)
    return ...[1] + ...[2] + ...[3]
end

Both functions will work the same and return the same result when you run addThreeNumbers(5, 8, 1), the only difference is that the first (normal) function will make 5 its a parameter, 8, its b parameter, and 1 its c parameters, meanwhile the ... function will take all of the numbers and make them into one table [5, 8, 1], which it will then make its ... parameter.

This is incredibly useful when working with functions that do not have a standard set of parameters. For instance, the print function is one. You can pass as many arguments as you want, and it will join them all with \t. Here is how that would (possibly) work:

function print(...)
    local finalString = ...[1]
    for i = 2, #..., 1 do
        local temp = ...[i]
        finalString = finalString.."\t"..temp
    end

    print(finalString) -- This would be the OS-default print function
end

Of course, I am not sure if this would work out of the box, but this is the general concept of .... It is made for condensing all variables into an array, which you can then access. Hopefully this made you understand. If you have any questions, do let me know.

EDIT: The code above will NOT work out of the box. You must first convert ... into an array by doing something like local argTable = {...}. Only then can you access it by referencing the table. So in the code above, every time you see ..., simply replace it with argTable and make sure you define argTable right at the function start.

3 Likes

They are used for a Variadic function, or a Tuple like for example with print, where they take in any value.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.