0 votes

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?
enter image description here

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

some node names are in portuguese

Godot version 3.2.3
in Engine by (16 points)

2 Answers

+1 vote

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.

by (396 points)
edited by
+2 votes

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.

by (7,190 points)

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.

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

Sure, I will help you when I am not busy

Thank u, I really appreciate.

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

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.

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

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

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

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.