Trouble managing mouseEvent priorities with Object and Button

Godot Version

4.6.2.stable

Question

So I have a card that moves when you click and drag it, and a TextureButton that changes the texture when pressed(no other mechanic). I have trouble trying to prioritize moving the card than pressing the button when they are overlapped. I don’t know what is the issue. I can press the button and drag the card, but i can’t drag the card and not press the button.

In the tree, the button is on top and then the card. The card has a process priority of 0, and button process priority is 5. Button Control Mouse is Pass.

Here is the code of the card, but I think it doesn’t do much:

extends Node2D
# Variables
var selected = false
var mouse_offset = Vector2(0, 0)
var speed = 400
var tilt_amount = 0.01 # Maximum tilt in radians
var tilt_speed = 10.0
var direction = 0
signal relativo(relative:float)
func _physics_process(delta):
	# Tilting: -direction tilts right when moving left, left when moving right
	var target_tilt = -direction * tilt_amount
	# Using lerp_angle to smoothly rotate
	if selected:
		if direction != 0:
			rotation = lerp_angle(rotation, target_tilt, tilt_speed * delta)
		elif direction == 0:
			rotation = lerp_angle(rotation, 0, tilt_speed * delta)
	else:
		rotation = lerp_angle(rotation, 0, tilt_speed * delta)
	# Called when the node enters the scene tree for the first time.
	
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if selected:
		followMouse()

func followMouse():
	position = get_global_mouse_position() + mouse_offset

func _input(event):
	if event is InputEventMouseMotion:
	# Using lerp_angle to smoothly rotate
		if !event.relative.is_zero_approx():
			direction = -event.relative.x
		else:
			direction = 0

func _on_area_2d_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.is_pressed():
			mouse_offset = position - get_global_mouse_position()
			selected = true
		else:
			selected = false
		print("Soy una carta")
	get_viewport().set_input_as_handled()

Thank you for reading, hope you can help me.

Check for mouse_down and mouse_up instead of is_pressed. Down for the drag code, up for the click code.

1 Like

The issue is that when card hitbox and button hitbox overlap, I can’t click on the card to drag without pressing the button too. When I press the mouse click down, I start draging the card, but the button does an animation too that reflects is being pressed. On Texture Pressed. I want to avoid that. Anyways, thank you for your reply. :slight_smile:

With respect, you can.

You are checking this:

if event.is_pressed()

That is fired when a user clicks on the mouse button - the start of both the drag event and the button press.

If you check this:

if event.is_released()

Then you get the event when the mouse button that has been pressed is released. That only happens when clicking on a button.

So for the button, only listen for release events.

Incidentally, there is a separate input event class for mouse button handling - you might find it easier to work with. InputEventMouseButton