0 votes

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 "getdragdata(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):
var preview = self.duplicate()
preview.modulate.a =1 
preview.get_node("color").rect_position = self.rect_size/2
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

asked Apr 3 in Engine by Faizan Ahmad Mir (15 points)
edited Apr 4 by Faizan Ahmad Mir

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

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

1 Answer

0 votes
Best answer

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".
answered Apr 4 by njamster (9,822 points)
selected Apr 5 by Faizan Ahmad Mir

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

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.