How to toggle fullscreen from code?

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

I’m trying to implement a button in my menu which toggles fullscreen on and off.

Basically, I want clicks of the button to toggle the fullscreen property from Project Settings > Display > Window , but I can’t figure out how to reference it.

I tried ProjectSettings.get_setting("size/fullscreen"), but it wasn’t a correct reference.

I also notice that there’s an OS.window_fullscreen, but I’m not sure if that’s the same as the property listed in Project Settings.

I want to work with this property from Project Settings rather than get and set resolutions of the screen and viewport directly.

Any advice?

:bust_in_silhouette: Reply From: Calinou

As of Godot 3.0, you can just set OS.window_fullscreen to true in any script to enable fullscreen – no need to deal with the project settings manually. It will behave the same way as the project setting does.

Tip: If you want to toggle fullscreen, you can avoid using an if condition by setting OS.window_fullscreen to its inverted value:

if event.is_action_pressed("toggle_fullscreen"):
    OS.window_fullscreen = !OS.window_fullscreen

Thanks. That inversion trick is very cool. Didn’t know about it.

Diet Estus | 2018-05-12 23:52

Thank You, this is a great answer!
I had to change it to the following, however:

if Input.is_action_just_pressed("toggle_fullscreen"):
	OS.window_fullscreen = !OS.window_fullscreen

The godot version I use is 3.0.2

All the best

greymalkin | 2018-08-04 14:34

In godot 3.2, you must use

OS.set_window_fullscreen(!OS.window_fullscreen)

as OS.window_fullscreen is read-only

cheers

frankiezafe | 2021-01-31 00:28

OS.window_fullscreen = !OS.window_fullscreen works in v3.3.2.stable.official for me

Mariothedog | 2021-08-24 13:29

Literally none of these solutions are working for me in version 3.4.3.stable, I have absolutely no idea what I could be doing wrong :confused:

This is just inside my Player script as I wasn’t sure where else to put it. Other keybindings work fine in other functions within this script. I have the button set as the F key in Project settings just like the other keybindings.

func _ready(): 
	animationTree.active = true 
	swordHitbox.knockback_vector = Vector2.DOWN 
	stats.connect("current_health_zero", self, "queue_free")
	OS.window_fullscreen = true
	if Input.is_action_just_pressed("toggle_fullscreen"):
		OS.window_fullscreen = !OS.window_fullscreen

What in the world am I doing wrong?? I’ve tried it without the OS.window fullscreen = true line, I’ve tried it with the Fullscreen checkbox checked and unchecked in the project settings, I’ve tried using different keybindings, and I’ve tried all of the above solutions.

PIZZA_ALERT | 2022-03-07 07:40

_ready() is only called once, when the node enters the scene tree and is therefore not suitable for checking input events.

IsThisTheFuture | 2022-03-07 23:22

:bust_in_silhouette: Reply From: jeroenheijmans

In Godot 4.x things seem to have changed slightly again, these functions have moved to the DisplayServer singleton. This worked for me:

func swap_fullscreen_mode():
    if DisplayServer.window_get_mode() == DisplayServer.WINDOW_MODE_MAXIMIZED:
        DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
    else:
        DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_MAXIMIZED)

See:

thank you for sharing this

sanket | 2023-04-21 19:32

@jeroenheijmans That isn’t exactly a solution for true toggling though :confused: I’m looking for a toggle loop that can store what the display mode was before toggling. This will allow players to set their display mode to whatever they want and if they hit the fullscreen button it will remember what to switch back to.

Update!!!
I’ve got it. This code (4.0.3 stable) will allow you to switch between exclusive fullscreen (value 4) and whatever was viewed previously to exclusive fullscreen. If the game started in exclusive fullscreen, there’s a loop in there too for switching it to maximized (value 2):

#top of script
@onready var previous_window = DisplayServer.window_get_mode()
@onready var current_window = DisplayServer.window_get_mode()

#body of script
func _input(event):
	if Input.is_action_just_pressed("toggle_fullscreen"):
		current_window = DisplayServer.window_get_mode()
		if current_window != 4:
			previous_window = current_window
			DisplayServer.window_set_mode(4)
		else:
			if previous_window == 4:
				previous_window = 2
			DisplayServer.window_set_mode(previous_window)

PIZZA_ALERT | 2023-06-23 05:17

2 Likes

This solution worked for me in Godot 4.1.3. It goes to a true fullscreen and gets windowed on a secondary key press. This statement was put in the _unhandled_input(event) function in the player script.

# Set the fullscreen toggle key to f11 because that's what it is on my keyboard
if Input.is_action_pressed("ui_home"):
	if DisplayServer.window_get_mode() == DisplayServer.WINDOW_MODE_FULLSCREEN:
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
	else:
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
1 Like