[SOLVED] Feeding parameters question

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
alejandroalzate
Prole
Posts: 37
Joined: Sat May 08, 2021 9:45 pm

[SOLVED] Feeding parameters question

Post by alejandroalzate »

so i have a real quick one
im feeding a draw function by a another function that returns x & y as a string

Code: Select all

return ((camara.x + camara.secondaryx + camara.secondaryx * dt) * width / 100) .. ", " .. ((camara.y + camara.secondaryy + camara.secondary * dt) * height / 100)
outputs like 123.48654, 123.5456
but my question is if i can feed the draw function with an array of some sort
like:
...rectangle("fill", (x, y = calcpos(myinputx, myinputy)), 20, 20)...
Last edited by alejandroalzate on Tue Aug 17, 2021 4:26 pm, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3282
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Feeding parameters question

Post by zorg »

do not make it a string, that's slow as hell and you don't want that anyway; lua can work with multiple return values, but you can't just insert those in a parameter list if there are others after those, since anything after the first will be truncated:

Code: Select all

-- return two numbers instead
return ((camera.x + camera.secondaryx + camera.secondaryx * dt) * width / 100), ((camera.y + camera.secondaryy + camera.secondary * dt) * height / 100)

local x,y = calcpos(myinputx, myinputy) -- this is necessary
love.graphics.rectangle("fill", x, y, 20, 20)
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
darkfrei
Party member
Posts: 815
Joined: Sat Feb 08, 2020 11:09 pm

Re: Feeding parameters question

Post by darkfrei »

alejandroalzate wrote: Tue Aug 17, 2021 2:55 am my question is if i can feed the draw function with an array of some sort
like:
...rectangle("fill", (x, y = calcpos(myinputx, myinputy)), 20, 20)
Note the { }

Code: Select all

draw_rectangle("fill", {x,y= calcpos(myinputx, myinputy)}, 20, 20)
But it's a custom function:

Code: Select all

function draw_rectangle (typ, pos, w, h)
    love.graphics.rectangle(typ, pos.x, pos.y, w, h)
end 
alejandroalzate
Prole
Posts: 37
Joined: Sat May 08, 2021 9:45 pm

Re: Feeding parameters question

Post by alejandroalzate »

oh its kinda EZ like love.window.setMode flags, and btw how i close the thread?
User avatar
zorg
Party member
Posts: 3282
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Feeding parameters question

Post by zorg »

alejandroalzate wrote: Tue Aug 17, 2021 4:09 am oh its kinda EZ like love.window.setMode flags, and btw how i close the thread?
You don't; just don't reply to it... you can also edit your opening post's subject to say" [SOLVED] Feeding parameters question"
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.
alejandroalzate
Prole
Posts: 37
Joined: Sat May 08, 2021 9:45 pm

Re: Feeding parameters question

Post by alejandroalzate »

Oh, ok [SOLVED]
User avatar
darkfrei
Party member
Posts: 815
Joined: Sat Feb 08, 2020 11:09 pm

[SOLVED] Feeding parameters question

Post by darkfrei »

alejandroalzate wrote: Tue Aug 17, 2021 7:41 am Oh, ok [SOLVED]
In the Subject in topic start:
Attachments
2021-08-17T10_24_27.png
2021-08-17T10_24_27.png (13.07 KiB) Viewed 7447 times
User avatar
pgimeno
Party member
Posts: 3288
Joined: Sun Oct 18, 2015 2:58 pm

Re: Feeding parameters question

Post by pgimeno »

darkfrei wrote: Tue Aug 17, 2021 4:05 am

Code: Select all

draw_rectangle("fill", {x,y= calcpos(myinputx, myinputy)}, 20, 20)
That doesn't work the way you expect.

What you've written is equivalent to this:

Code: Select all

do
  local t = {}
  t[1] = x
  t.y = calcpos(myinputx, myinputy)
  draw_rectangle("fill", t, 20, 20)
end
If you want to assign multiple return values to table members at once, the only option is to make an array:

Code: Select all

draw_rectangle("fill", {calcpos(myinputx, myinputy)}, 20, 20)
which needs to be used by numeric indices:

Code: Select all

function draw_rectangle (typ, pos, w, h)
    love.graphics.rectangle(typ, pos[1], pos[2], w, h)
end 
I don't recommend this approach anyway because it allocates a table that needs to be garbage-collected.
alejandroalzate
Prole
Posts: 37
Joined: Sat May 08, 2021 9:45 pm

Re: [SOLVED] Feeding parameters question

Post by alejandroalzate »

darkfrei wrote: Tue Aug 17, 2021 8:26 am
alejandroalzate wrote: Tue Aug 17, 2021 7:41 am Oh, ok [SOLVED]
In the Subject in topic start:
BRUUUUUHHHH my fault let me close this
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 15 guests