+3 votes

I want to intercept keyboard inputs at higher-level nodes and prevent them from propagating downwards to other child nodes, if possible.
Originally, I was trying to do get_tree().set_input_as_handled(), but input events were still happening in my child nodes.

My root node adds a scene - GameScene - in it's _ready function:

const game_scene = preload('res://scenes/game_states/game_state.tscn')
func _ready():
    var game_scene_instance = game_scene.instance()
    add_child(game_scene_instance)

And my GameScene, it adds my Level1 scene in it's _ready() function:

const level_1 = preload("res://scenes/stages/stage01.tscn")
func _ready():
    var level_instance = level_1.instance()
    add_child(level_instance)

In my stage01 scene, I have a Player scene that looks at input thru unhandledinput() function, and does something based on that input.

func _unhandled_input(event):
    if event.is_action_pressed('ui_down'):
        print("Adding 15 points to the player's score")

But, I do not want my Player scene to receive any input. So, in my GameScene, I implemented it's unhandledinput() function to mark every input it receives as handled.

func _unhandled_input(event):
    get_tree().set_input_as_handled()

But, if I press the 'ui_down' key on my keyboard, I still see "Adding 15 points to the player's score" printed in the console.

in Engine by (20 points)

1 Answer

+1 vote

+1 as your clear question answered a similar issue I was getting.
Answer for me was to put gettree().setinputashandled() in the _input event (with conditions).

by (77 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 Frequently asked questions and 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 [email protected] with your username.