How do you get TileData from a TileSet (Better Terrain Plugin Issue)

Godot Version

4.4.1

Question

I want to get the TileData Resource for my tiles from a TileSet Resource. I’ve tried to do this by using tile_set.get_source() but that only returns a TileSetSource, which has no method for getting TileData. I’d need to get the TileSetAtlasSource otherwise, and idk how to do that.

all of this is so I can get a list of all the tiles in a given terrain. eg, if i have terrain with id 0, i want to be able to have a list or somesuch of coordinates the tiles that are in that terrain. the initial plan was to get this from each tile’s TileData resource but i cannot get to it from a TileSet.

It’s been a while, but I have a bunch of helper methods here that I believe do what you need, or at least a variation on the theme:

1 Like

As long as you know that you’re TileSetSource is a TileSetAtlasSource, you can cast the variable to one like this:

var tile_set_source: TileSetSource = tile_set.get_source()
var tile_set_atlas_source := tile_set_source as TileSetAtlasSource

If the cast was unsuccessful Godot will silently set tile_set_atlas_source to null, so ideally you should probably check that that’s not the case before using the variable

1 Like

Thanks for the help, this looks like what I was trying to do and I’m pretty sure it would work. Unfortunately (for me yesterday) my problem was a fairly artificial, because I didn’t realise that the BetterTerrain plugin I was using changed where terrain data was stored.

Apparently the TileSetAtlasSource objects were being returned as normal but when i got the terrain data normally it always returned -1, this is because the terrain data is now stored in metadata as per the BetterTerrains plugin.

# how it is normally done
print(source_atlas.get_tile_data(Vector2i(1,0), 0).terrain)
	
# what i should have been doing (using Better Terrains plugin)
print(source_atlas.get_tile_data(Vector2i(1,0), 0).get_meta("_better_terrain"))

So if I was using the default terrain system this would have worked, I just need to add the extra call to get meta data. I simply didn’t realise the plugin changed where the data was stored. Thank you for helping. I’ll mark this as solved.

PS: TileSet.get_source() returns a TileSetSource object but I was unsure why casting it to a TileSetAtlasSource would allow me to get the TileData. To me, because TileSetAtlasSource inherits from TileSetSource I assumed the data in the former would not be present in the latter, which is why i thought my code wasn’t working. (also when you put a TileSetSource into print() it says its a TileSetAtlasSource which was doubly confusing)

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.