lualove2dtiled

tiledmap.lua:33: bad argument #2 to 'draw' (Quad expected, got nil)


Using love2d framework and Tiled to make a visual map

I keep pulling a missing value error

"Quad expected, nil recieved", i think it's "local tid = layers.data[index]" but i have no idea what's wrong or how to even begin fixing the problem

I was following these instructions Tiled Love2D Tutorial

main.lua

require "tiledmap"

function love.load()
    _G.map = loadTiledMap('chessboard')
end

function love.draw()
    _G.map:draw()
end

tiledmap.lua

    function loadTiledMap(path)
    local map = require(path)
    map.quads = {}
    
    local greenfeed = map.tilesets[1]
    map.greenfeed = greenfeed
    map.image = love.graphics.newImage(greenfeed.image)
    for y = 0, (greenfeed.imageheight / greenfeed.tileheight) - 1 do
        for x = 0, (greenfeed.imagewidth / greenfeed.tilewidth) - 1 do
            local quad = love.graphics.newQuad(
                x * greenfeed.tilewidth,
                y * greenfeed.tileheight,
                greenfeed.tilewidth,
                greenfeed.tileheight,
                greenfeed.imagewidth,
                greenfeed.imageheight
            )
            table.insert(map.quads, quad)
            print(map.quads)
        end
    end

    function map:draw()
        for i, layers in ipairs(self.layers) do
            for y = 0, layers.height - 1 do
                for x = 0, layers.width - 1 do
                    local index = (x + y * layers.width) + 1
                    local tid = layers.data[index]
                    if tid ~= 0 then
                        local quad = self.quads[tid]
                        local xx = x * self.greenfeed.tilewidth
                        local yy = y * self.greenfeed.tileheight
                        love.graphics.draw(
                            self.image,
                            quad,
                            xx,
                            yy
                        )
                    end
                end
            end
        end
    end
    return map
end

chessboard.lua

    return {
  version = "1.5",
  luaversion = "5.4",
  tiledversion = "1.7.2",
  orientation = "orthogonal",
  renderorder = "left-up",
  width = 25,
  height = 25,
  tilewidth = 35,
  tileheight = 35,
  nextlayerid = 2,
  nextobjectid = 1,
  properties = {},
  tilesets = {
    {
      name = "greenfeed",
      firstgid = 3,
      tilewidth = 35,
      tileheight = 35,
      spacing = 0,
      margin = 0,
      columns = 2,
      image = "ggreen.png",
      imagewidth = 70,
      imageheight = 35,
      objectalignment = "unspecified",
      tileoffset = {
        x = 0,
        y = 0
      },
      grid = {
        orientation = "orthogonal",
        width = 35,
        height = 35
      },
      properties = {},
      wangsets = {},
      tilecount = 2,
      tiles = {}
    }
  },
  layers = {
    {
      type = "tilelayer",
      x = 0,
      y = 0,
      width = 25,
      height = 25,
      id = 1,
      name = "Tile Layer 1",
      visible = true,
      opacity = 1,
      offsetx = 0,
      offsety = 0,
      parallaxx = 1,
      parallaxy = 1,
      properties = {},
      encoding = "lua",
      data = {
        3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3,
        4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
        3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3,
        4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
        3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3,
        4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
        3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3,
        4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
        3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3,
        4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
        3, 4, 3, 4, 3, 4, 3, 4, 3, 4,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 4, 3, 4,
        3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3
      }
    }
  }
}

Solution

  • The tiles in the layers data array are GIDs (global Ids). You need to convert them into tileset-local ids first. Every tileset has a firstgid field, which defines the first GID this tileset contains, in your case 3. Therefore, you have to subtract 3 for each data (and add 1 since indices start at 1).