This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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

_physics_process(_delta):

    ...

    velocity = move_and_slide(velocity, Vector2.UP)

    for i in get_slide_count():
        var collision = get_slide_collision(i)
        if collision.collider.name == "MysteryBox":
            remove_tile(collision.position)

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!

Godot version 3.4.2
in Engine by (105 points)
edited by

1 Answer

+1 vote
Best answer

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...

by (105 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 Frequently asked questions and 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 [email protected] with your username.