0 votes

I am using this to set the game window to the top left corner of the screen:

OS.set_window_position(Vector2 (0,0))

However, the window appears a few pixels to the right. Playing around with it, I have found that a Vector2 (-7,0) will stick the window to the top left corner. Is there an offset? and if so, is it because the game is running in windowed mode?

I do not want to use fullscreen mode or maximize the window, though. Maybe there is a better way/method to achieve this?

Thanks for your attention!

in Engine by (24 points)

I have been trying more stuff since I posted this question.

In borderless mode there are no issues whatsoever. Both OS.getwindowsize() and OS.getrealwindowsize() return the same value (as expected), and OS.setwindow_position(Vector2(0,0)) sets the window at the top left corner perfectly.

In windowed mode, however, OS.getwindowsize() and OS.getrealwindow_size() return a difference of 18x47, that I assume corresponds to the frame of the window, and might vary in each OS and computer. However, in my case, the left and right frames of the window are only 1 pixel wide, which means there is a gap of 8 pixels when trying to position the window at (0,0).

My question will be, is this something intended by Godot or calculated by the OS? Does this extra framing gap is fixed, or changes with a different OS / different computer?

1 Answer

+1 vote
Best answer

I just had this issue as well, and I searched through the source code to find the cause.

The problem appears to be that when the window is in windowed mode, the window decor isn't taken into account to correct the position of the window.

OS.get_real_window_size() - OS.get_window_size() will give the total decor on each axis, so you must divide by 2 to get the amount per side.

Because the window has a title bar, the amount of decor on the top is much greater than on the bottom (the bottom only has the border and shadow), so dividing by 2 doesn't work. There you'd need to get the height of the title bar and subtract it from the y value of the calculated decor.

The problem (at least when I tested) seemed to only affect the horizontal position, so just calculating the horizontal decor and dividing by 2, then subtracting that from the origin (0,0) will give you the position for the window so that it is correctly in the top corner of the screen.

var window_decor = OS.get_real_window_size() - OS.get_window_size() var window_position = Vector2(0,0) - window_decor.x/2 OS.set_window_position(window_position)

I accidentally looked through the Godot 4.0 source code first, so I believe this issue is taken care of because they have a comment that specifically mentions that the window decor is a problem in the old approach. But, I would need to either test it out in 4.0 or read more carefully to make sure.

by (46 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.