[Solved]Table as an 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
jurses
Prole
Posts: 9
Joined: Sun May 03, 2015 10:23 am
Location: Canary Islands

[Solved]Table as an argument

Post 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?
Last edited by jurses on Fri Oct 30, 2015 1:27 pm, edited 2 times in total.
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

Re: Table as an argument

Post 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
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Table as an argument

Post 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()
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
jurses
Prole
Posts: 9
Joined: Sun May 03, 2015 10:23 am
Location: Canary Islands

Re: Table as an argument

Post 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.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Table as an argument

Post 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.
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
jurses
Prole
Posts: 9
Joined: Sun May 03, 2015 10:23 am
Location: Canary Islands

Re: Table as an argument

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 3 guests