To make simple drag and drop ui

Godot Version

godot 4.5

Question

how do use code to make simple drag and drop ui that close and open when click on sorry if sound like i want somebody make it for me it just most tutorials i search only show how to add items and not how drag uit of the inventory

i
If you share code, please wrap it inside three backticks or replace the code in the next block:

var dragging = false
var mouse_offset
var dalay = 8


func _physics_process(delta):
	if dragging == true:
		var tween = get_tree().create_tween()
		tween.tween_property(self ,"position", get_global_mouse_position() -mouse_offset , dalay * delta)
	
func _input(event):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_RIGHT:
		if event.pressed:
			if get_rect().has_point(to_local(event.position)):
				dragging = true
				mouse_offset = event.position - position
		elif event.button_index == MOUSE_BUTTON_RIGHT and not event.pressed:
			dragging = false


func _on_area_2d_area_entered(area):
	queue_free()

There’s actually multiple methods of making a drag and drop UI. One method is to use the built in drag and drop system. Another is to use areas or controls to detect clicks and make a custom system. Which one are you interested in?

1 Like

Use areas and controls option could
i can use a sprite 2d with a area 2d to make basic inventory or do need use a control node

and the code use for drag and drop func is attached to sprite2d

Sorry, I don’t really understand what you are trying to say.