Confused on how to access the data field of STI tiled file

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
_Joply
Prole
Posts: 3
Joined: Mon Sep 18, 2023 8:28 am

Confused on how to access the data field of STI tiled file

Post by _Joply »

I am about to implement some pathfinding in a game I am working on using jumper, and when I looked inside the .lua files generated by tiled I saw that they have an array that seems convertable or usable for this. When i try accessing a layers data field I was expecting this array to be returned but instead I get a table that contains a table, that contains nil

For debugging I've been just doing this

Code: Select all

    local layer = gameMap.layers["PathFinding"]
    print (layer.data[1])
    print (layer.data[1][1])
    print (layer.data[1][1][1])
I first get a table, which I was assuming might mean its a 2d array, so then I tried indexing further and got another table?, and the last index is just nil? I must be misunderstanding how to access this mapping. I just want to generate a 2d array for jumper based off the tile ids 2 and 3 (2 being obstacle) (3 being walkable).
User avatar
marclurr
Party member
Posts: 112
Joined: Fri Apr 22, 2022 9:25 am

Re: Confused on how to access the data field of STI tiled file

Post by marclurr »

I've not used STI before but had a quick gander at the source code and it's building a fairly complex representation of the map under the hood. You're right that layer.data is a 2D array, but the value stored there is a table with a load of metadata about the tile there. From what I can gather the field "gid" is the original ID as it appears in the file generated by Tiled so you could do something like this:

Code: Select all

local layer = gameMap.layers["PathFinding"]
print (layer.data[y][x].gid)
To simplify your pathfinding you might want to generate your own array with just the values you care about, rather than coupling it to STI.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests