Is it possible to add properties to tiles in TileSet & access tiles?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ferryman
:warning: Old Version Published before Godot 3 was released.

Hello,

as a complete beginner I’m trying to do a simple roguelike and was wondering if there’s any way to add additional properties to the tiles or TileSet, something like:

export(bool) var passable

This would allow me to edit the properties directly in the editor without the need to keep an external array/list where I would store information whether player can move on the tile position. I’m not sure however if it’s possible to add script to the TileSet though.

Even if that is not possible the other main issue is that I don’t know how to access the TileSet resource. Even if I can get the tile index in the TileMap I cannot use the tile in the TileSet, getting error “Invalid get index ‘0’ (on base: ‘TileSet’)”.

I’m sure it’s just some syntax error, but I was not able to solve it.
I’m trying to access it this way:

var tilemap = get_tree().get_root().get_node("Node2D/TileMap")
var cell = tilemap.get_cell(0,0)
var tileName = tilemap.tile_set[cell].Name
:bust_in_silhouette: Reply From: mateusak

No, it’s not possible to attach variables to cells. You can make a separate 2D array with flags and keep it updated, as a workaround.
No, it’s not possible to attach a script to a TileSet. It’s possible to attach it to a TileMap, though.
I think that’s what you mean:

var tilemap = get_tree().get_root().get_node("Node2D/TileMap")
var cell = tilemap.get_cell(0,0)
var tileset = tilemap.get_tileset()
var tileName = tileset.tile_get_name(cell)

That’s something that is being discussed in GitHub. It would be very useful to attach variables/scripts to certain tiles, but it’s not possible in Godot right now. TileMap should be used to make the static terrain, and if you want special tiles, you need a workaround.

It is possible to attach a script to a tileset.

  • Just create a new script that extends TileSet, export the variables you want as usual.
  • Save the script somewhere.
  • Then in the property inspector for the TileSet containing your tiles, add the script at the bottom of the inspector.
  • Now use the arrows at the top of the inspector to go back, then forward, (to refresh the pane) and you’ll see your script variables.
  • You can then save your tileset as normal, and edit the properties as you wish.

You can also easily edit and access the properties through scripting as usual.

xelivous | 2016-09-07 23:03

Interesting. I’ll look into that.

mateusak | 2016-09-07 23:45

:bust_in_silhouette: Reply From: xelivous

Even if that is not possible the other main issue is that I don’t know how to access the TileSet resource.

var tilemap = get_tree().get_root().get_node("Node2D/TileMap")
var tileset = tilemap.get_tileset()
var tilename = tileset.tile_get_name(0)

or alternatively:

var tilemap = get_tree().get_root().get_node("Node2D/TileMap")
var cell_id = tilemap.get_cell(0,0)
var tilename = tilemap.get_tileset().tile_get_name(cell_id)