Efficient way to get nodes by tilemap cell?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By FlavoredCactus
:warning: Old Version Published before Godot 3 was released.

So i noticed using world_to_map() on the position of a node will give me the position of that node on a tile map but i can’t seem to figure out a way to efficiently get a list of nodes within a neighboring tile.

I made a work around using groups and comparing the world_to_map location of every node in that group:
https://gist.github.com/FlavoredCactus/10c45f2caf0e17efefd037783e00438b

There must be a simpler way or something i’m missing. any thoughts?

Keep it mind tiles are not nodes at all. Do you need to get neighboring tiles, or nodes that are on these neighboring nodes?

Zylann | 2016-08-20 23:53

I need the nodes that exist in that neighboring tile. I’m aware tiles are not nodes at all. I’m using them as a form of measurement.

say a node is at 5,5 and needs to know if a node is at 5,6 before moving spaces.

FlavoredCactus | 2016-08-21 00:23

:bust_in_silhouette: Reply From: Zylann

Generally, in that case you can use the collision engine and raycast up, down, left and right, or use intersect_shape to know if there is something on the neighboring tiles. This requires the objects to have a collision shape.

If your game is really tied to the tilemap grid (objects only move tile by tile in 4 directions), you could have a 2D array in a script of your tilemap, and when objects move they set a value in that grid, so you can test neighboring pretty fast.

Thanks, the one thing I’m wondering about the collision method is what happens when there are multiple nodes in a tile.

I ended up going with the 2D array by the way.

FlavoredCactus | 2016-08-21 04:52

If you can have multiple nodes per tile, you can use a grid of lists if you go that way.
Also with intersect_shape you can get multiple results I think.

Zylann | 2016-08-21 12:19