Toggle Full Screen

Godot Version

4.3

Question

what’s the correct way to toggle full screen / windowed mode?

I use this in a script:

var fullscreen = false

func _process(_delta):
	if Input.is_action_just_pressed("full_screen"):
		fullscreen = not fullscreen
	toggle_fullscreen()

func toggle_fullscreen():
	if fullscreen:
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
	else:
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)

This works at the beggining, but sometimes (always?) after a minute or so it starts to bring a millions of these errors:

E 0:01:16:0498   utilities.gd:25 @ toggle_fullscreen(): Parameter "hicon" is null.
  <C++ Source>   platform/windows/display_server_windows.cpp:3198 @ set_icon()
  <Stack Trace>  utilities.gd:25 @ toggle_fullscreen()
                 utilities.gd:10 @ _process()

And the game eventually crashes.

Thank you!

You are setting the window mode every frame, if you indented toggle_fullscreen() it would only try to set the window mode when toggled.

I use this script for most games, main difference is putting it inside a _input override instead of every frame.

extends Node

func _input(event: InputEvent) -> void:
	if event.is_action_pressed("Fullscreen"):
		var mode := DisplayServer.window_get_mode()
		var is_window: bool = mode != DisplayServer.WINDOW_MODE_FULLSCREEN
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN if is_window else DisplayServer.WINDOW_MODE_WINDOWED)

omg! you are absolutely right!

i was so focused on that “hicon” error that i didn’t even notice about calling the function 24/7!

by the way, you just increased my fps from 40 to 280 haha

thanks a lot!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.