How to get a node on which user clicked in 3D?

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

Hi, I have stumbled on something that I can’t figure out.

The situation is as follows: I’ve got a scene inheriting from DAE file. It has some parenting in it (some objects are parents for others). I also create collision meshes by modelling them in Blender and appending -colonly to the name. I try to pick those objects with mouse clicking on them. I use input_event signal from StaticBody node. The signal returns the index of the collision object user clicked on (shape_idx). But for me it always returns 0 no matter on which object I click.

enter image description here

Here is the image that shows the nodes in the scene. There are two StaticBodies with collision shapes. I connect them to the method which handles clicking like so:

			if N is StaticBody:
				N.connect("input_event", self, "clicked_on", [])

And handle the clicking with:

func clicked_on(camera, event, click_position, click_normal, shape_idx):
	if event is InputEventMouse:
		if event is InputEventMouseButton:
			if event.is_action_pressed('mouse_lmb'):
				print('clicked on me: ' + str(shape_idx))

I click on one and it prints ‘clicked on me: 0’. I click on the second one the same happens. What am I missing?

:bust_in_silhouette: Reply From: wombatstampede

The shape index is a relative index to the CollisionObject/StaticBody. This is because you can have multiple shapes assigned to one CollisionObject.

If you don’t want to handle the event in the StaticBody itself, give the node as parameter to the event:

...
if N is StaticBody:
   N.connect("input_event", self, "clicked_on", [N])
 ...
 func clicked_on(camera, event, click_position, click_normal, shape_idx, node):
     if event is InputEventMouse:
         if event is InputEventMouseButton:
             if event.is_action_pressed('mouse_lmb'):
                 print('clicked on me: ' + node.get_name())

This is untested but I did something similar with a value_changed event.