How to center game window?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By spicy
:warning: Old Version Published before Godot 3 was released.

Hi, I’m fairly new to Godot Engine and GDScript. I’m working on a little game project of mine. The game itself is run in a non-resizeable window.

I was wondering if anyone knew how to center the game window, so that when I launch my game (ie my very first scene) it starts dead in the center instead of the top left of the screen.

I’ve already looked in the project display settings but there isn’t anything there that I can see that would allow me to do that. So that leaves me to believe that this could be accomplished using GDScript.

Does anyone know how to go about doing this? Any help would be greatly appreciated. Thanks :slight_smile:

:bust_in_silhouette: Reply From: Dana Olson

Look at the Window Management demo included in Godot. It will have what you need.

Thanks for pointing me in the right direction. I couldn’t find any demo’s including in Godot, but I looked on Github and found the demo files you were on about.

This particular line of code seems very interesting.
‘OS.set_window_position(Vector2(0,0))’

I could move the window to the coordinates I wanted, but I’m still no closer to putting it in the center regardless of user screen resolution. Perhaps you could help me a little bit more? Thanks.

spicy | 2016-02-28 22:23

:bust_in_silhouette: Reply From: ndee

I think that should work. This will center your window on the screen.

  var screen_size = OS.get_screen_size(screen=0)
  var window_size = OS.get_window_size()
    
  OS.set_window_position(screen_size*0.5 - window_size*0.5)

Thank you, it works perfectly! :slight_smile:

spicy | 2016-02-28 23:03

Should (for us newbies) have started to put the code in the _ready() section, as well as added indentation for the “OS.set_window_position()”.

func _ready():
>>    OS.set_window_position(screen_size*0.5 - window_size*0.5)

ALSO : you don’t need to include the “screen=0” in the call.

just:

var screen_size = OS.get_screen_size()

darkwiz666 | 2017-05-17 02:27

For whatever reason,

OS.get_screen_size(screen=0)

gives me an error “Unexpected assign”. You can just do what darkwiz666 suggested, which is to pass no arguments, and the function says it will just use the current screen (which is probably better for most people anyways). But if you want to do it like ndee suggested, you just have to take out the “screen=” part like this:

OS.get_screen_size(0)

If anyone could tell me why screen=0 is invalid, that would be nice to know, it seems like it should work fine. But it’s not that important since it works fine without it.

apwhitelaw | 2020-08-13 16:56

:bust_in_silhouette: Reply From: ConnorVex

ndee’s answer is imperfect on computers with multiple monitors. Here is a better solution:

OS.set_window_position(
		OS.get_screen_position(OS.get_current_screen()) + 
		OS.get_screen_size()*0.5 - OS.get_window_size()*0.5)
:bust_in_silhouette: Reply From: rainlizard

Godot 4.0:

DisplayServer.window_set_position(Vector2(DisplayServer.screen_get_position()) + DisplayServer.screen_get_size()*0.5 - DisplayServer.window_get_size()*0.5)