knuxyl wrote: ↑Sat Mar 07, 2020 10:24 pm
Perfect, thanks so much for your help! One more question, if ... is more than one variable, then is it stored as a table? like to reference the second variable in the passed arguments, would it be like this
Code: Select all
function something(...)
x = ...
print(x[2])
end
No, it's a tuple, not a table. However, you can pass a tuple to a table constructor and then you can do what you want. Tuples are just multiple values, and they are handled specially.
Code: Select all
function something(...)
local a, b = ... -- sets a to the first element and b to the second, ignores the rest
local x = {...} -- constructs a table with all arguments
print(x[2]) -- prints the second argument (taking it from the table)
print(b) -- prints the same
local c = select(2, ...)
print(c) -- prints the same
end
select() is a system function that takes an index (or the string "#") and returns all arguments starting in that position (or the number of arguments):
Code: Select all
print(select(2, "a", "b", "c")) -- prints: b c
print(select(3, "a", "b", "c")) -- prints: c
print(select("#", "a", "b", "c")) -- prints: 3
When you pass it '...', each element in the tuple is passed as an additional argument, and select() returns the argument at the index you want.