+2 votes

I have created a custom control node that I am using as part of my GUI. It takes input as follows:

func _input_event(event):
if (event.type == InputEvent.MOUSE_BUTTON):
    if (event.pressed):
        print("control mouse button pressed")
        active=true
        accept_event()
    else:
        print("control mouse button released")
        active=false
elif (active and event.type == InputEvent.MOUSE_MOTION):
    print(event.relative_pos)

I then have an area 2d node that is processing input as follows:

func _input(event):
if (event.type == InputEvent.MOUSE_BUTTON):
    if (event.pressed):
        print("area 2d mouse button pressed")
        get_tree().set_input_as_handled()

In this scenario, I would expect that clicking inside of the control node would produce the output "control mouse button pressed" and "control mouse button released" and then nothing else since I am accepting the event and control nodes are supposed to receive events first. Instead, I get:

 area 2d mouse button pressed
 control mouse button pressed
 control mouse button released

My understanding was that control nodes receive events first and can accept those events causing them to not be passed on to other nodes. Why is the area 2d node receiving the input event first? Am I misunderstanding this or should I be handling events a different way?

in Engine by (76 points)

1 Answer

+2 votes
Best answer

I had the same problem when I was playing with the input methods in the engine. I had a button and an Area2D node with the _input() callback enabled. When I clicked the button, the _input() method triggered in the area node as well, although it shouldn't have.

I tried using accept_event() but it had no effect. I solved this by using _unhandled_input() instead of _input(). This way, clicking the button didn't triggered area's callback for handling input and clicking outside the button activated the input method in the area.

by (195 points)
selected by

If I want to click mouse button

and changes the property of a sprit to active visibly as phaco it?

Thanks Ceilingdoor! That seems to work. Though it's odd the desired behavior cannot be accomplished using the _input() method in conjunction with the accept_event()

Yeah, that behaviour seems strange. Basically, the input that haven't been handled by some control is routed to _unhandled_input() and the _input() seems to catch any input that comes from the user.
accept_event() doesn't seem to work as well as set_input_as_handled.

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.