_gui_input: Checking if neighbors can take focus before calling accept_event()

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Kanor

I am right now making a menu that pops up mid-game, but that doesn’t pause the game. I want the menu to consume all inputs so the player doesn’t start moving, but I also want to rely on the focus_neighbors system to make changing the menu easy if I need to add stuff later.

Here’s the important bit of code:

func _gui_input(event):
    accept_event()
    if event.is_action_pressed("ui_cancel"):
	     get_node(menu_path).close(true)

The problem is, accept_event() comes BEFORE checking for focus_neighbors! I want left/right/up/down input to use focus neighbors if able, and if it can’t, consume the input so the player doesn’t hear the input and start moving. Is there a way to do that?

A different option might be to simply wrap your input event processing code in an if block that’s aware (via some boolean value) of whether the menu is open or not. If the menu is open, simply don’t execute the code that processes the input events you’re trying to avoid. Then, when the menu closes, reset the boolean so that the events are processed as normal.

jgodfrey | 2023-03-11 23:41

That’s a good idea! If there’s no way to do the above, I might go with that. But honestly, this feels like something that should be solvable, right? The Control node exists for menus that take control out of the game. That’s why _unhandled_input exists, after all. It would be self-defeating to having a feature in Control nodes that doesn’t respect that need.

Kanor | 2023-03-12 00:33