How to programmatically delete tiles and how can you find the name of a tile you are colliding with

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Jackson King

I know it is possible to programmatically place a tile, but is it possible to programmatically delete a tile given that you know it’s location?

And, on a somewhat related note, how can you find the name of a tile that your character is colliding with. Or, if this isn’t possible (as I suspect it’s not), how can you find the name of a tilemap your character has collided with?

:bust_in_silhouette: Reply From: kidscancode

Deleting a tile is documented in the TileMap reference:

$TileMap.set_cellv(location, -1)

When you collide with a TileMap, the TileMap is the colliding object, so you can get the tilemap’s name with name.

It is possible to get the individual tilename by index from the TileSet resource (property tile_set). First you have to find the cell you collided with (assuming KinematicBody2D):

for i in get_slide_count():
    var collision = get_slide_collision(i)
    if collision.collider is TileMap:
        var tile_pos = collision.collider.world_to_map(position)  # character's position
        tile_pos -= collision.normal  # colliding tile position
        var tile = collision.collider.get_cellv(tile_pos)  # colliding tile index

Once you know the index of the tile you collided with, you can get its data from the TileSet:

var tile_name = collision.collider.tile_set.tile_get_name(tile)

See TileSet reference for more information.

1 Like