0 votes

I have Node2D which needs to process input events unless they are not processed by its children.


Children need to process input only if the mouse is above them, the root doesn't care about pointer position. So I use _input_event for children and _unhandled_inputfor the root. But _input_event is always called after _unhandled_input.

How can I make children process input first?

One of the possible solutions is to make the root Area2D covering the entire viewport, but it looks bad in terms of performance in case of many instances.

Another workaround is to use _unhandled_input on children too and manually check if events position collides with the area. But I don't know how to convert mouse position to local coordinates because scene tree can have any depth and parent objects can be scaled.

Do you have any ideas?

in Engine by (14 points)
edited by

The first solution also doesn't work. Parent.inputevent is called before child.inputevent. Is it intentional or is it a bug?

2 Answers

0 votes

I also found it very weird on the input-call behaviors, in which the inputevent for Area2D is always called after unhandledinput. After I google around, I did not found a proper solution.
Fortunately, after some trick applied, I got a very simple workaround. NOTE, for anyone who does NOT use Area2D input for mouse above, this trick may be not workable.

  1. setup input for Area2D to capture mouse click (not _inputevent);
  2. setup other Node for unhandledinput capture;
  3. do the trick;
    3.1 add an ismouseover flag for the Area2D;
    3.2 link mouseentered message to set ismouseover true;
    3.3 link mouse
    exited message to set ismouseover false;
    3.4 under Area2D input, only handle the event(run your own code) and gettree().setinputashandled() when ismouse_over is true, or you don't even handle the event under Area2D;

The idea behind it. input is always fired before _unhandledinput in real code, so instead of using inputevent, for Area2D I use _input to get the event msg in advance; and only handle the event while the mouse is over this Area2D.

pros & cons. I think all input will be going through this Area2D first, so if you instance this node a lot, maybe will get a performance issue, which I did not test. the nice thing is, at least it works.

by (14 points)
0 votes

Been searching for a similar solution with no luck..

What I ended up doing is emitting a signal by _input_event and then "revert" or "override" the behaviour modified by_unhandled_input

by (14 points)
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.