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

in Engine by (15 points)
edited by

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

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

In Godot 4 is

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".
by (10,555 points)
selected by

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.