Several major issues with drag-and-drop

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By grognard3

Hello, I am have set up the following code for a drag and drop interface to move 3 squares around my screen:

var drag

func _on_StaticBody2D_input_event(viewport, event, shape_idx):
if event is InputEventMouseButton:
	drag = event.pressed

func _process(delta):
if Input.is_mouse_button_pressed(BUTTON_LEFT) and drag:
	position = get_global_mouse_position()

The drag-and-drop is fine, but there are a few major side effects that I haven’t been able to troubleshoot:

  1. All my buttons don’t work. Old buttons (prior to the d-n-d coding) and new buttons made after don’t work. If I put a print() command in the on_ready portion of the buttons, it’s fine, but the on_pressed part of the buttons won’t work at all. I assume it’s because the EventMouseButton is being co-opted by the d-n-d interface, but I’m not sure how to correct this.

  2. Dragging the squares leaves them on top of some objects, and below others. They are below one of my buttons, and square 2 always flies below square 1 but above square 3. I tried setting the z-index to 1, but that doesn’t seem to change anything. Is there any solution to this?

Thanks so much!

Sorry, I was a little to quick on the submit button - the first issue was actually already resolved. I am still have problems with the dragged items flying below other objects.

grognard3 | 2019-05-27 00:09

:bust_in_silhouette: Reply From: Thomas Karcher

If you want the dragged node to be in front, you not only have to raise the Z-Index of this node, but also reset the Z-Index of all previously dragged nodes whenever the drag flag changes:

var drag

func _on_StaticBody2D_input_event(viewport, event, shape_idx):
    if event is InputEventMouseButton:
        drag = event.pressed
        z_index = 99 if drag else 0

Thank you so much!!

grognard3 | 2019-05-28 10:31