Page 1 of 1
[Solved] (typo) How can I save a list of args in a variable, and call a function with it?
Posted: Mon Feb 21, 2022 8:52 am
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)
Re: How can I save a list of args in a variable, and call a function with it?
Posted: Mon Feb 21, 2022 9:56 am
by veethree
setColor can take a table as an argument.
Just do
Code: Select all
love.graphics.setColor(Settings.myColor)
Re: How can I save a list of args in a variable, and call a function with it?
Posted: Mon Feb 21, 2022 1:39 pm
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] )
Re: How can I save a list of args in a variable, and call a function with it?
Posted: Mon Feb 21, 2022 1:43 pm
by pgimeno
Beware of nil values in the arguments, though, as those will cause
unpack() to stop returning arguments.
Re: How can I save a list of args in a variable, and call a function with it?
Posted: Mon Feb 21, 2022 3:14 pm
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.
[Solved] (typo) How can I save a list of args in a variable, and call a function with it?
Posted: Mon Feb 21, 2022 8:24 pm
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.