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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Rhinoceros
Prole
Posts: 35
Joined: Mon Jun 11, 2012 2:59 am
Location: Under the Floorboards

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

Post by Rhinoceros » Fri Nov 06, 2015 8:05 pm

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.

User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

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

Post by undef » Fri Nov 06, 2015 8:06 pm

Code: Select all

unpack( t )
twitter | steam | indieDB

Check out quadrant on Steam!

User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

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

Post by vrld » Fri Nov 06, 2015 8:30 pm

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
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine

Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests