Problem with logic regarding window size

Godot Version

4.2.1.stable

Question

Hello again! I need help understanding what’s wrong with the logic in this function:

var file = FileAccess.open("user://window.data", FileAccess.READ_WRITE)
var s = file.get_var()
if not DisplayServer.window_get_size() == s["size"]:
	s["size"] = DisplayServer.window_get_size()
	file.store_var(s)
file.close()

Basically the game is remembering the window size the player sets, it saves in a file to be loaded when the game boots. It works the first time; the second, it keeps going back to the original size the player set and don’t adjust to the new size.

To explain better: Let’s say I adjust the window to be 100x100. I quit the game, boot again, and the window is 100x100.
After that I set it to 200x200, I quit the game, and when I boot it’s 100x100 again, not 200x200.

I don’t know what’s wrong in the code regarding logic.
Thanks for the help!! Much appreciated :heart_hands:

When does this function run?

It seems you only read the “size” but never actually set it. You need to add something like get_window().size = Vector2i(width, height)

On exit. It works the first time, it creates the save file and everything is nice, but the second time the game exits (with a different window size of course) it doesn’t rewrite the new size in the save file.

Yes it reads but the ‘file.store_var(s)’ should store what it read in the save file, no? Because it works the first time: there is no save file, the function runs, create a save file, and the next time I boot the game the window size is the one in the save file; but the second time it doesn’t work… Now I understand that for the first time the code is a little different: the ‘s[“size”] = DisplayServer.window_get_size()’ and ‘file.store_var(s)’ are not within a if function… so it works like this:

if save file exists:
— if DisplayServer.window_get_size() different than the one in the save file:
------- save current window size in the save file
else:
— create new save file with current window size

the first file creating is the same coding as the second time, except for 2 things: second time is withing a if function and it’s READ_WRITE, unlike the first save file creation that is just WRITE; the reason for that is that in the second (or any amount of times really) I want to read with ‘s[“size”] = DisplayServer.window_get_size()’ and write with ‘file.store_var(s)’. Here’s the code for the first time creating the save file:

var file = FileAccess.open("user://window.data", FileAccess.WRITE)
var s = {}
s["size"] = DisplayServer.window_get_size()
file.store_var(s)
file.close()

It’s so similar to the first one I don’t know why it’s not working. Now that I’m writing this I noticed that ‘var s = {}’ in the first time and ‘var s = file.get_var()’. The thing is and I tried this yesterday, I can’t put ‘var s = {}’ again because the ‘var s’ is already expressed in the function… I don’t know if the problem is here but I’ll keep investigating.
Thanks for your help you answered many posts of mine already! :hugs: