Bizarre behavior of DisplayServer.window_set_mode

Godot Version

4.6.3 stable

Question

OK so my game had a simple fullscreen toggle function.

The project is set Window / Mode = Windowed in editor settings.
There’s a fullscreen bool, that starts out false, and whenever you pressed F11, it calls a simple flipper:

		if fullscreen:
			DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED) 
			print(fullscreen, " so set to windowed")
			fullscreen = false
		else:
			DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN) 
			print(fullscreen, " so set to full")
			fullscreen = true

It worked fine.

However, I have started working on a config system, and the first thing I’ve implemented is remembering the value of fullscreen and restoring it correctly. Here I hit a strange snag.

The process is thus:

  • called off the _ready() function of the main scene, the value of fullscreen is read from file (if no file, it is initialized to false)
  • a function is called with said value:
func set_fullscreen(val : bool):
	fullscreen = val
	if val == true:
		print ("setup said ", val, " so full")
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN) 
	else:
		print ("setup said ", val, " so window")
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
  • when the game is closed, the script checks the current value of fullscreen and writes it to config file

Here’s the snag:
The system works fine if the loaded setting is false. The game starts windowed, F11 flips to fullscreen, F11 again flips to windowed, etc.

BUT
If the loaded setting is true, the game starts fullscreen… but the first F11 sets the game fullscreen again, and only the second F11 flips to window. This is NOT an issue with bools. Based on the print() logging above, in this situation, upon the first F11 the game logs correctly true so set to windowed, but remains fullscreen; the second F11 logs false so set to full, and the game becomes windowed. In other words, DisplayServer.window_set_mode behaves incorrectly.

How to remedy this?

Restoring fullscreen from _ready() often races the window manager: the first window_set_mode can be ignored or overridden once the main window finishes mapping, which would explain why the first F11 after a fullscreen start seems to do nothing. A common pattern is to defer the restore one frame (await get_tree().process_frame or call_deferred) after reading the config, then keep F11 on the same code path. Also check whether you are mixing WINDOW_MODE_FULLSCREEN with EXCLUSIVE_FULLSCREEN or borderless settings, since those behave differently per platform. Solid window-state handling is hard; we bump into similar “remember the layout” problems in spatial workspaces too.

Thank you, putting an await of process_frame in the F11 code path (not in the setup part) seems to have solved the problem.