How do I enable VSYNC in gdscript?

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

Ok so I am trying to make a setting in my game to enable and disable VSYNC. I have found some things, but they all give me an error.

:bust_in_silhouette: Reply From: Calinou

The 3D Graphics Settings demo offers a V-Sync setting. For example, if you have an OptionButton with 3 choices (Disabled, Adaptive, Enabled), you can connect its item_selected signal to this function:

func _on_vsync_option_button_item_selected(index: int) -> void:
    if index == 0: # Disabled (default)
        DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED)
    elif index == 1: # Adaptive
        DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_ADAPTIVE)
    elif index == 2: # Enabled
        DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_ENABLED)
:bust_in_silhouette: Reply From: GlitchedCode

Previously we were able to do this with the OS class and call vsync_enabled, setting it to true or false.
In Godot 4, we now use the Display server class to set the vsync mode to enabled, disabled, adaptive, or mailbox.

So to use this we would use a line of code like the following:

DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_ENABLED)

VSYNC_DISABLED: turns vsync off

VSYNC_ENABLED: turns vsync on limiting your fps to your monitors refresh rate

VSYNC_ADAPTIVE: fps is limited by the monitor as if vsync is enabled, however, when frames are below the monitors refresh rate it behaves as if vsync has been disabled.

VSYNC_MAILBOX: Displays the most recent image with the frame rate being unlimited. The image is rendered as fast as possible which could reduce input lag but no guarantees are made. This mode is also known as “Fast” Vsync and works best when your frame rate is at least twice that of the monitor refresh rate