Page 1 of 1

I'm using Tiled and I'm trying to access a table. Help?

Posted: Mon Mar 23, 2015 5:36 am
by lolsasory
So this is my map.lua

Code: Select all

return {
  version = "1.1",
  luaversion = "5.1",
  tiledversion = "0.11.0",
  orientation = "orthogonal",
  width = 45,
  height = 45,
  tilewidth = 16,
  tileheight = 16,
  nextobjectid = 1,
  properties = {},
  tilesets = {
    {
      name = "Default",
      firstgid = 1,
      tilewidth = 16,
      tileheight = 16,
      spacing = 0,
      margin = 0,
      image = "../images/tileset.png",
      imagewidth = 160,
      imageheight = 160,
      tileoffset = {
        x = 0,
        y = 0
      },
      properties = {},
      terrains = {},
      tiles = {
        {
          id = 0,
          properties = {
            ["Solid"] = "Solid"
          }
        }
      }
    }
  },
  layers = {
    {
      type = "tilelayer",
      name = "Background",
      x = 0,
      y = 0,
      width = 45,
      height = 45,
      visible = true,
      opacity = 1,
      properties = {},
      encoding = "lua",
      data = {
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1,
        1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
      }
    }
  }
}
My actual data table (at the end of the code) is much larger I just shortened it for this post. I'm trying to access the first number with

Code: Select all

map.layers[1].data[1]
So "map" gets into the file, ".layers" gets me into the layers table, the [1] gets me into the unnamed sub-table, ".data" gets me into the data table, and the last [1] should get me that first digit. But it says "string expected, got table". It says the same thing if I make it

Code: Select all

map.layers[1].data[1][1]
and if I make it

Code: Select all

map.layers[1].data[1][1][1]
it returns nil. What am I doing wrong?

Re: I'm using Tiled and I'm trying to access a table. Help?

Posted: Mon Mar 23, 2015 11:02 am
by Robin
Please upload your .love. I think the actual problem might be somewhere else.

Re: I'm using Tiled and I'm trying to access a table. Help?

Posted: Mon Mar 23, 2015 1:51 pm
by lolsasory
I dont actually have a .love file, I am just running some test code out of notepad++

but here is my main, and the two files involved


I can change the map01.lua file, I named that table in layers and that worked. But then it doesnt work with STI

Re: I'm using Tiled and I'm trying to access a table. Help?

Posted: Tue Mar 24, 2015 7:04 pm
by Jasoco
We can't recreate it properly with just those files. We need a whole project with all the included libraries.

Re: I'm using Tiled and I'm trying to access a table. Help?

Posted: Mon Mar 30, 2015 2:02 pm
by Skeiks
lolsasory wrote:I dont actually have a .love file, I am just running some test code out of notepad++

but here is my main, and the two files involved


I can change the map01.lua file, I named that table in layers and that worked. But then it doesnt work with STI

Making a love file is simple. Just zip everything up and rename it to .love. However I think your problem is that you're not accessing the right variable. In STI all tiles are their own tables with references to a host of different things to help you out. What you want to do, to find the ID of the tile, is probably this:

Code: Select all

map.layers[1].data[1][1].gid
I would recommend you try using ZeroBrane studio, it's got a debugger that can help you out with issues like this. Let me know if that helps.

Also you might want to reference your layers using their names, not numbers:

Code: Select all

map.layers.Background.data[1][1].gid