window at screen position not working

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

I’m having this issue, where I want to get the Screen ID at which my window is at currently. So I found this handy DisplayServer.get_window_at_screen_position() function. And I wrote:

var window_pos = DisplayServer.window_get_position()
var current_display_id = DisplayServer.get_window_at_screen_position(window_pos) 

print(current_display_id)

,but it always returns 0.

2x 2650-1440 Monitors

Honeslty this feals like a bug on the DEV Side. Still if somebody knows a fix, can you please help me out? thx

According to the docs, window_get_position() takes an optional window_id argument, the defaults to 0 if not supplied. So, you’re sample code appears to be asking for the position of window ID 0.

Next, you pass in that position to get_window_at_screen_position(), which returns the ID of the window at the specified position. So, since you requested the position of window id 0, I’d expect that second call to tell you that the window at the specified position is windows 0, as it apparently has.

So, this seems to work as expected based on the posted code.

(Comments based solely on your posted code and the docs. I haven’t experimented with any of the above).

jgodfrey | 2023-04-29 22:41

Your post should make sense, but evan with changine the window_get_position() parameter or just simulating a variable to act like my window position it always return 0.
As an addition the DisplayServer.get_window_at_screen_position(window_pos) method should also return -1 if the window is out of the screen bounds.

Mirolo | 2023-04-30 07:34

Your post should make sense, but evan with changine the window_get_position() parameter or just simulating a variable to act like my window position it always return 0.
Additionally the DisplayServer.get_window_at_screen_position(window_pos) method should also return -1 if the window is out of the screen bounds.

Mirolo | 2023-04-30 07:36

:bust_in_silhouette: Reply From: Mirolo

I found a workaround using the DisplayServer.get_screen_from_rect method:

var window_pos = DisplayServer.window_get_position(0)
var window_size = DisplayServer.window_get_size(0)
var window_rect = Rect2(window_pos, window_size)
var current_display_id = DisplayServer.get_screen_from_rect(window_rect)

print(current_display_id)

Hope this helps in the future.