Check if a given position has a Node2D (or subclass)

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

I’m trying to make a prototype for a turn-based dungeon crawler. I say “turn-based” because I want the player to move tile by tile without necessarily using a _process function.

I’ve looked at the 2D Role Playing Game Demo. I notice it uses the TileMap for every game entity; walls, player, NPCs, and items. Before moving to the next tile (of the given direction), the game checks whether it contains a wall tile or another object, using the TileMap.get_cellv() function.

So far so good.

But what if I wanted to have a Node2D (or some subclass of that) for NPCs/enemies and items, instead of the counter-intuitive TileMap? I know the target position, but I can’t seem to find a way of checking whether that position contains a node (while I can check a TileMap for a specific tile).

And I’m guessing many will suggest I use _process and Area2Ds and signals. But I’d rather not, since it’s a game where everything moves step by step and I want to keep it to the bare essentials.

Note: Area2Ds only seem to work asynchronously (aka with signalling), while get_overlapping_areas() doesn’t work properly either.

Thank you both. I ended up using the TileMap as used in the demo and it’s not that bad after all.

GiannisG | 2023-01-10 10:32

:bust_in_silhouette: Reply From: Inces

Designs like these usually require a simple approach to keep track of all objects of the tilemap manually, using a dictionary { Vector : tile content }
You will update it whenever something is created on the map or something moves, and will read from it before attempting to move, just like in the demo.

:bust_in_silhouette: Reply From: ChickenFriedCode

Try using raycast to detect in the direction you’re moving towards. It will detect every thing in that next tile without having to keep track of it in a loop. Which can decrease game performance.

Here is a good youtube video explaining something similar to what you’re looking for:

He shows you how to use a ray cast to detect what is in the direction of the character, then to do something accordingly (just stop walking if it’s a tilemap item or ignore if it’s any thing else, in this example) And the characters stay on grid. Never moving off it.