ilia_here
ilia_here

Reputation: 33

Getting Contact Information with Tiles in Godot

Can I somehow get information that the player has touched a specific tile located in the resource (tilemap.res)? It is necessary to determine which tile it stands on (for example, on land or in water)

Upvotes: 0

Views: 1432

Answers (1)

Putnam
Putnam

Reputation: 714

It depends on what kind of behavior you're expecting. There's two major ways to do this:

  1. In each individual script that you want to be affected by the tiles, get the tile it's on every time it moves, then run the logic directly from there.
  2. As above, get the tile the unit is on each time it moves, but instead of just running the logic every time, emit a signal whenever the tile changes, and keep the current tile cached so you can check when it's on a new tile; have the TileMap itself attach to the signal, and when it receives the signal, act on the Node2D until it leaves. This is what I'd do.

The exact method to find the tile is TileMap.world_to_map(TileMap.get_cellv(Node2D.position))--you just need to have the Node2D and the TileMap, and you can get both in the same function using the above two methods.

Technically, you could also procedurally add Area2Ds to the tilemap based on the tiles at various positions using TileMap.get_used_cells_by_id, making sure that the ID is the one that has the special behavior, then attaching the TileMap to those areas' body/area_entered and body/area_exited and using that, but spamming a whole lot of Area2Ds isn't necessary when you can check the tile a Node2D is on directly.

Upvotes: 1

Related Questions