[Help] attempt to perform arithmetic on a table value

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
Cebux
Prole
Posts: 2
Joined: Fri Apr 07, 2017 10:19 pm

[Help] attempt to perform arithmetic on a table value

Post 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
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post 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.
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.
Cebux
Prole
Posts: 2
Joined: Fri Apr 07, 2017 10:19 pm

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 7 guests