Issues with .setColor

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Dranzule
Prole
Posts: 4
Joined: Fri Apr 22, 2022 1:01 am

Issues with .setColor

Post 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?
Dranzule
Prole
Posts: 4
Joined: Fri Apr 22, 2022 1:01 am

Re: Issues with .setColor

Post 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.
User avatar
BrotSagtMist
Party member
Posts: 655
Joined: Fri Aug 06, 2021 10:30 pm

Re: Issues with .setColor

Post 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.
obey
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Issues with .setColor

Post 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. ^^ )
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Issues with .setColor

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Dranzule
Prole
Posts: 4
Joined: Fri Apr 22, 2022 1:01 am

Re: Issues with .setColor

Post 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.
Dranzule
Prole
Posts: 4
Joined: Fri Apr 22, 2022 1:01 am

Re: Issues with .setColor

Post 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


Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests