jay
jay

Reputation: 25

map-functions.lua:60: attempt to index a nil value (love2d, physics)

I know that the error is caused by the index not existing, but i dont know why it is not existing. I am trying to make a program implemented in the mapDraw method which adds to every wall tile(#) a physics object:

function drawMap()
  objects = {}
  for x,column in ipairs(TileTable) do
    for y,char in ipairs(column) do
      love.graphics.draw(Tileset, Quads[ char ] , (x-1)*TileW, (y-1)*TileH)
      if char == '#' then --addding the physics for collision(walls)--
        objects[objectIndex] = {
          body = love.physics.newBody(world, (x-1/2) * TileW, (x-1/2) * TileH),
          shape = love.physics.newRectangleShape(32, 32),
          fixture = love.physics.newFixture(objects[objectIndex].body, objects[objectIndex].shape, 1)
        }
      end
    end
  end
end

I am only starting out with love2d and game making and would appriciate help, thank you.

Upvotes: 1

Views: 137

Answers (1)

hjpotter92
hjpotter92

Reputation: 80639

In the following snippet:

objects[objectIndex] = {
  body = love.physics.newBody(world, (x-1/2) * TileW, (x-1/2) * TileH),
  shape = love.physics.newRectangleShape(32, 32),
  fixture = love.physics.newFixture(objects[objectIndex].body, objects[objectIndex].shape, 1)
}

you are self referencing the table key, while it is being assigned. This is an invalid step in lua. Assign the fixture key a value later:

objects[objectIndex] = {
  body = love.physics.newBody(world, (x-1/2) * TileW, (x-1/2) * TileH),
  shape = love.physics.newRectangleShape(32, 32)
}
objects[objectIndex].fixture = love.physics.newFixture(objects[objectIndex].body, objects[objectIndex].shape, 1)

Upvotes: 1

Related Questions