Search code examples
lualove2d

Lua Tilemap Specific Locations


My code:

for mapy=0, 10 do
  for mapx=0, 13 do
    if map[mapy][mapx] == 0 then
        love.graphics.setColor(255, 255, 255)
        love.graphics.rectangle("line", mapx * 8, mapy * 8, (mapx * 8) + 64, (mapy * 8) + 64)
    end
  end
end

I'm trying to test the specific row + column of map with mapy and mapx but it's only returning an error.

(The love.graphics is just setting the color to white, and drawing from mapx and mapy.)

I'm not even sure if this is the right way to do it, but I'd like to know what I'm doing wrong.

The map is 13x10. As for the error, it's at the if statement.

Attempt to index a nil value.


Solution

  • Arrays in Lua use index beginning from 1, not 0. If the map is 13x10, then it should be:

    for mapy = 1, 10 do
      for mapx = 1, 13 do