2 issues with my code

Godot Version

Godot 4.3

Question

`I have 2 issues. First, I have a main menu that the player sees as soon as they enter the game. There is an exit button that opens a new scene to confirm that the player wants to leave the game (Are you sure you want to leave? Yes or no). I want to be able to navigate the menus with a keyboard, and so I use the grab_focus thing. But when the player selects “No” and closes the scene, and goes back to the main menu, my buttons lose focus and now you have to manually click on them with your mouse. Is there a way to regain focus?

The second thing is to blur the screen whenever the Are you sure you want to leave? scene enters.

This scene is just a dialog box that pops up in the middle of the screen. Like a small pause menu.

I hope that my explanation was not too ambiguous. Thanks!`

Hi,

Is there a way to regain focus?

Yes, you basically have to call grab_focus on the button you want to focus once the dialog box is closed. That can be tricky to do depending on your code, but one easy way of doing that is, when instantiating the dialog box, send it the node that should be focused when the player selects “No”, by assigning a variable.
Here’s some pseudo code to explain the idea:

class_name MainMenu

@export var quit_button


# Open the dialog box and send it the quit button.
func open_dialog_box():
    var dialog_box = instantiate_dialog_box()
    dialog_box.focused_on_closed = quit_button

And then, you’d have a code like this in the dialog box script:

class_name CloseDialogBox

var focused_on_closed


# Called when player selects "no"
func press_no():
    queue_free()  # Remove the dialog box
    focused_on_closed.grab_focus()

Let me know if that’s clear enough and if that works for you.


The second thing is to blur the screen whenever the Are you sure you want to leave? scene enters.

You should be able to do that fairly easily by using a shader like this one: Simple Blur - Godot Shaders. You can look for another one if you want to, there are plenty of blur shaders there: https://godotshaders.com/?s=blur
Basically, plug the shader/material to a CanvasItem, and place the node between your main menu and the dialog box, and that should do it.

1 Like

It might be that I’m doing something wrong myself, but it doesn’t work. Both the code for the first issue and the second one throw me errors. The export variable and the focused_on_closed are causing issues I think.

The shader thing threw me some strange error that I couldn’t begin to understand, but I’ve only tried the Simple Blur. Maybe I’ll try others and see if those work.

Thanks for your reply! I’ll keep trying.

1 Like

You’re welcome.
Keep trying, but feel free to ask again with some code attached if you’re still stuck.

1 Like