How to get an object/node in a scene based on its position?

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

I’m trying to get a node in a scene I have but the only reference I can use is its position. Anyway I can use that to locate the node?.

I don’t get it. You can’t know it’s position before actually getting the node to query the position info. Formulate better your question.

rredesigns | 2017-05-13 06:08

:bust_in_silhouette: Reply From: eons

Is a weird situation but if the Node is a CollisionObject with a shape, casting a ray/shape in that position will get what you want.

Otherwise you will have to iterate over all the nodes and check their positions (grouping or locating all the lost nodes in the same parent may speed up the search).

The reason for this is because I’m using a GridMap and the GridMap class is less fleshed out/ half done in my opinion. My character is standing at a point on the grid but I need to get the object that’s at that cell on the grid. Using get_cell_item returns a int pertaining to the items in the mesh library you use not the node at that point. So fo example if I want to delete the node that my character is standing on there’s no way to actually get that specific object/node from the GridMap. TileMap class is far more robust and fleshed out in contrast.

vonflyhighace2 | 2017-05-13 15:15

Is because there are no nodes on the gridmap, is just like a node with a bunch of meshes.

Transform the Node position to gridmap coordinates (Tilemap has a method for that but gridmap not), should be like “translation/grid size” rounded down.

With that position you can get the cell item below it (y-1), knowing the cell item you will see if exists (!=-1) and if can be removed/changed according the cell item data, to remove it just set the cell item as -1, like with Tilemaps.


I think there are requests for updates on Gridmap (which lack of a lot of features comparing to Tilemap), you can search and support these requests on GitHub to give it more attention for contributors (or add features yourself if you can).

eons | 2017-05-13 17:00