2
\$\begingroup\$

I'd been following Godot tutorials to add Drag & Drop functionality to Control nodes. They've presumably been created for Godot 3.x, but my project is 4.0. And I can't seem to get it to work, thinking something has changed.

To make sure it's due to the difference in Godot versions, I did the exact same steps to reproduce the situation in both Godot 4.0 and 3.5.1:

  1. Create new project, all default settings

  2. Add Node2D root scene

  3. Add a PanelContainer child to that

  4. Add a TextureRect child to that, and drag the Godot icon resource on it for testing

  5. Attach a new script to PanelContainer with this code:

     extends PanelContainer
    
     func get_drag_data(_position):
         var preview = TextureRect.new()
         preview.texture = $TextureRect.texture
         set_drag_preview(preview)
         return { id = "foobar" }
    
  6. Run the game

The results:

  • In 3.5.1, as expected, you can drag around the Godot icon;
  • In 4.0, nothing happens, dragging the icon around won't work;

What am I missing? How to get dragging of nodes working in version 4.x?

I've tried a few extra things to answer my own question:

  • Search the web, but at the time of writing it seems to give results that are 3.x-specific;
  • Go through the node's "Inspector" properties, especially Mouse properties
\$\endgroup\$

1 Answer 1

8
\$\begingroup\$

Nearly discarded my question because I found the answer myself, but perhaps just posting it will help others running into the same gotcha...

If you look at the official documentation for both versions, there's a tiny but all-important difference:

  • v3.5 docs: Variant get_drag_data ( Vector2 position ) virtual
  • v4.0 docs: Variant _get_drag_data ( Vector2 at_position ) virtual const

So in v4.x there is an _ (underscore) prefix to the method that wasn't there in 3.x.

The godot engine source code even have a rename map entry for this change, presumably to help with converting projects? For reference: see the commit that introduced this change.

\$\endgroup\$
2
  • 4
    \$\begingroup\$ I think it's worth leaving this up & voted accordingly. As more projects pivot from 3 into 4, this sort of thing is bound to trip some folk up. The (unenforced) convention is for virtual function to have have a leading underscore, so this might be a case of the update bringing the API in line with their own guidelines. Reminder: remember to circle back & mark this is a the accepted answer as it helps others. Good luck with your project. \$\endgroup\$
    – Pikalek
    Commented Mar 10, 2023 at 17:50
  • \$\begingroup\$ Thanks for the encouragement! That makes sense. - The system allows you to accept your own answer after 2 days, will try to remember to do so. \$\endgroup\$
    – Jeroen
    Commented Mar 10, 2023 at 18:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .