Remove a tile from a scene with GDScript

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

How do I remove a tile from a scene programmatically? I have tried the following to no avail:



    velocity = move_and_slide(velocity, Vector2.UP)
	for i in get_slide_count():
		var collision = get_slide_collision(i)
		if == "MysteryBox":
func remove_tile(position):
	var tilemap= get_parent().get_node("Tilesets/MysteryBox")
	tilemap.set_cell(position.x, position.y, -1)

I can detect the MysteryBox when the player collides with it, but the remove function is not removing the tile the player touched from the scene. Thanks in advance!

:bust_in_silhouette: Reply From: keidav

Changed func remove_tile() to pass the collision object and modified the function as follows:

func remove_tile(collision):
	var tilemap = get_parent().get_node("Tilesets/MysteryBox")
	var local_position = tilemap.to_local(collision.position)
	var cell_position = tilemap.world_to_map(local_position)
	cell_position -= collision.normal
    tilemap.set_cell(cell_position.x, cell_position.y, -1)

Now the collision works and the tile is removed form the scene…