[Solved] (typo) How can I save a list of args in a variable, and call a function with it?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
cloudfrog
Prole
Posts: 8
Joined: Mon Feb 21, 2022 8:31 am

[Solved] (typo) How can I save a list of args in a variable, and call a function with it?

Post by cloudfrog »

A sample is worth a thousand words. Take the following line:

Code: Select all

love.graphics.setColor( 228/255, 6/255, 38/255, 1)
I want to define my color value elsewhere, and then set it when needed. I don't want to have to store r,g,b,a separately though. Below is what I would like to do, just not sure exactly how.

Code: Select all

-- in love.load() for example
Settings.myColor = { 228/255, 6/255, 38/255, 1 }


-- Later in my draw()
love.graphics.setColor(Settings.myColor)
Last edited by cloudfrog on Mon Feb 21, 2022 8:26 pm, edited 1 time in total.
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: How can I save a list of args in a variable, and call a function with it?

Post by veethree »

setColor can take a table as an argument.

Just do

Code: Select all

love.graphics.setColor(Settings.myColor)
MrFariator
Party member
Posts: 548
Joined: Wed Oct 05, 2016 11:53 am

Re: How can I save a list of args in a variable, and call a function with it?

Post by MrFariator »

As veethree pointed out, you can just use a table. However, for some functions that do not allow a table to be passed, you can still wrap the arguments in a table, and use unpack().

Code: Select all

local arguments = { 4, 2, 1 }
local myFunction = function ( arg1, arg2, arg3 )
  print(arg1, arg2, arg3) -- prints: 4, 2, 1
end
myFunction ( unpack(arguments) )
Of course, I wouldn't recommend you to rely on this too much, or in time critical code, because unpack() can be fairly slow. In a case like this, where the number of arguments is known, it'd be better to do just:

Code: Select all

local arguments = { 4, 2, 1 }
local myFunction = function ( arg1, arg2, arg3 )
  print(arg1, arg2, arg3) -- prints: 4, 2, 1
end
myFunction ( arguments[1], arguments[2], arguments[3] )
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: How can I save a list of args in a variable, and call a function with it?

Post by pgimeno »

Beware of nil values in the arguments, though, as those will cause unpack() to stop returning arguments.
User avatar
dusoft
Party member
Posts: 635
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: How can I save a list of args in a variable, and call a function with it?

Post by dusoft »

cloudfrog wrote: Mon Feb 21, 2022 8:52 am A sample is worth a thousand words. Take the following line:

Code: Select all

love.graphics.setColor( 228/255, 6/255, 38/255, 1)
I want to define my color value elsewhere, and then set it when needed. I don't want to have to store r,g,b,a separately though. Below is what I would like to do, just not sure exactly how.

Code: Select all

-- in love.load() for example
Settings.myColor = { 228/255, 6/255, 38/255, 1 }


-- Later in my draw()
love.graphics.setColor(Settings.myColor)
Also, please use Support forum for this kind of questions.
cloudfrog
Prole
Posts: 8
Joined: Mon Feb 21, 2022 8:31 am

[Solved] (typo) How can I save a list of args in a variable, and call a function with it?

Post by cloudfrog »

Ok... I'm sorry this was lack of coffee on my part. My first thought was that I already tried exactly that (pass the table as the args). and i did!

However in the code where i populate the table I actually had:

Code: Select all

Settings.debugCollisionColor = { 228/255,  6,255,  38/255 , 1}
Rather then an error, i was just getting the wrong color. I took a screenshot just now to show it. But when also setting up a test using named args s.r, s.g etc I was able to see the mistake.

Thank you all for the input.


Note taken on using the support forum.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests