How to get the positions of nearby tiles

Godot Version

4.5

Question

Hi, I’m making a top - down 2D game with enemies that have pathfinding and I’m trying to make them be able to wander around. I was wondering if there was a relatively simple way to give the enemies the ability to read the positions of nearby tiles around them?

There is a function for that:

2 Likes

Could you please give me an example in code?

Define “position of a tile” and define “nearby”?

I want to find the position of the tiles within a certain radius relative to the enemy.

Sorry about going to another guy behind your back.

Also the problem I started having with my new tliemap was that the entire thing was divided into quarters rather than divided by individual tiles.

What does that even mean?

Divided into four equal parts where each sector has a coordinate rather than the individual tiles having one.

How did you determine that?

I moved the enemy around. Also there was some weird debug colors indicating such.

Does each individual tile in a tilemap layer have a position in the world baked into them or is that something that I have to program into them?

Individual cells in a tilemap are identified by cell coordinates which are integers that represent position in a tilemap grid. Those are also called “map coordinates”. To get an actual position of cell’s corner you need to use TileMap(Layer)::map_to_local(), which converts map coordinates to node’s local coordinates. If the node is not transformed, then coordinates returned by that function will coincide with global coordinates. Otherwise you need to additionally transform them to global space using Node2D::to_global()

Thanks