Getting node from an intersects_point()?

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

I’m casting a point and would like to get the node that the point collides with and then check if that node containst a specific component/script. I’m basically making an RPG interact system. My question is, how do I get the node from the return of the intersects_point()?

:bust_in_silhouette: Reply From: eska

From the reference:

Check whether a point is inside any shape. The shapes the point is inside of are returned in an array containing dictionaries with the following fields:

  • shape: Shape index within the object the point is in.

  • metadata: Metadata of the shape the point is in. This metadata is different
    from Object.get_meta(), and is set with Physics2DServer.shape_set_data().

  • collider_id: Id of the object the point is in.

  • collider: Object the point is inside of.

  • rid: RID of the object the point is in.

The return value of intersect_point is an array containing one dictionary per intersecting collision body.
Each one of those dictionaries has a key named collider that has the colliding CollisionObject2D (a Node) as its value.

If the intersecting shape doesn’t belong to a Node, collider will be null (should only happen if you work with Physics2DServer directly).

If you want an array containing just the nodes, you could use this code:

var return_value = direct_space_state.intersect_point(...)
var colliding_bodies = []
for dictionary in return_value:
	if typeof(dictionary.collider) == TYPE_OBJECT:
		colliding_bodies.push_back( dictionary.collider )