Drag and Drop of an Object

Godot Version

4.3 Stable

Question

Hi all am at the very beginning of Godot and GDScript learning adventure

I’m kind of stuck on drag and drop in Godot for a 2D puzzle game that I’m trying to make

This is the script for the Drag and drop that is connected to the Draggable object Scene

var is_dragging = false #state management
var offset = Vector2(0,0)

func _process(_delta):
	if is_dragging:
		followMouse()
func followMouse():
	position = get_global_mouse_position() + offset

 
func _on_area_2d_input_event(_viewport, event,_shape_idx):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.pressed:
			offset = position - get_global_mouse_position()
			is_dragging = true
		else:
			is_dragging = false
		

Draggable object Scene is set up as following

Node2D
-Sprite2D
-Area2D
–CollisonPolygon2D(I have a script that makes this Collison based on bitmap from the Sprite2D after the object has been instantiated)

In another Scene that is my main scene.
I have a script that instantiates this object with different texture for the sprite and creates the said Collison Polygon depending on the said texture depending on what button at the bottom was pressed.

I have checked and the script does generate the object with correct texture and collision but the drag scrip on those object is present but not working.

if I click on the object it doesn’t change the state to draggable i can change the state via inspector tool and it does indeed move.

Scene that i instantiated the object to is set up as
-ControlNode
–TextureRect(background)
–CanvasLayer
—UI stuff and the buttons
–Game as (Node2D)
–Area2D
—CollisonShape2D(Rect Shape)(Holder for the draggable objects that get instantiated )

But it seems the object that gets instantiated is not reaction to the mouse clicks.
So I’m not sure where the problem is.

Any advice or link that can point me in the right direction would be appreciated.

Hi,
have you set the event connection for the area2d correctly? This would be my first guess.

Hi I have connected the in the scene for that draggable object _on_area_2d_input_event to the from area2D to the Node2D that the script is attached to.

is there a way to check this connection after I instantiate that scene in the main scene?