Drag-n-drop Button node triggers when dragged

Godot Version

v4.5.1.stable.mono.official [f62fdbde1]

Question

Hello everyone, I’ve been meaning to set up a system to drag-n-drop buttons around into slots.
Basically, I have two components:

  • ButtonItem (extends Button) are nodes that can be both clicked and dragged/dropped
  • ButtonSlot (extends Control) are nodes that are supposed to contain a ButtonItem

ButtonItem is the parent class of all my custom button-related nodes. In my project (an incremental game), those buttons are at the core of the gameplay.
ButtonSlot is the class for the button’s slots. ButtonItems can be moved from slots to slots.

Anyway, the ButtonItems can be successfully dragged from one ButtonSlot to another, but when dragging them, it triggers their pressed signal. How can I prevent this?

I also tried this solution before :

# button_item.gd
var dragging := false

func _ready():
   # I wanted to make it so that only when the click was released, the button's
   # behavior would run. The actual press trigger would only be used to drag the 
   # node around.
   action_mode = BaseButton.ACTION_MODE_BUTTON_RELEASE
   button_up.connect(_on_pressed)
   # ...

func _on_pressed():
   # if the button is being dragged, skip the behavior
   if dragging:
       return
   # ...

func _get_drag_data(_at_position):
    dragging = true
    # ...

It turned out to not do anything. I must be missing something. I’d really appreciate it if someone could help me with that! Thanks :grin:

(Apologies for bad english, it’s not my first language)

You might find this answer useful. The control already checks if there is a drag, of at least 10px, to avoid clicking the button.

If this part doesn’t work as intended it might require something specific for your system.

1 Like