Search code examples
if-statementluaprogram-entry-pointlove2d

Love2D If # of i,v in table


So I want to make a button system in Love2D and I have the code all written out. The problem is it won't let me test for the number of i,vs in my table. My code is this:

function love.load()
 colorA={"blue", "yellow"}
 colorB={"blue"}
 colorC={"blue"}
end
function love.update(dt)
 --The update stuff that I wrote
end
function love.draw()
 for i,v in pairs(colorA) do
  if i,v == 2 then
   love.graphics.setColor(255, 255, 0)
  else
   love.graphics.setColor(0, 0, 255)
  end
 end
 love.graphics.rectangle(My button data)
 --The same thing for other 2 buttons
end

And it reads

"Syntax error: main.lua:57: 'then' expected near ','".

(I have more code, so don't think it's something else because of "main.lua:57", that is where the line is on my actual code.) Help!


Solution

  • it won't let me test for the number of i,vs in my table if i,v == 2 then

    You can't do two comparisons at once. Each comparison is a separate expression, which you can combine with boolean operators. For instance:

    if i == 2 and v == 2 then
    

    However, look at the rest of your code it looks like you were trying to compare just i or just v, so you should do that.


    Referring to color by it's hardcoded position in an array is a pretty bad approach. You're also cycling through a list of colors and calling setColor, but only the last call is actually going to do anything, so that really doesn't make sense.