0 votes

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
Godot version 3.2.3
in Engine by (288 points)
edited by

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

https://docs.godotengine.org/en/stable/getting_started/workflow/best_practices/godot_notifications.html#ready-vs-enter-tree-vs-notification-parented

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.

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

Can you format your code a little ... just add 4 space for each line :-)

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

1 Answer

+1 vote

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
by (636 points)
edited by

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.

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

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 :-)

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.

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?

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.

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.