Top-down 2d game and I need help creating a script to detect what tile a Point2D is on top of

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

I also need help to remove the tile at the click of a button, what I am creating is a building mechanic for my game.
The tilemap and the player are both sibling nodes of a Node2D, but the player is from a different scene which has a child node of a Point2D. I’m new to godot and I really need some help on this script. It’s been a pain in the ass for the past two weeks.

:bust_in_silhouette: Reply From: cassie

Unless there’s a specific reason to know the exact tile, then all you should need to know is that there’s a tile. Even that may be too much data tbh. I would control all of this in the player script, and track the states that allow the player to do stuff. You’ve given very little here, so I’m assuming a mining game. This is a good instance for a basic ground check, and an action that becomes available only then. Your character could then “mine” and not have to care where he is or what tile he’s standing on. He’ll just mine, and let the tilemap data pass what he mined into the rest of your processes.

All speculation of course, I can’t see your code and am blind guessing at mechanics. I’m certainly willing to help more tho.

:bust_in_silhouette: Reply From: zhyrin

You can get the global position of your Point2D. After you have that, TileMap has a couple of functions that will get the information you need:

  • Node2D.to_local() to turn your Point2Ds global position into your
    TileMaps local coordinate system
  • TileMap.world_to_map() this will convert a 2D coordinate in local
    space into a grid cell coordinate
  • TileMap.get_cell() will return the cell type at a given cell
    coordinate You can use this to query information about the cell in
    the TileSet

As for your other question: you can also get the global position of your click, you can use the same steps to get to the cell you clicked on.
TileMap.set_cell() can be used to clear a cell.