Topic was automatically imported from the old Question2Answer platform.
Asked By
xaran_librof
I’m trying to create a context sensitive right-click pop up menu that is displayed near the mouse. However, when I try to call popup() with a position corresponding to the event, the popup shows up way out of position. The relevant code to make the popup appear is roughly the following:
if event is InputEventMouseButton && event.button_index == BUTTON_RIGHT:
$PopupMenu.rect_position = event.position
$PopupMenu.popup()
Other potentially relevant data:
event.position and event.global_position both report the same position
For an expected rect_position of (958, 1130) (as manually placed in the editor), I see a mouse event position of roughly (480,566)
OS.get_screen_size() reports 1920,1080
OS.get_screen_scale() reports 1
There are no explicit ViewPort nodes in the scene.
extends Node2D
func _input(event):
if event is InputEventMouseButton and event.button_index == BUTTON_RIGHT:
$Sprite.position = event.position
And it worked perfectly. You haven’t shared the code for popup() so maybe something odd is happening there. Check your popup menu’s origin is zeroed, it doesn’t have a non-identity basis.
Hmm. popup() is just a built-in method on nodes descended from Popup like PopupMenu (I didn’t write it). I don’t know what you mean when you say this:
Check your popup menu’s origin is zeroed, it doesn’t have a non-identity basis.
Regardless, I also tested with a simplified layout, and position appears to be accurate. I’ll add more information to my question on my current node hierarchy, but I guess I’ll have to debug my current scene to figure it out. Maybe it has something to do with the fact that most of the children in the scene are instanced scenes?
xaran_librof | 2021-10-21 13:40
Oh, I see. Sorry, I misunderstood. I’ve just set up a control and a child popupmenu with this script.
extends PopupMenu
func _input(event):
if event is InputEventMouseButton and event.button_index == BUTTON_RIGHT:
popup()
rect_position = event.position
And it appears where I right click. No idea why yours doesn’t.