I’m trying to figure out why my buttons lose focus immediately. I was able to find out how this was happening by attaching a function to the focus_exited
signal of the button and adding a breakpoint.
As it turns out, a control that was inside of another SubViewport
was grabbing focus. In my game, I have several SubViewport
s in order to render UIs on quad meshes in the game world. However, it appears that when my button in the main viewport is focused, this causes the control in the SubViewport
to get null
when calling gui_get_focus_owner()
.
Apparently, according to godotengine/godot-proposals#7229, this is because gui_get_focus_owner()
will return null
if the focus owner isn’t a child
currently viewports know which control is the focus owner ONLY if it’s directly within them. if it’s within another viewport. they will report a null focus owner.
My code would try to grab focus when the focus owner wasn’t set, and this subsequent grab_focus()
would cause my original button to lose focus. So, it appears that grab_focus
will also cause all other Viewport
s to lose focus.
I think the only way to workaround this is to only grab focus when the corresponding SubViewport
is focused and the player attempts interact with it.