0 votes

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?

asked Dec 10, 2017 in Engine by mronetwo (28 points)

1 Answer

+1 vote

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.

answered Dec 12, 2017 by wombatstampede (3,193 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.