Page 1 of 1
Issues with .setColor
Posted: Fri Apr 22, 2022 12:29 pm
by Dranzule
Hello. I'm trying to make a simple application which, when run, will render a cube as well as a simple text on the top right of the screen. That cube is supposed to have a 1/5 chance of being red(instead of orange). I'm somewhat of a beginner, so sorry if this is kinda obvious.
Here's the piece of code:
Code: Select all
function love.load()
isHungry()
gameFont = love.graphics.newFont(25)
end
function isHungry()
number = love.math.random(5, 5)
if number == 5 then
love.graphics.setColor(1, 0, 0)
else
love.graphics.setColor(1, 0.7, 0)
end
end
-- "rectangle" will never be red related to love.draw() running once per frame?
function love.draw()
love.graphics.rectangle("fill", 325, 300, 150, 150)
love.graphics.setColor(1, 0.7, 0)
love.graphics.setFont(gameFont)
love.graphics.print("Level: 25", 650, 35)
end
However, as you can see by comment, "rectangle" will never be red if it's followed by love.graphics.setColor. I've come to understand this happens due to isHungry() only running once in love.load(), while love.draw() runs at 60 FPS(ideally), making isHungry() kinda useless. I want to print text with a separate color(orange), but I don't know how to do so without making the cube change it's color. Help?
Re: Issues with .setColor
Posted: Fri Apr 22, 2022 2:52 pm
by Dranzule
Found the solution to the problem. Don't think it's the most optimized, but it will work with no issues.
Code: Select all
function love.load()
isHungry = determineHunger()
gameFont = love.graphics.newFont(25)
end
function determineHunger()
number = love.math.random(0, 5)
if number == 5 then
return true
else
return false
end
end
-- "rectangle" will never be red related to love.draw() running once per frame?
function love.draw()
if isHungry == true then
love.graphics.setColor(1, 0, 0)
else
love.graphics.setColor(1, 0.7, 0)
end
love.graphics.rectangle("fill", 325, 300, 150, 150)
love.graphics.setColor(1, 0.7, 0)
love.graphics.setFont(gameFont)
love.graphics.print("Level: 25", 650, 35)
end
By using a if statement, I can check into the love.draw function what value determineHunger() has returned.
Re: Issues with .setColor
Posted: Fri Apr 22, 2022 5:30 pm
by BrotSagtMist
Your solution puts an if case for a neverchanging value inside a frame.
Thats 60 times per seconds requesting if true stays true, its a waste of cpu.
You should instead define a colour in the load process.:
isHungry = {1,0,0}
love.graphics.setColor(ishungry)
Although on luajit the impact is minimal, other languages will bite you for that.
Oh an same for setfont btw. Its the only font you use so it makes little sense to set it every frame.
Re: Issues with .setColor
Posted: Sat Apr 23, 2022 2:52 am
by ReFreezed
Dranzule wrote: ↑Fri Apr 22, 2022 2:52 pm
Code: Select all
number = love.math.random(0, 5)
if number == 5 then
Note that both arguments for love.math.random() are inclusive, which means it returns one of 012345 here, i.e. the condition has a 1/6 chance of succeeding - not 1/5.
(And don't worry about optimization at this point - that's a more advanced topic.
)
Re: Issues with .setColor
Posted: Sat Apr 23, 2022 6:43 am
by zorg
The initial issue, according to the first code snippet posted, was that you were using `number = love.math.random(5, 5)` which gives you a random number between 5 and... 5
guess how many whole numbers are in that range.
Re: Issues with .setColor
Posted: Sat Apr 23, 2022 2:04 pm
by Dranzule
zorg wrote: ↑Sat Apr 23, 2022 6:43 am
The initial issue, according to the first code snippet posted, was that you were using `number = love.math.random(5, 5)` which gives you a random number between 5 and... 5
guess how many whole numbers are in that range.
Yeah sorry, I had changed that for testing and had not realized.
Re: Issues with .setColor
Posted: Sat Apr 23, 2022 6:05 pm
by Dranzule
BrotSagtMist wrote: ↑Fri Apr 22, 2022 5:30 pm
Your solution puts an if case for a neverchanging value inside a frame.
Thats 60 times per seconds requesting if true stays true, its a waste of cpu.
You should instead define a colour in the load process.:
isHungry = {1,0,0}
love.graphics.setColor(ishungry)
Although on luajit the impact is minimal, other languages will bite you for that.
Oh an same for setfont btw. Its the only font you use so it makes little sense to set it every frame.
isHungry is only meant to be triggered in a 20% chance though. Defining it in the load process also leads to the setColor() responsible for setting the text to orange colour to set the rectangle as orange colour. I've messed with the code and ended up shuffling it a bit, moving away unneeded stuff from love.draw().
Code: Select all
function determineHunger()
number = love.math.random(1, 5)
if number == 5 then
return true
end
end
function love.load()
isHungry = determineHunger()
if isHungry == true then
hungerColorCode = {1, 0, 0}
else
hungerColorCode = {1, 0.7, 0}
end
gameFont = love.graphics.newFont(25)
love.graphics.setFont(gameFont)
end
function love.draw()
love.graphics.setColor(hungerColorCode)
love.graphics.rectangle("fill", 325, 300, 150, 150)
love.graphics.setColor(1, 0.7, 0)
love.graphics.print("Level: 25", 350, 35)
end