Lua 2D array error

So, I'm pretty new to Lua, and in other languages ​​I managed to create a 2D array of variables and just index by array to create a tiled map. Whenever I try to do this in lua, I get an error (in particular, an error indicating that I am indexing the nil value). How can i fix this?

CODE

function love.load()
love.graphics.setColor(255,255,0)
tile = love.graphics.newImage("lightGrass.png")
map = { {1,1,0,0,0,0,0,0,0,0},
        {0,1,0,0,0,0,0,0,0,0},
        {0,1,0,0,0,0,0,0,0,0},
        {1,1,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0}
    }
end

function love.draw()
    for i = 0, 10 do
        for j = 0, 10 do
            newPos = map[i][j]
            if newPos == 0 then -- this is where the error is!!!!!!!!!!!!!!!
                love.graphics.draw(tile,j * 32, i * 32)
            end
        end
    end

end

function love.update(dt)

end
+4
source share
1 answer

Arrays in Lua start at 1, not at 0. So your loops forshould start at 1.

+4
source

All Articles