Hello!
I’ve been reading up on the hub and I saw a parameter called … What is it?
It does put all the arguments given into a table and you could print these arguments out with a for loop
local function PrintArgs(...)
local Args = (...)
for Index,Argument in pairs(Args) do
print(Argument)
end
end
This allows you to accept tuples in your code.
But what is a tuple? Is it a table?
It’s a variadic function. Basically, the 3 dots are any extra parameters that weren’t already defined. Here’s an example:
local function func(...)
local x = {...}
print(x)
end
func(2,4,"hi")
This example will print a table consisting of the parameters input in func
, which are 2, 4, ae
But, why does it error when I try to run it?
local function PrintArgs(...)
local Args = (...)
for Index,Argument in pairs(Args) do
print(Argument)
end
end
PrintArgs()
Think of a tuple as a loose table. I can’t really explain it well, but here’s a pack() function to give you an idea.
function pack(...)
return {...}
end
print(pack("one",2,"three")[2]) -- 2
Oops, my bad i’ve mistaken {} with ()
This one should work
local function PrintArgs(...)
local Args = {...}
for Index,Argument in pairs(Args) do
print(Argument)
end
end
Because pairs
is reading a string when it’s supposed to read a table. It should say that in the terminal output.
You’re also passing a nil value, which pairs
is trying to read from and failing.