How to poup a PopupMenu at the mouse position?

Godot Version

4.2

Question

I want to have a PopupMenu show itself when the user right-clicks a spot on my window and have it positioned so the top left corner of the popup menu is at the spot where I clicked. How do I do this? I’m finding all the popup_*() methods confusing and none seem to work for a single point.

You can use _input(event) to check the positioning of your click:

func _input(event):
    var pointer = event.position

then work with that inside input function or return pointer variable to work outside

Right. But how do I pop up the popup menu at that point?

Can you show me your pop-up node? I usually just use visible property and set it true or false. You can try with this tutorial on Godot docs

Right now I’m using:

%popup_actions.popup(Rect2i(Vector2i(position), Vector2i(0, 0)))

It works, but it is centered vertically, which is a bit unexpected.

Maybe this tutorial can help you

@onready var pointer: Vector2

func _input(event):
    if event.pressed:
        pointer = event.position

#func show_popup():
    var popup = %popup_actions.get_popup()
    popup.position = pointer
    popup.show()

It’s still centered vertically for some reason. I guess I can live with it, but wish I knew why. Perhaps there is some parameter that affects that?

If you don’t have, by any change, your popup inside a control node centered, I think can be a property that set the vertical center by default.

Remember to disable on project configuration > window > subwindows the embed subwindows option to have absolute coordinates, if you don’t, your popup it’s set with viewport parent coordinates

Its simple, do like this:

$PopupMenu.popup(Rect2i(get_global_mouse_position().x, get_global_mouse_position().y, 100, 30))