Page 1 of 1

[Solved]Table as an argument

Posted: Fri Oct 30, 2015 12:45 pm
by jurses
Is it possible pass a table as an argument, I've seen in love.graphics.polygon is possible like:

Code: Select all

local vertices = {100, 100, 200, 100, 150, 200}
 
-- passing the table to the function as a second argument
love.graphics.polygon('fill', vertices)
and what I want is

Code: Select all

function love.load()
triangle={a = { x = 20, y = 20},
          b = { x = 40, y = 20},
          c = { x = 30, y = 40}
          }
end

function love.draw()
love.graphics.polygon('fill',triangle)
end
Is that possible?

Re: Table as an argument

Posted: Fri Oct 30, 2015 12:48 pm
by TheMeq
I think if the table is formatted correctly, then yes.

I think if triangle={x1,y1,x2,y2,x3,y3} then it should work, I know it works for colours.

Code: Select all

-- Not Tested
function love.load()
    triangle={20,20,40,20,30,40}
end

function love.graphics()
    love.graphics.polygon('fill',triangle)
end

Re: Table as an argument

Posted: Fri Oct 30, 2015 1:02 pm
by zorg
Anything is possible!*

Code: Select all


local tri = {
a = { x = 20, y = 20}, 
b = { x = 40, y = 20}, 
c = { x = 30, y = 40},
}

function createTriangle(t)
  return {t.a.x, t.a.y, t.b.x, t.b.y, t.c.x, t.c.y}
end

function love.draw()
  love.graphics.polygon('fill',createTriangle(tri))
end
*Citation needed

Okay, okay, this is a very specific code snippet, so it has a dozen catches, and you really shouldn't do this, but meh. :3
Also, no such thing as a function/callback called love.graphics(), you probably meant love.draw()

Re: Table as an argument

Posted: Fri Oct 30, 2015 1:26 pm
by jurses
zorg wrote:Anything is possible!*

Code: Select all


local tri = {
a = { x = 20, y = 20}, 
b = { x = 40, y = 20}, 
c = { x = 30, y = 40},
}

function createTriangle(t)
  return {t.a.x, t.a.y, t.b.x, t.b.y, t.c.x, t.c.y}
end

function love.draw()
  love.graphics.polygon('fill',createTriangle(tri))
end
*Citation needed

Okay, okay, this is a very specific code snippet, so it has a dozen catches, and you really shouldn't do this, but meh. :3
Also, no such thing as a function/callback called love.graphics(), you probably meant love.draw()
Thanks that works, but I do not understand why I shouldn't do this.

Re: Table as an argument

Posted: Fri Oct 30, 2015 1:35 pm
by zorg
It's usually faster to not have an extra function sorely for putting some data into another representation where you could just use the form love.graphics.polygon asks for (the simple numerically indexed table that asks for vertex points, as in, coordinate pairs of your polygon).

That representation is generic, in that, if you want a septagon (7-sided polygon) instead of a triangle, you don't need to do anything, but have 8 more values in your table, the index can increase simply, and you have no problems.)

Using your labelled tables (the triangle table having a,b,c string keys, and x and y keys inside those tables) is hard because the code doesn't know what comes after 'c'; not to mention that you need to edit your converter function to deal with it.

Re: Table as an argument

Posted: Fri Oct 30, 2015 1:47 pm
by jurses
zorg wrote:It's usually faster to not have an extra function sorely for putting some data into another representation where you could just use the form love.graphics.polygon asks for (the simple numerically indexed table that asks for vertex points, as in, coordinate pairs of your polygon).

That representation is generic, in that, if you want a septagon (7-sided polygon) instead of a triangle, you don't need to do anything, but have 8 more values in your table, the index can increase simply, and you have no problems.)

Using your labelled tables (the triangle table having a,b,c string keys, and x and y keys inside those tables) is hard because the code doesn't know what comes after 'c'; not to mention that you need to edit your converter function to deal with it.
Ok, thanks for the tip :awesome: . Then I will use the direct value for the polygon.