Page 1 of 1

Returning a List of Variables (x, y, z, ...) From a Table

Posted: Fri Nov 06, 2015 8:05 pm
by Rhinoceros
Hello, I am currently trying to make enemy typing in a game I am making. I am using HardonCollider (HC) to construct polygons for enemy shapes, and for some reason it hates it when I try to pass a table as an argument, instead of a list of raw numbers (in the form func(1,2,3,4,5,6,etc.)). Is there a way to write a function that returns all the values of a table in the form of a,b,c,d,e,f,etc. ?

Something like:

function foo(t)
return t[1], t[2], t[3], t[4], t[5], t[6], etc.
end

But without having to write out a million billion table references.

Re: Returning a List of Variables (x, y, z, ...) From a Tabl

Posted: Fri Nov 06, 2015 8:06 pm
by undef

Code: Select all

unpack( t )

Re: Returning a List of Variables (x, y, z, ...) From a Tabl

Posted: Fri Nov 06, 2015 8:30 pm
by vrld
For completeness, here is another way that you really shouldn't use because it is way slower and also less readable than unpack (but the technique is good to know anyway):

Code: Select all

function my_unpack_helper(n, t, ...)
    if n == 0 then  -- end recursion
        return ... 
    end
    return my_unpack_helper(n-1, t, t[n], ...) -- observe the power of tail recursion!
end

function my_unpack(t)
    return my_unpack_helper(#t, t)
end