Button stops emmiting the ButtonUp event when its parent has changed

Godot Version

Godot 4.3

Question

The problem is described in the topic title.
Is there a way to fix or work around this?

Code

using Godot;
using System;

public partial class Draggable : Node
{
	[Export]
	public BaseButton zone;

	public override void _Ready()
	{
		zone.FocusMode = Control.FocusModeEnum.None;
		zone.Connect(BaseButton.SignalName.ButtonDown, new Callable(this, nameof(OnButtonDown)));
		zone.Connect(BaseButton.SignalName.ButtonUp, new Callable(this, nameof(OnButtonUp)));
	}


	public void OnButtonDown()
	{
		GD.Print("ButtonDown");
		//This part changes parent
		Node zoneContainer = zone.GetParent();
		zoneContainer.RemoveChild(zone);
		zoneContainer.AddChild(zone);
	}


	public void OnButtonUp()
	{
		GD.Print("ButtonUp");
	}
}

Result:
image
If I don’t change the parent the ButtonUp event will happen.
image

you can use += instead Connect method, your program will work exactly the same

	public override void _Ready()
	{
		zone.FocusMode = Control.FocusModeEnum.None;
		zone.ButtonDown += OnButtonDown;
		zone.ButtonUp += OnButtonUp;
	}

if you put change parent to OnButtonUp all gonna work as expected,
when your button exit tree he lose focus and don’t exist while re-parent.

in Summary you can

  • You can move your reparent logic to OnButtonUp(),
  • You can remove reparenting?,
  • You can make custom events on keys and mouse after ButtonDown pressed,

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.