Calling different functions with different amount of parameters within the same statement

So how would I achieve this?

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

Is there any way to achieve this?

1 Like

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

2 Likes

oh ok i will test it out thanks

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 :sweat_smile:

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")
1 Like

Oh, and you could just do

DoSomethingElse("A", "B", "C", ...)

but I’m guessing that’s not what you meant?

well yea I mean I think its impossible to do what i am trying to achieve sadly

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
local v = {1,2,3}

local modulefunction = ... different functions depends

modulefunction(Player, table.unpack(v))
1 Like

what thank you so much this works i appreciated so much

ps i am quite dumb

1 Like

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