Why can't I store this value in variable, yet I can use it normally?

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
tommcjeff
Prole
Posts: 3
Joined: Sat May 01, 2021 1:12 pm

Why can't I store this value in variable, yet I can use it normally?

Post by tommcjeff »

So I'm trying to make a simple scrolling platformer, but when I'm making it scroll, I came across this line:

Code: Select all

love.graphics.polygon("fill", objects.ground.body:getWorldPoints(objects.ground.shape:getPoints()))
I tried to replace it with this:

Code: Select all

local allpoints = objects.ground.body:getWorldPoints(objects.ground.shape:getPoints())
love.graphics.polygon("fill", allpoints)
and it errored with:
Error

main.lua:46: Number of vertex components must be a multiple of two

Would anyone know why this happens? I'm new to love2d so I'm not sure why this is happening.
User avatar
BrotSagtMist
Party member
Posts: 604
Joined: Fri Aug 06, 2021 10:30 pm

Re: Why can't I store this value in variable, yet I can use it normally?

Post by BrotSagtMist »

This function returns multiple number values, Assignment with = only work for one single value.
You save the x value of the first point and discard the rest, obviously this cannot get drawn.
The workaround is to turn all returns into a table:
local allpoints = {objects.ground.body:getWorldPoints(objects.ground.shape:getPoints())}
obey
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: Why can't I store this value in variable, yet I can use it normally?

Post by MrFariator »

You can assign multiple return values from a function with the assignment operator, you just need to introduce additional variables.

Code: Select all

local p1x, p1y, p2x, p2y, p3x, p3y = objects.ground.body:getWorldPoints(objects.ground.shape:getPoints())
love.graphics.polygon("fill", p1x, p1y, p2x, p2y, p3x, p3y)
Of course, this works mostly if you already know ahead of time how many points your polygon will have, because as far as I know, love.graphics.polygon will fail if any of the passed variables is nil.

Wrapping the points into a table like BrotSagtMist pointed out works, too. However, this is not advisable because you'll be creating a new table every time such code is run (every frame when drawing), which is rather needlessly costly. As such, for any arbitrarily sized polygons, you're better off using the first line of code in your opening post.
User avatar
zorg
Party member
Posts: 3435
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Why can't I store this value in variable, yet I can use it normally?

Post by zorg »

also, love.graphics.polygon can accept variadic number of arguments (i think, look at the wiki to make sure) after the first one, so that's also the reason why the first code block works, since all passed arguments will be used.
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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 16 guests