Problem with using calling values in a table

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
User avatar
Dopplespots
Prole
Posts: 4
Joined: Wed Nov 25, 2015 12:35 am

Problem with using calling values in a table

Post by Dopplespots »

I'm trying to use three different tables to sync up specific RBG values when setting the background color of my game.
However, when I try to use a variable to call the values, it doesn't work! What am I doing wrong?

Code: Select all

function love.load()
 
rainbowChangeRed = {237, 250, 247, 93, 81, 131, 241}
rainbowChangeGreen = {27, 162, 236, 187, 166, 118, 118}
rainbowChangeBlue = {81, 27, 47, 77, 220, 156, 166}
changeTimer = 1

end

function love.update(dt)

changeTimer = changeTimer + dt*1440

end

function love.draw()

love.graphics.setBackgroundColor(rainbowChangeRed[changeTimer], rainbowChangeGreen[changeTimer], rainbowChangeBlue[changeTimer])

end
I don't understand why I am unable to use changeTimer to call rainbowChangeRed/Green/Blue values. Please help me :nyu:
(Also I have no idea what I'm doing and if there's an easier/better way to do this, please tell me. Thanks again!)
Well okay then.
MrFariator
Party member
Posts: 510
Joined: Wed Oct 05, 2016 11:53 am

Re: Problem with using calling values in a table

Post by MrFariator »

Code: Select all

changeTimer + dt*1440
Try printing the values that produces. If you're using the default love.run (ie. you haven't defined it yourself), I imagine you're getting non-integer values because of the delta time. This then gives you table indexes that don't exist, thus giving you errors when you end up passing nil values to love.graphics.setBackgroundColor.

At the very least you probably need to use math.floor on the calculated value, though I figure you want a more fool proof solution.
For example, this a very simple naive approach where instead of using changeTimer you add another variable to track the table index:

Code: Select all

function love.load()
 
  rainbowChangeRed = {237, 250, 247, 93, 81, 131, 241}
  rainbowChangeGreen = {27, 162, 236, 187, 166, 118, 118}
  rainbowChangeBlue = {81, 27, 47, 77, 220, 156, 166}
  changeTimer = 0
  -- tracker variable for the table index
  changeIndex = 1
end

function love.update(dt)

  changeTimer = changeTimer + dt

  -- If accumulated deltatime passes a certain threshold update the changeIndex
  -- Here it's 0.5 as an example
  if changeTimer >= 0.5 then
    changeTimer = 0
    changeIndex = changeIndex + 1
    -- Wrap around when changeIndex >= 8
    if changeIndex > 7 then
      changeIndex = 1
    end
  end

end

function love.draw()
  -- Change background color based on changeIndex
  love.graphics.setBackgroundColor(rainbowChangeRed[changeIndex], rainbowChangeGreen[changeIndex], rainbowChangeBlue[changeIndex])
end
As an aside, I recommend against using global variables, and instead try to use local ones instead.
User avatar
Dopplespots
Prole
Posts: 4
Joined: Wed Nov 25, 2015 12:35 am

Re: Problem with using calling values in a table

Post by Dopplespots »

MrFariator wrote: Sat Aug 12, 2017 10:12 pm Try printing the values that produces. If you're using the default love.run (ie. you haven't defined it yourself), I imagine you're getting non-integer values because of the delta time. This then gives you table indexes that don't exist, thus giving you errors when you end up passing nil values to love.graphics.setBackgroundColor.

At the very least you probably need to use math.floor on the calculated value, though I figure you want a more fool proof solution.
For example, this a very simple naive approach where instead of using changeTimer you add another variable to track the table index:

As an aside, I recommend against using global variables, and instead try to use local ones instead.
Thank you for your help!
Well okay then.
Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests