Page 1 of 1

[SOLVED] Feeding parameters question

Posted: Tue Aug 17, 2021 2:55 am
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)...

Re: Feeding parameters question

Posted: Tue Aug 17, 2021 3:24 am
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)

Re: Feeding parameters question

Posted: Tue Aug 17, 2021 4:05 am
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 

Re: Feeding parameters question

Posted: Tue Aug 17, 2021 4:09 am
by alejandroalzate
oh its kinda EZ like love.window.setMode flags, and btw how i close the thread?

Re: Feeding parameters question

Posted: Tue Aug 17, 2021 5:11 am
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"

Re: Feeding parameters question

Posted: Tue Aug 17, 2021 7:41 am
by alejandroalzate
Oh, ok [SOLVED]

[SOLVED] Feeding parameters question

Posted: Tue Aug 17, 2021 8:26 am
by darkfrei
alejandroalzate wrote: Tue Aug 17, 2021 7:41 am Oh, ok [SOLVED]
In the Subject in topic start:

Re: Feeding parameters question

Posted: Tue Aug 17, 2021 11:40 am
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.

Re: [SOLVED] Feeding parameters question

Posted: Tue Aug 17, 2021 4:24 pm
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