How to use get_drag_data(position) function

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Faizan Ahmad Mir

So I am new to godot and I want a drag and drop functionality in Godot,
I was able to check an online tutorial but wasnt able to replicate the functionality as “get_drag_data(position)” wouldnt trigger ,Can someone guide me with the same please.
For what I have tried is this
1)Make 2 control nodes inside a node2d
2)one is origin and the other is the destination
3) each node has a background color and a foreground color property which are actually colorrects
4)the code for origin is as follows

extends Control

 func get_drag_data(position):
print("called")
var preview = self.duplicate()
preview.modulate.a =1 
preview.get_node("color").rect_position = self.rect_size/2
preview.get_node("bkg").hide()
set_drag_preview(preview)
return self

the code for destination is as follows :

extends Control
    func can_drop_data(position, data):
return true
   func drop_data(position, data):
$color.color = data.get_node("color").color
data.get_node("color").color = Color(randf(), randf(), randf(), 1)

The problem here is when I start drag in the origin none of the functions gets called

Can you give more info about what you’ve tried?

andersmmg | 2020-04-03 16:03

Hi I have edited the OP for the same,Please check

Faizan Ahmad Mir | 2020-04-04 09:13

In Godot 4 is
_get_drag_data(at_position)

luislodosm | 2022-09-25 19:08

:bust_in_silhouette: Reply From: njamster

Your code works fine. Make sure to setup your nodes correctly as well:

  • The Control-nodes (the two children of the Node2D) have to have a rect_size bigger than Vector2(0, 0) - that will be the clickable region.
  • The ColorRect-nodes will “steal” the input if they overlap with the Control-node if the default mouse_filter is used. Set it to “Pass” or “Ignore”.

Hey
I was able to figure this out ,Thank you ,it works perfectly now …Cheers !

Faizan Ahmad Mir | 2020-04-05 13:41