Setting and changing button focus in a PopupMenu

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

In Godot 3.0, I am trying to implement a PopupMenu which allows scrolling through its buttons using the keyboard.

My scene structure is

PopupMenu
    > ColorRect
    > Button1
    > Button2

Focus mode on the PopupMenu is set to All.

I have a very simple script on the PopupMenu which attempts to assign focus to Button2:

extends PopupMenu

func _ready():
    get_node("Button2").grab_focus()

When I press escape, I call show() on the popup and it is displayed, but neither of my buttons are focused on. So my question is: How can I set the focus on buttons in a PopupMenu and scroll through them with keyboard?

I noticed that if the buttons are children of a VBoxContainer as the root node, it works as desired. But not if the VBoxContainer is a child of the PopupMenu.

:bust_in_silhouette: Reply From: Pieter-Jan Briers

First of all, I’m pretty sure that to open a Popup, you’re supposed to be using methods like popup(), not show(). Then again I might be wrong on that.

Either way: _ready() is called when the node gets added to the scene, not when it is shown. You’ll want to hook the NOTIFICATION_POST_POPUP notification (using the _notification virtual) to run code after the popup is opened. (If I was wrong and show() is the correct way to open it, you’ll probably want the visibility_changed signal of Control)

Thanks for the answer. I bet you’re right about using popup() instead of show(). How do I use the _notification virtual? My PopupMenu doesn’t have that listed in its signals.

Diet Estus | 2018-03-19 00:45

_notification is a virtual function you can override just like _ready. It’s defined on the base Object

Pieter-Jan Briers | 2018-03-19 06:34