PopupMenu did not trigger events

Godot Version

4.4.1

Question

I added a new scene. Added popup menu. Added a few dummy items to the menu. I hooked the ‘id pressed’ to my c# script. But it never triggers. Same with index signal. All signals work fine except the popup menu specific ones. My question is how to use the popup menu? And it’s there proper documentation how to use it somewhere? Thanks on advance.

This seems to work fine:

extends Node


func _ready() -> void:
	$PopupMenu.id_pressed.connect(func(id:int):
		print("Pressed ID %s" % id)
	)
	$Button.pressed.connect(func(): $PopupMenu.popup_centered())

Result:

Thanks for the answer. I can see a couple of differences between our implementations. First of all you used a button to show the popup menu and I want to use right mouse click. Is that a regular button and do you use show() or popup()? Second, you use gdscript and I use C#. I suppose we can assume gdscript and C# implementations should work identically so that should not be an issue.

1 Like

If nobody has any idea based on what I wrote then it’s most likely that my description wasn’t good enough. I can add more details and code later today after I get home. My implementation was roughly like this.
Scene:
Node2D
└── PopupMenu

Setup:

  • menu is set to visible from its properties
  • added four random text only items to the menu
  • hooked the event to a script (the script is held by the parent node)

Code:

private void OnPopupMenuPressed(int id){
    GD.Print("item clicked");
}

Yes.

I used Window.popup_centered() as it’s a popup. Using Window.show() also works.

Yes. They should be the same. I don’t have C# setup here so I can’t test it.

This works fine too:

extends Node


func _ready() -> void:
	$PopupMenu.id_pressed.connect(func(id:int):
		print("Pressed ID %s" % id)
	)


func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventMouseButton:
		if event.pressed and event.button_index == MOUSE_BUTTON_RIGHT:
			$PopupMenu.popup(Rect2(event.global_position, Vector2.ZERO))

Result:

The weird thing is that I don’t see the items react to when hovering over the menu. So the code is probably ok. There must be something preventing events reaching the popup menu specific stuff. Because as I said the mouse enter event works fine with the menu.

Okay, I’ve been looking around and this may be a bug. For some reason when the PopupMenu starts visible the flag Windows.FLAG_NO_FOCUS is set to true so it won’t focus.

The only place where I can see this happening is here godot/scene/gui/popup_menu.cpp at 96579d139951262f7c766fa0ea88583c85aca674 · godotengine/godot · GitHub which make me think that is_embedded() returns false when initializing the scene.

If the PopupMenu starts hidden and you try to show() or set visible to true in _ready() the PopupMenu won’t be shown. If you wait a frame with await get_tree().process_frame the PopupMenu will show and works fine.

If the PopupMenu starts visible and you call show() or set visible to true then the PopupMenu will work without needing to wait a frame. You can also set Window.unfocusable to false and the PopupMenu will work fine (this property just changes the FLAG_NO_FOCUS flag).

I’ll open an issue in the issue tracker.

Edit: Issue opened here Embedded `PopupMenu` with `visible` set to `true` in the inspector can't `grab_focus()` · Issue #105176 · godotengine/godot · GitHub

2 Likes

@Deppis Is possible to get small project showing your problem? can be on git repository
I

I will play around with it first now that I got some hints from @mrcdk. But sure I can put a small repo if necessary.

1 Like

Ok. I got everything up and running. Thanks to @mrcdk and everyone else for your help and thoughts. I appreciate it.

Quite fascinating to see people actually caring about opening bug reports and reading the source code!

1 Like