Page 1 of 1

[Help] attempt to perform arithmetic on a table value

Posted: Fri Apr 07, 2017 10:21 pm
by Cebux
It seems to error on line 22 in function draw.

Code: Select all

function love.load(arg)
  gr = love.graphics
  total = 0
  objects = {}
  amount = 0
end

function love.update(dt)
  total = total + dt
  for i = 1, #objects do
    v = objects[i]
    v.x = v.x + v.speed * dt
    v.h = math.max(5,v.h + math.sin(total))
    v.w = math.max(5,v.w + math.sin(total))
  end
end

function love.draw()
  for i = 1,#objects do
    v = objects[i]
    if #objects > 1 and i ~= #objects then
      print(objects[i]+1)
      b = objects[i]+1
      gr.line(v.x,v.y,b.x,b.y)
    end
    gr.rectangle("fill", v.x, v.y, v.w, v.h)
  end
end

function love.keypressed(key, scancode, isrepeat)
  if key == "space" then
    objects[#objects+1] = newRec()
  end
end

function newRec()
  object = {}
  object.x = gr.getWidth() - gr.getWidth()
  object.y = gr.getHeight() * 0.5
  object.w = 20
  object.h = 20
  object.speed = gr.getWidth() / 20
  return object
end

Re: [Help] attempt to perform arithmetic on a table value

Posted: Fri Apr 07, 2017 10:43 pm
by zorg
newRec creates and returns a table, so all values for all indices of objects is a table;
print(objects[ i ]+1) <-- is the line that errors
objects[ i ] is a table, and you can't add a number to a table, as the error says.

Re: [Help] attempt to perform arithmetic on a table value

Posted: Fri Apr 07, 2017 11:23 pm
by Cebux
zorg wrote: Fri Apr 07, 2017 10:43 pm newRec creates and returns a table, so all values for all indices of objects is a table;
print(objects[ i ]+1) <-- is the line that errors
objects[ i ] is a table, and you can't add a number to a table, as the error says.
ey thank you. I just noticed what I did wrong. I did it right in some places then of course I would do it wrong there.