Some of you may know this, but you can “unpack” a table into a list like this, so the items can be used as function parameters
a={1,2,3}
SomeFunction(unpack(a))
--is the same as
SomeFunction(1,2,3)
--and you can unpack into the end of a list
a={1,2,3}
SomeFunction(5,unpack(a))
--is the same as
SomeFunction(5,1,2,3)
--but if there are any parameters after your unpack statement, then only
--the first table item is used (this is deliberate, I forget the reason)
a={1,2,3}
SomeFunction(5,unpack(a),4)
--is the same as
SomeFunction(5,1,4)
Note the built in reference incorrectly tells you to unpack tables with tableName.unpack(). The correct syntax is unpack(tableName).
You can unpack vectors the same way, but the syntax is vectorName:unpack().
This sounds right up my street @ignatz for level design. Is it possible to store code for ‘Waves’ of aliens for my shoot em up in another class that has the table and call that? Does the class have to be placed on the canvas for me to access that class’ table data?
I ask as I’m looking to keep all the level data out of the main file to keep it as light as possible for processing speed and only call the ‘wave’ data when I need it
@Ignatz I’m not sure that makes it easier or not. The receiving function would need a variable for each table entry SomeFunction(a,b,c) or using SomeFunction(...) with arg={...} would be about the same as SomeFunction(tab) and reading the table tab. Passing the table followed by other variables wouldn’t have a problem of the missing variables in the table.
Actually, I never noticed the vector:unpack(). That’s very handy. It’s hard not to run into functions that seem more appropriate for points and ones that require positions. Though, it would be much nicer if you could unpack and obtain the full list in a set with other attributes.
It’s useful for making a shallow copy of tables (ie table must be one-dimensional). There is now the table.move command, but I find this method easier to use (less parameters needed)
a = {10, 15, 20}
b = {unpack(a)} --shallow copy (nb 1 dimensional)
c = a --just a pointer, not an independent copy
a[2] = a[2] + 100
print(unpack(a)) --10, 115, 20
print(unpack(b)) --10, 15, 20
print(unpack(c)) --10, 115, 20
Thanks, was more interested for understanding, seemed unpack would have to be slower if it’s a real call. Speed has always been fine, and the Pro is amazing.
@matkatmusic The way you show doesn’t work, but this does.
function setup()
r,g,b,a=123,123,123,255
t= {r,g,b,a }
drawColor = color(unpack(t))
end
function draw()
background()
fill(drawColor)
ellipse(200,200,50)
end