(ANSWERED) How to get a node in a position

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

I want to delete a node that is in certain position, how can i get a node only by knowing the position?

For example, there are many nodes, and i want to delete the one that is at 100,52 for example, how could i do it?

:bust_in_silhouette: Reply From: YeOldeDM

Any object you want to check the position of should be in a group. You could then iterate through the nodes in that group and check their position against the one you’re looking for.

func kill_node_at_pos( pos=Vector2(100,52) ):
    for node in get_tree().get_nodes_in_group("guffins"):
        if node.get_pos() == pos:
            node.queue_free()

This most likely will be an ineffective way of doing what you’re doing, unless you’re quantizing your positions in some way. If the node is at position (100, 52.00001) it will fail the check.

This will only work if your node is EXACTLY at the position you test, in pixels, without any consideration about its size or shape. That should be ok for tile-based games with no movement animation from tile to tile.

Zylann | 2017-06-07 12:36

:bust_in_silhouette: Reply From: eons

Objects you want to check for overlaps should be or have a CollisionObject (area, body, with shape), then you can use the Physics server functions to check point, ray or shape intersections.

http://docs.godotengine.org/en/stable/classes/class_physics2ddirectspacestate.html

In case someone needs it I put an example in another question: Replace randomly placed objects on collision - Archive - Godot Forum

Zylann | 2017-06-07 12:37