function DoSomethingElse(word1,word2,word3)
print(word1)
print(word2)
print(word3)
end
function DoSomething()
local v = {
"A","B","C"
}
DoSomethingElse(v[1],v[2],v[3])
end
DoSomething()
--this is just some example code
So what I want to achieve is that
what if v has more variables like v = {“A”,“B”,“C”,“D” ,… ,“Z”}
I tried searching for a quite similar solution but not what I was quite looking for its called variadic function
ps I want to achieve this because I am calling multiple functions in the same statement and they have different amounts of parameters
If it can sometimes have less arguments, then some of the parameters will just display as nil.
If it can sometimes have more arguments, then add ... as the final parameter of the DoSomethingElse function. ... serves almost as an overflow array, and behaves like a table which contains every parameter that didn’t get an individual name
Uhh here’s some code that does something but I really don’t know what you’re trying to achieve. My bad if I’m completely misunderstanding
function insertAll(t, ...)
local toInsert = {...}
for _, value in toInsert do
table.insert(t, value)
end
return t
end
function flatten(...)
local result = {}
for _, value in {...} do
if typeof(value) == "table" then
for _, innerValue in value do --Don't recurse in this implementation, although that would be valid
table.insert(result, innerValue)
end
else
table.insert(result, value)
end
end
return result
end
function DoSomethingElse(...)
local words = {...}
for _, word in words do
print(word)
end
end
function DoSomething(...)
local preWords = {"A", "B", "C"}
--DoSomethingElse(table.unpack(insertAll(preWords, ...)))
DoSomethingElse(flatten(preWords, ...)) --I think I prefer this?
end
DoSomething("D", "E")
this is what i tried to achieve but i don’t think it’s possible
local v = {1,2,3}
local modulefunction = ... different functions depends
if #v == 1 then
modulefunction(Player,v[1])
elseif #v == 2 then
modulefunction(Player,v[1],v[2])
elseif #v == 3 then
modulefunction(Player,v[1],v[2],v[3]
end