A way to play sound on dropdown item hover

Godot Version

v4.7.stable.official [5b4e0cb0f]

Question

Is it possible to detect a hover event for the dropdown items of an OptionButton? There’s a item_focusedsignal, but no item_hovered signal. We would like to play a sound on hover to increase feedback for the player.

They have the mouse_entered and mouse_exited signals, sounds like that’s what you need?

How can that be used to access the automatically generated PopupMenuItems? I’m not talking about hovering the OptionButton but the items in the dropdown it generates.

So to my surprise, it’s not a built-in thing, despite the fact that item_focused works exactly what you need IF someone uses either a controller OR the arrow keys. Either way, you can make a custom OptionsButton class that has this signal. This is probably not a very efficient way of doing it but something like this worked for me:

extends OptionButton

signal ItemHovered(index: int);

var LastHovered: int = -1;
var popUp: PopupMenu;

func _ready() -> void:
	set_process(false);
	popUp = get_popup();
	popUp.about_to_popup.connect(OnPopupOpened);
	popUp.popup_hide.connect(OnPopupClosed);

func _exit_tree() -> void:
	popUp.about_to_popup.disconnect(OnPopupOpened);
	popUp.popup_hide.disconnect(OnPopupClosed);

func OnPopupOpened() -> void:
	LastHovered = -1;
	set_process(true);

func OnPopupClosed() -> void:
	set_process(false);

func _process(_delta: float) -> void:
	var current: int = popUp.get_focused_item();
	if current != LastHovered:
		LastHovered = current;
		ItemHovered.emit(current);

Thank you for your solution. Unfortunate that we need to do it manually as such. Do you happen to know if PopupMenu.get_focused_item() is expensive? We’ve been trying our best to keep our _process() calls as minimal as possible to keep the game light on the CPU.

That’s why I disable process when the dropdown is not open. I wouldn’t think it’s that expensive unless you plan to have the user open up 100s of dropdown menus at once. That code only runs WHILE a dropdown menu is open in my example.