When does _CanDropData fire?

Godot Version

4.3.stable.mono

Question

Hey folks, I’m working on drag and drop right now. The drag and drop works, but _CanDropData is only firing during mouse movement over certain controls. I don’t understand when _CanDropData is triggered. Is there a way to expand the area that it triggers?

From your message I get the impression that the areas for which _drop_data and _can_drop_data work differ, but I don’t see how that’s possible.

From the documentation:

bool _can_drop_data(at_position: Vector2, data: Variant) virtual const :link:
Godot calls this method to test if data from a control’s _get_drag_data can be dropped at at_position. at_position is local to this control.

Basically if your script is attached to a Control node, the area of that control node will determine the area in which _can_drop_data works.

It’s possible to expand the area if you have a parent control node that covers a larger area. You can use a Control’s set_drag_forwarding function to get more control over what controls do checks and which controls will accept the drop. It also depends on which controls accept mouse inputs and in what order.

In your case, you can use set_drag_forwarding on the top parent control, or create a new control that covers a larger area. From my understanding, this area should then also have the _drop_data function set, otherwise there is no response to the drop data event.

Here’s an example of the set_drag_forwarding I use on a specific control: Dimensionfall/Scripts/ItemFoodEditor.gd at ec7a8c018a780fbeaee180c081e6d133a8cccf0d · Khaligufzel/Dimensionfall · GitHub

2 Likes

Thank you for the detailed reply and example! I will look into _set_drag_forwarding, that sounds promising.

1 Like