Start a game with saved window size and position.

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

I have a game that sets the window size and position based on saved data.
The code is executed in the _ready() function.
This method is good but it becomes annoying after a while because the game starts with the default window settings and adjusts the window when the game is ready.

What is the proper way of making the window start with the saved size and position (if it exists)?

I’m not looking for a way to adjust the window after running, I want the game to start with the saved position and size.

It’s not a huge problem but it would be great to have this behavior which is similar to almost every other program including Godot itself.

Here’s the code:

func _ready():
	load_window_data()

func save_window_data():
	var new_data = window_data_class.new()
	new_data.window_maximized = OS.window_maximized
	if OS.window_maximized:
		OS.window_maximized = false
		new_data.window_position = OS.window_position
		new_data.window_size = OS.window_size
		OS.window_maximized = true
	else:
		new_data.window_position = OS.window_position
		new_data.window_size = OS.window_size
	ResourceSaver.save("user://user_data/window_data.tres", new_data)
func load_window_data():
	var dir = Directory.new()
	if not dir.file_exists("user://user_data/window_data.tres"):
		return false
	var window_data = load("user://user_data/window_data.tres")
	OS.window_position = window_data.window_position
	OS.window_size = window_data.window_size
	OS.window_maximized = window_data.window_maximized

I’m kind of a noob, but maybe it’s the _init() function or one of these others… that you want…

Godot notifications — Godot Engine (stable) documentation in English

CassanovaWong | 2021-04-12 10:24

Tried these functions already with no success. I know that it’s normal but I’m looking for a way to make the game run with the window size already loaded and not adjust it after running.

Bubu | 2021-04-12 11:43

It is always good to add your code to a question. Guess my answer has overlap with your :-p

clemens.tolboom | 2021-04-13 09:51

Can you format your code a little … just add 4 space for each line :slight_smile:

clemens.tolboom | 2021-04-13 12:03

Sorry, for some reason the formatting is lost when pasting, some underscores are missing as well.

Bubu | 2021-04-13 13:21

:bust_in_silhouette: Reply From: clemens.tolboom

I use this which works like a charm adapted to a long loading ‘main scene’.

# Startup scene
extends Node

func _ready():
    loadwindow_data()
	# Depends on screen location ... ie second screen starts at 1920
	OS.window_position = Vector2(1920,0)

	# FullHD for recording
	OS.window_size = Vector2(1920, 1080)

func _on_Timer_timeout():
    get_tree().change_scene("res://Main.tscn")

Main scene has long running code

# main scene
func _ready():
    for i in range(100_00_000):
        var x = i * i

This is the method I currently use, it works fine until you have more stuff to do when running.

It takes even more time when some settings need to be loaded.

Bubu | 2021-04-13 11:06

I made my ‘main scene’ second in line by adding a startup scene. And the window size code is done my my/your code.

(-snip moved code into answer -snip)

So adding a ‘dummy’ scene to resize first then the long loading scene

clemens.tolboom | 2021-04-13 12:16

I’ve updated my answer taking care of a scene with long running code (‘main’).

Hope this helps and if so please mark my answer :slight_smile:

clemens.tolboom | 2021-04-13 12:21

Nice idea, now the window resizes quickly. However, the program now takes significantly more time to start. This shouldn’t happen, right?
Furthermore, now that the screen is resized even before the boot splash is gone the splash “jumps” and moves to the side of the window, sometimes it gets clipped too.

Nevertheless, thanks for your help.

I’m sorry that the question wasn’t clear enough. Here’s what I really want to do:
Running Godot makes it open at the same position and size as the last session.

It starts as if you changed the window size through the Project Settings.

The window doesn’t open at a default size and then resizes and moves, it actually opens with the intended size and position straight away without the need to be adjusted.

I would like this behavior to be implemented in my application, I don’t strictly need it but I wonder if there’s a way to do this without adjusting the window after running.

It would be beneficial to know how to do this as it makes the window run smoothly, avoids artifacts and ensures that the splash is positioned correctly.

Bubu | 2021-04-13 13:18

You can always update your question for others to have a quick start.

even before the boot splash is gone

Interesting … never done that

that the splash is positioned correctly.

Can one position the splash?

clemens.tolboom | 2021-04-14 06:52

I meant that running the program with the correct position and size without adjusting would eliminate the need to position the splash screen which is not possible as far as I know.

Bubu | 2021-04-14 07:33