Best practice: where to handle _unhandled_input()

Godot Version

4.6.3

Question

In my scene I have all kind of different objects that should all trigger some kind of action if I click on them.

What is the best spot to place the _unhandled_input() management? For example the typical boiler plate code to raycast and figure out WHICH one of the objects was clicked in the first place, do I put that in the _unhandled_input() of the individual object or should that in the main scene and from there I just then call the functions of the individual objects?

Check this please :

Helpful resource :

The question was not where to place it in the script itself but rather where to place it in the whole project. Does it make sense to have one central scene that is managing all the input events or does it make more sense to code that on the actual node level that receives the input?

I think it really depends on the project and how you setup your control nodes.

But in general I think you should use it per individual nodes. Because that’s kinda why unhandled input is used. From higher priority to lower priority, if the input is not handled it passes through. So if you set up your control nodes properly, it should work as intended.

I also don’t get how raycast and unhandled_input is related. Maybe there is something I miss there.

It is a good question that does not have a correct answer.

Ask yourself: how can I find it? Does my button tightly couple with one node, or does it orchestrate many?

Some things you just learn by doing.

No single solution fits all problems, but the InputManager can be implemented as a plugin or a singleton if your project’s complexity requires it.

Thanks all for your advice! For now I am going with an InputHandler scene that is managing all the input events and is then distributing it to the nodes (e.g. all objects will have a custom function on_click() that is called by the InputHandler and then everything else is up to the node’s implementation.

Let’s see where it gets me :slight_smile:

The link @artemisia posted would help you out here. The fact that you are using a raycast to determine where the mouse pointer is means you have likely overcomplicated your solution. Most objects in the game you would want to click on can already detect mouse clicks on their own - whether 2D or 3D.

Also, _unhandled_input() is typically to catch in-game commands that aren’t caught by your UI. You can use _input() to handle mouse clicks on an object for example, or link to the gui_input signal on Control nodes and bypass a lot of second-guessing.

An InputHandler is a version of the Manager Anti-Pattern, and I recommend against it. There are a lot of threads on here about why that will cause you problems down the road.

If your solution makes you happy, go for it. But, if you’d like some help then I recommend making a post that gives us enough information to help you. You didn’t even tell us if this is a 2D or 3D game.

Thanks @dragonforge-dev ! I have followed that advice and stumbled right into the next issue :smiley:

But I agree, it makes more sense to add the code to the actual nodes where it belongs.