cartographer icon indicating copy to clipboard operation
cartographer copied to clipboard

Error when using tiles iterator

Open DominikStyp opened this issue 5 years ago • 2 comments
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'

DominikStyp avatar Oct 09 '20 14:10 DominikStyp

Can you provide an example project that demonstrates the issue?

tesselode avatar Oct 09 '20 18:10 tesselode

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.

tesselode avatar Oct 10 '20 01:10 tesselode