love.graphics.getColor() returns 0

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Fabimawn
Prole
Posts: 23
Joined: Wed Jan 04, 2023 11:28 am
Location: Lua

love.graphics.getColor() returns 0

Post by Fabimawn »

When using love.graphics.getColor() to store the current color in a variable to reset it later in a function, it returns an error where it says that when using love.graphics.setColor I passed a nil value to the function. When looking into this problem with a debugging tool I saw that even if I stored the result of love.graphics.getColor() in a global variable in the main loop of my program and directly setting the color with that variable afterwards it still wouldn't work. For the record I've also set love.graphics.setColor before calling the get function, and it still doesn't work. Please see the attached issue for the problem.

When trying love.graphics.setColor(love.graphics.getColor()), the program actually doesn't crash, but when storing the get output in a variable it just doesn't want to bother.

What is going on???
Attachments
Problem.PNG
Problem.PNG (49.15 KiB) Viewed 9960 times
MrFariator
Party member
Posts: 559
Joined: Wed Oct 05, 2016 11:53 am

Re: love.graphics.getColor() returns 0

Post by MrFariator »

As per wiki, getColor returns r, g, b and alpha as separate values, as opposed to a table. You're effectively only storing the red channel's value. If you only provide the red channel's value to setColor, you'll get a crash, because love.graphics.setColor either expects a table, or that you provide each channel separately as a number value. If doing the latter, only the alpha channel may optionally be dropped, where it will default to 1.

Effectively, do the following:

Code: Select all

local curR, curG, curB, curA = love.graphics.getColor ( )
-- your code here
love.graphics.setColor ( curR, curG, curB, curA )
The reason love.graphics.setColor(love.graphics.getColor()) works is because lua is evaluating that as you passing each of the four channels. So, basically the same as the code snippet above, but without explicitly assigning intermediate local variables.
User avatar
Fabimawn
Prole
Posts: 23
Joined: Wed Jan 04, 2023 11:28 am
Location: Lua

Re: love.graphics.getColor() returns 0

Post by Fabimawn »

Ooh that slipped under my attention. Thank you very much!
I guess I'm working with these color tables so much that even after double checking the wiki I didn't figure it out.
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests