Skip to content

Draggable Nodes

Valk edited this page Sep 22, 2024 · 10 revisions

Simply add the [Draggable] attribute to any Node2D or Control node and it will become draggable in-game.

using Godot;

// Apply the [Draggable] attribute to make this node draggable
[Draggable(DragType.Hold, DragConstraints.Vertical)]
public partial class DraggableNode : Node2D
{
    public override void _Ready()
    {
        // Hook into the Draggable.DragReleased event
        Draggable.DragReleased += OnDragReleased;
    }

    private void OnDragReleased(Node node)
    {
        // Check if the released node is of type Player
        if (node is Player player)
        {
            // Do something with player
            player.QueueFree();
        }
    }

    public override void _ExitTree()
    {
        // Unsubscribe from the event when the node is removed from the tree
        Draggable.DragReleased -= OnDragReleased;
    }
}
Clone this wiki locally