Inconsistent InputEvent trigger order for overlapping CollisionShape2Ds

Godot Version

4.4.1

Question

When I have two overlapping CollisionShape2Ds, the calling order of the _InputEvent function for them seems to be inconsistent. My example tree is organized like this:

  • Field
  • Boxes
    • Box1

Box1 and Field each have a CollisionShape2D that overlaps the other. I have an _InputEvent function on Field and Box1 like this:

Field:

    public override void _InputEvent(Viewport viewport, InputEvent @event, int shapeIdx)
    {
        if (@event is InputEventMouseButton mouseButton)
        {
            GD.Print("Select Field");
            viewport.SetInputAsHandled();
        }
    }

Box1:

    public override void _InputEvent(Viewport viewport, InputEvent @event, int shapeIdx)
    {
        if (@event is InputEventMouseButton mouseButton)
        {
            GD.Print("Select Box1");
            viewport.SetInputAsHandled();
        }
    }

As I understand the documentation, the _InputEvent for Box1 should always be called before the _InputEvent for Field since it is lower in the tree, meaning that only “Select Box1” messages will be seen if Box1 is clicked. However, there seems to be some sort of race condition causing the _InputEvent functions to be called in a random order. When I just click repeatedly on Box1, I would expect to see only “Select Box1” messages. However, I see output like the below:

Select Box1
Select Box1
Select Field
Select Field
Select Field
Select Box1
Select Field
Select Field
Select Field
Select Field
Select Box1
Select Box1
Select Box1
Select Box1

I’ve been researching this for a while now and haven’t been able to come up with any explanation, any thoughts on why this might be happening? Thanks in advance.

It’s either a bug or a mistake in the documentation. But I’m not sure which.

Input events emitted by physics objects aren’t sorted by default. You can enable sorting with Viewport.physics_object_picking_sort

1 Like