Search code examples
luadatatableslove2dtiled

Getting data from a table


Using Tiled I generated a Lua file which contains a table. So I figured that I'd write a for loop which cycles through the table gets the tile id and checks if collision is true and add collision if it was. But, I've been unable to get the tile id's or check they're properties. But it returned a error saying that I tried to index nil value tileData.

Here is the Map file

return {
    version = "1.1",
    luaversion = "5.1",
    -- more misc. data
    tilesets = {
      {
        name = "Tileset1",
            firstgid = 1,
            tilewidth = 16,
            tileheight = 16,
            tiles = {
              {
                id = 0,
                properties = {
                    ["Collision"] = false
                }
              },
            }
        }
    layers = {
      {
        type = "tilelayer",
        name = "Tile Layer 1"
        data = {
            -- array of tile id's
        }
      }
    }
  } 

And here is the for loop I wrote to cycle through the table

require("Protyping")
local map = love.filesystem.load("Protyping.lua")()
local tileset1 = map.tilesets
local tileData = tileset1.tiles
local colision_layer = map.layers[1].data

for y=1,16 do 
    for x=1,16 do
        if tileData[colision_layer[x*y]].properties["Colision"] == true then
            world:add("collider "..x*y,x*map.tilewidth, y*tileheight,tilewidth,tileheight)
        end
    end
end

Solution

  • lhf's answer (map.tilesets[1] instead of map.tilesets) fixes the error you were getting, but there are at least two other things you'll need to fix for your code to work.

    The first is consistent spelling: you have a Collision property in your map data and a Colision check in your code.

    The second thing you'll need to fix is the way that the individual tiles are being referenced. Tiled's layer data is made of 2-dimensional tile data laid out in a 1-dimensional array from left-to-right, starting at the top, so the index numbers look like this:

    tiled indexes

    You would think you could just do x * y to get the index, but if you look closely, you'll see that this doesn't work. Instead, you have to do x + (y - 1) * width.

    Or if you use zero-based x and y, it looks like this:

    tiled indexes 0-based

    Personally, I prefer 0-based x and y (but as I get more comfortable with Lua, that may change, as Lua has 1-based arrays). If you do go with 0-based x and y, then the formula is x + 1 + y * width.

    I happen to have just written a tutorial this morning that goes over the Tiled format and has some helper functions that do exactly this (using the 0-based formula). You may find it helpful: https://github.com/prust/sti-pg-example.

    The tutorial uses Simple Tiled Implementation, which is a very nice library for working with Tiled lua files. Since you're trying to do collision, I should mention that STI has a plugins for both the bump collision library and the box2d (physics) collision library.