How to drag a node 2D with a background control wich cannot ignore the mouse

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

I have a simple and especific question:

I have a draggable Node2D and a ColorRect in background. This ColorRect in background is used to prevent me from interacting with what is beyond it, but the problem is that I am unable to interact with the draggable Node2D as well. Somebody has any suggestions?

You can suggest anything, change nodes types, hierarch, add new ones. Any light I’d appreciate.

some node names are in portuguese

:bust_in_silhouette: Reply From: IHate

You could probably fix this by rearranging the nodes and deleting the ColorRect if all it does is block a zone from being interacted with. Instead of having them active all the time, which could generate unpredictable behaviors, you can use booleans to check if a node is active or not. You probably already have something like this in place and that’s how you manage your ColorRect, so instead of handling the ColorRect do it with the nodes directly if there’s a bunch of nodes you want to deactivate you could make a function and hook it with a signal that’s emitted when you want to switch on and off all those nodes.

:bust_in_silhouette: Reply From: exuin

If you look at this diagram from the InputEvent doc page
Diagram
you can see that Controls will always get InputEvents before Area2Ds regardless of node hierachy. So rearranging the nodes won’t cut it. The _input() function gets the event ahead of everything else, however. You can use the _input() function on the draggable node in order to catch it before the ColorRect can.

What I meant with rearrange was deleting the ColorReact altogether in case all it did was blocking the input.

ColorRect in background is used to prevent me from interacting with what is beyond it

But in case you really need ColorRect you can use _input() like Exuin said.

IHate | 2021-04-23 09:27

Wow, I got it.
That hint really seems to solve my problems. But could any of you show me a summarize way in code of how I achieve this? I am a really newbie in Godot for now.
can be just the func input to change the order wich the node is catch

And if that helps, that is my actual code for draggable Node2D

extends Node2D

var selected = false
var final = false

var rest_nodes = []
var final_point
#var pos_origin = global_position

func _ready():
	rest_nodes = get_tree().get_nodes_in_group("Zone")
	
func _physics_process(delta):
	if selected:
		global_position = lerp(global_position, get_global_mouse_position(), 25*delta)
	else:
		if final:
			global_position = lerp(global_position, final_point, 10*delta)
		#else:
		#	global_position = lerp(global_posiition, pos_origin, 10*delta)

func _input(event):
	if event is InputEventMouseButton:
		if event.button_index == BUTTON_LEFT and not event.pressed:
			selected=false
			var shortest_distance = 75
			for child in rest_nodes:
				var distance = global_position.distance_to(child.global_position)
				if distance<shortest_distance:
					final_point = child.global_position
					shortest_distance = distance
					final=true


func _on_Area2D_input_event(viewport, event, shape_idx):
	if Input.is_action_pressed("click"):
		if final==false:
			selected=true

SubBut | 2021-04-23 23:29

Sure, I will help you when I am not busy

exuin | 2021-04-24 14:36

Thank u, I really appreciate.

SubBut | 2021-04-24 15:31

Hmm, so the code you have doesn’t work? It looks like it should work. How exactly doesn’t it work?

exuin | 2021-04-25 02:34

It works, I just wanted to know how I apply that hint with the input thing you gave me, to “ignore” ColorRect, in my code.
Sorry if Im making me hard to understand.

SubBut | 2021-04-25 14:28

The code you have already ignores the ColorRect since the _input() function captures the input event before the ColorRect gets it.

exuin | 2021-04-25 14:45

Oh, so I dont know why this is happening. I guess I will have to get rid of the ColoRect, right?

SubBut | 2021-04-25 19:18

Does it work without the color rect? Are you sure the color rect is the issue?

exuin | 2021-04-25 19:45

Yeah, it works, even if I just assign the Inspector>Mouse>Ignore, it already works.

SubBut | 2021-04-25 20:18