Table.pack & table.unpack With Functions Parameters

local function c(thing1, thing2, thing3)
    print(thing1) -- Outputs: 5
    print(thing2) -- Outputs: 1
    print(thing3) -- Outputs: nil
end

local function b(tbl)
    print(table.unpack(tbl)) -- Outputs: 5 6
    c(table.unpack(tbl), 1) -- [[Noted line #1]]
end

local function a(...)
    print(table.pack(...)) -- Outputs: {[1] = 5, [2] = 6, ["n"] = 2}
    b(table.pack(...))
end 

a(5, 6) -- [[Noted line #2]]

I’m trying to get the two arguments on noted line #2 and the one extra added argument on noted line #1 as parameters to the c function. However, thing3 keeps printing nil. How do I get the unpacked table to separate into two/amount of entries in the table different parameters?

I need three functions for this because in what I’m actually doing, each function is in a different module.

You can’t table.unpack and then add another value after. The way table.unpack works means that you’re just cutting off the rest of the tuple.
i.e. you cannot table.unpack(t), value I’m not sure if you can do it the other way around, but you can try, if order matters, you will have to add the values you want to the table before you unpack it. Such as:

table.insert(tbl, 1)
c(table.unpack(tbl))
1 Like