[SOLVED] Question : Use table as argument

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
Le Codex
Prole
Posts: 31
Joined: Thu Feb 09, 2017 10:56 am
Location: France
Contact:

[SOLVED] Question : Use table as argument

Post by Le Codex »

Hi,

I'm currently working a little project which goal is to facilitate the adding of Particle Systems.
I'm using a function called initPartSyst() that demands all the useful informations for a Particle Sytsem (x, y, buffer, time, ...)
The problems come with the colors part. To parameter the color of a Particle System, you must use ps:setColors(color1, color2,...) which can take 4-long tables as input.

My question is this: Can I put all those tables in a big table and use it as arguments? :huh:

I put the code so you can see why I'm asking this more in detail:

Code: Select all

function love.load()
   img = love.graphics.newImage("Textures/particle.png")
   emitters = {}
end

function love.update(dt)
   for i,v in ipairs(emitters) do
    v:update(dt)
   end
end

function love.draw()
   love.graphics.setBlendMode("add")
   for i,v in ipairs(emitters) do
    love.graphics.draw(v)
   end
end

function love.mousepressed(x, y, button)
  if button == 1 then table.insert(emitters, initPartSys(img, x, y, 1500, 0.1, 0.5, -200, -2000, 200, 100, 3000, {234, 217, 30, 128, 255, 0, 0, 0})) end --Fire
  if button == 2 then table.insert(emitters, initPartSys(img, x, y, 1500, 0.1, 0.5, -200, -100, 200, 2000, 3000, {1, 255, 255, 128, 0, 0, 255, 0})) end --Water
  if button == 3 then table.insert(emitters, initPartSys(img, x, y, 1500, 0.1, 0.5, -500, -500, 500, 500, 3000, {255, 255, 255, 255, 255, 255, 255, 0})) end --Smoke
end

function initPartSys(image, x, y, maxParticles, mintime, maxtime, minx, miny, maxx, maxy, rate, colors)
   local ps = love.graphics.newParticleSystem(image, maxParticles)
   ps:setPosition(x, y)
   ps:setParticleLifetime(mintime, maxtime)
   ps:setSizeVariation(1)
   ps:setEmissionRate(rate or 100)
   ps:setLinearAcceleration(minx, (miny or minx), maxx, (maxy or maxx))
   colorarg = {}
   for i=0,#colors - 1 do
     if i%4 == 0 then table.insert(colorarg, {}) end --Add a new color array every 4 variables
     table.insert(colorarg[math.floor(i/4)+1], colors[i+1]) --Insert each variable of colors in the proper color array
   end
   ps:setColors(colorarg) -- Colorarg contains the informations of colors, segmented in multiples 4-long tables.
   return ps
end
I use also an image called 'img' in the code which is a circle colored with a radial gradiant form white to transparent. But it works also properly with just a canvas with a circle in it.
If a solution exists (not necessarily what I've planned, it can be another way to do it), I'll be glad to know it ^^ .
Last edited by Le Codex on Sat Jul 01, 2017 5:41 pm, edited 1 time in total.

Code: Select all

if your.timeSpeed > 0 then universe:update(dt) else universe:destroy() end
User avatar
zorg
Party member
Posts: 3449
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Question : Use table as argument

Post by zorg »

Here's what the wiki says about ParticleSystem:setColors
Arguments can be passed in groups of four, representing the components of the desired RGBA value, or as tables of RGBA component values, with a default alpha value of 255 if only three values are given. At least one color must be specified. A maximum of eight may be used.
So either listing everything in a table like {1,2,3,4,5,6,7,8} would work (as long as the number of parameters you had was divisible by 4), or have it nested, like {{1,2,3,4},{5,6,7,8}}; the trick is that you need to use the unpack function.

Code: Select all

-- either of the following should work
initPartSys(img, x, y, 1500, 0.1, 0.5, -200, -2000, 200, 100, 3000, {{234, 217, 30, 128}, {255, 0, 0, 0}})
initPartSys(img, x, y, 1500, 0.1, 0.5, -200, -2000, 200, 100, 3000, {234, 217, 30, 128, 255, 0, 0, 0})



function initPartSys(image, x, y, maxParticles, mintime, maxtime, minx, miny, maxx, maxy, rate, colors)
   local ps = love.graphics.newParticleSystem(image, maxParticles)
   ps:setPosition(x, y)
   ps:setParticleLifetime(mintime, maxtime)
   ps:setSizeVariation(1)
   ps:setEmissionRate(rate or 100)
   ps:setLinearAcceleration(minx, (miny or minx), maxx, (maxy or maxx))
   ps:setColors(unpack(colors)) -- unpack the table containing the values it expects
   return ps
end
(Note that if unpack doesn't work, try table.unpack; to be honest, i don't remember which one works in löve.)

On the other hand, your original code didn't unpack the nested tables you created inside the function, so if it works like that as well, you don't even need to use unpack.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Le Codex
Prole
Posts: 31
Joined: Thu Feb 09, 2017 10:56 am
Location: France
Contact:

Re: Question : Use table as argument

Post by Le Codex »

Thank you!
I tried everything you told, but never with the unpack function... I admit that will help me a lot!

Code: Select all

if your.timeSpeed > 0 then universe:update(dt) else universe:destroy() end
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest