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

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
lolsasory
Prole
Posts: 21
Joined: Mon Nov 19, 2012 5:27 am

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

Post 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?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post by Robin »

Please upload your .love. I think the actual problem might be somewhere else.
Help us help you: attach a .love.
lolsasory
Prole
Posts: 21
Joined: Mon Nov 19, 2012 5:27 am

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

Post 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
Attachments
map01.lua
the tables
(7.31 KiB) Downloaded 80 times
loadmap.lua
the print command
(116 Bytes) Downloaded 77 times
main.lua
(535 Bytes) Downloaded 86 times
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

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

Post by Jasoco »

We can't recreate it properly with just those files. We need a whole project with all the included libraries.
User avatar
Skeiks
Citizen
Posts: 51
Joined: Wed Jan 28, 2015 1:51 pm

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

Post 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
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 100 guests