I want to make a classic mining a cave mechanic in a 2D game, where the player can detect a tile in front of them and press a key to delete that tile so it looks like you mined it.
But it seems complicated enough for me to not understand how to do it, so please let me know how you'd do it!

I'm guessing I need either an area2D or a raycast2D to detect the tile, and I've watched a few videos about tiles, but I just can't get it to work.
I don't know how to get the tile id, the coords, or whatever you need to erase the specific tile you're detecting! Help! (Using Godot 4 right now)

It also doesn't help that all tutorials I've watched do this by using the mouse. I don't want to use the mouse for this.
Also some functions have been changed/removed for Godot 4, like world_to_map or cellv, so I have no idea!

    Check the documentation for TileMap. You can use the function set_cell to erase a cell. You use local_to_map to convert pixel positons to cell positions. Or you can use cell positions directly (like if you press the arrow key, it adds 1 to the position you are tracking as the current position, which is a integer cell location of type Vector2i).

    https://docs.godotengine.org/en/stable/classes/class_tilemap.html

    BubbleMagex You may have to take the RPG Maker approach where the blocks themselves are "events" rather than tiles, because you can instance them. There are also ways to procedurally generate instances like this to do what you want.

    Coding the blocks this way would be quite simple. You could have a variable that flips when the player is in its area and perhaps check which way the player is facing with a raycast or another area2d that rotates with the player. While putting the blocks out would take a while, the coding would be simple enough. But games are work, after all.

    ### put this in your breakable object header
    var _canBreak = false

    That bool would flip when a player is in its area, but check if the player is facing it. I would use an Area2D with perhaps a square-shaped collision shape the size of your blocks. (Make it smaller so it doesn't register two of them. If you make this box a child of the player or object, it should rotate with the player if coded correctly). So we can put another bool in the player to check if breakable objects are in the path:

    ### in your player object and connect a signal from the area to flip this when a breakable is there.
    var _hasBreakable = false

    Then back in the object we connect a signal from the player (or you can just connect it in code) to check if the body entering the area is registering a break.
    So the signal would look like this, assuming your Breakable object is called "Mining object":

    func __on_Mining_object_body_entered(body_name):
         if body.name == "Player":
            if body._hasBreakable:
                _canBreak = true

    Then just have your input check for the _canBreak and only have it work if _canBreak is true. This is pretty rough code so you'll want to optimize it. Best of luck!

    I don't understand much, honestly, this is what I've done and gotten so far (still doesn't work, no tile gets deleted and nothing happens at all)

    The yellow area of the player is the mining area.

    Console prints when touching a few tiles:

    Something I noticed is sometimes I get a different "tile" result depending from where the area touches the tile, like this one, even if the tile is the same one:

    Also the coords are always the same? This is how my tilemap looks:

    I'm sorry I don't really understand much of what I'm doing, need more help please!

    Those don't look like valid coordinates. I believe it starts a 0, 0 and goes up in values.

    Probably because the body is in the different space than the tilemap. The pixel position should be local to the tilemap, not the global space.

    4 days later

    Alright finally did it with more help too, this is how it looks now (also I forgot to mention I have every node scaled to 3...):

    It finally works! yahoo

    7 months later