+2 votes

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
in Engine by (17 points)

2 Answers

+4 votes
Best answer

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.

by (1,093 points)
selected by

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.

Interesting. I'll look into that.

0 votes

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)
by (48 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.