Odd behaviour when drawing tiles (SOLVED)

Hello,

when I was implementing a zooming function to our game yesterday I noticed a pretty strange behaviour of my tile drawing function. Every other time I call it, it somehow magically reverses the draw order. Any idea on what might be causing this? I am posting condensed bits of code below, all the tiles are generated by the same factory and (I guess) in the same order.

function render_tiles(self)

go.delete_all(self.rendered_tiles)
for k,v in pairs(self.rendered_tiles) do self.rendered_tiles[k]=nil end

for h=1, self.tilemap_height, 1 do
for w=1, self.tilemap_width, 1 do
local tile_pos = vmath.vector3(w,h,0)

  	local tile_offset = vmath.vector3()
  	tile_offset.x = tonumber(self.tilemap()["tilesets"][tileset]["tileoffset"]["x"])
  	tile_offset.y = tonumber(self.tilemap()["tilesets"][tileset]["tileoffset"]["y"])
  	
  	local pixel_pos = coords_to_pixel(self, tile_pos, tile_offset)
  	
  	local tile = factory.create("#tileFactory", vmath.vector3(pixel_pos.x, pixel_pos.y, 0), nil, {}, self.scale_level)
  	table.insert(self.rendered_tiles, tile)
  end

end
end

Here is an example of the odd behaviour

Thanks!

local tile = factory.create("#tileFactory", vmath.vector3(pixel_pos.x, pixel_pos.y, 0), nil, {}, self.scale_level)

Instead of setting the z value to 0, set to to a value which you want to determine the guaranteed z order depth. The value can be extremely granular (0.00001, 0,00002…)between your z render range.

3 Likes

Thanks, worked straight away.
I tried this before, but the tiles didn’t render at all because I was using integers.

The default render range is -1 to 1. You can increase this in your custom copy of the render script, and then have a broad whole number range for z depth if you wish.

2 Likes