cartographer
cartographer copied to clipboard
Error when using tiles iterator
trafficstars
cartographer = require 'externalLibs.cartographer.cartographer' -- if it's in subfolders
-- load a map
local map = cartographer.load 'map.lua'
function love.load()
local layer = map:getLayer("layer1")
local allTiles = layer:getTiles()
for v in allTiles do
print(v)
end
end
function love.update(dt)
-- update animations
map:update(dt)
end
function love.draw()
-- draw the whole map
map:draw()
end
This code doesn't work, despite that map is drawn on the screen, following error occurs when I try to iterate over tiles:
Error
externalLibs/cartographer/cartographer.lua:649: attempt to perform arithmetic on local 'i' (a nil value)
Traceback
externalLibs/cartographer/cartographer.lua:649: in function '(for generator)'
main.lua:8: in function 'load'
[C]: in function 'xpcall'
[C]: in function 'xpcall'
Can you provide an example project that demonstrates the issue?
Actually never mind, I think I know what your issue is.
Try changing:
local allTiles = layer:getTiles()
for v in allTiles do
print(v)
end
To:
for v in layer:getTiles() do
print(v)
end
Similar to ipairs, getTiles returns 3 different values, all of which are needed for the loop to function properly. Since you were assigning getTiles() to only one intermediate variable, you're missing the other two variables.