Godot Version
4.7.1
Question
Why is it so difficult to find a reliable way to change the screen resolution. I have been trying for days, reading docs and watching videos. But nothing works consistently. The following code works perfectly on my Mac, and only partially on my Linux machine,.
Is there an easier way?
func _on_item_selected(index: int) -> void:
# Calculate the new window position.
var screen_size = DisplayServer.screen_get_size()
var screen_position = DisplayServer.screen_get_position()
var window_size = RESOLUTIONS.values()[index]
var left = (screen_size.x/2) - (window_size.x/2)
var top = (screen_size.y/2) - (window_size.y/2)
# Resize and reposition the window..
DisplayServer.window_set_size(RESOLUTIONS.values()[index],get_window().get_window_id())
DisplayServer.window_set_position(Vector2(screen_position.x + left,screen_position.y +top),get_window().get_window_id())
Likewise the following code works perfectly on my Mac, and Linux, but not at all on WIndows:
func _on_item_selected(index: int) -> void:
if index == 0:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED,get_window().get_window_id())
DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_BORDERLESS, false, get_window().get_window_id())
$"../../HBoxContainer/ResolutionMenu".disabled = false
$"../../HBoxContainer/ResolutionMenu"._on_item_selected($"../../HBoxContainer/ResolutionMenu".selected)
if index == 1:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED,get_window().get_window_id())
DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_BORDERLESS, true,get_window().get_window_id())
$"../../HBoxContainer/ResolutionMenu".disabled = false
$"../../HBoxContainer/ResolutionMenu"._on_item_selected($"../../HBoxContainer/ResolutionMenu".selected)
if index == 2:
DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_BORDERLESS, true,get_window().get_window_id())
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_EXCLUSIVE_FULLSCREEN,get_window().get_window_id())
$"../../HBoxContainer/ResolutionMenu".disabled = true
Thanks
Are you embedding your game when running?
This works perfectly for me, both Linux and Windows
func _on_option_button_item_selected(index: int) -> void:
var window := get_window()
window.size = SIZES[index]
Why not allow the user to resize the window how ever they like, rather than by pre-defined numbers?
Thank you for your reply.
No, I’m not embedding anything. I’v only been using Godot for a few days, so don’t even know what that means. I want to offer a selection of sizes so that my content stays on the correct aspect ratio.
Perhaps it is a problem with PopOs.
Then you very well may be embedding your game, it’s the default setting. After hitting play disable these two settings, they may be restricting your window operations
You can set the scale mode in your project settings to ensure the assigned aspect ratio. In your project settings assign “display/window/stretch/mode” to either “canvas_items” or “viewport”, the latter option is best for pixel art, and the former for anything else.
Check the Stretch aspect project settings (you probably want keep for your use case).
I dont seem to have that menu. Is it on the game window itself?
Ok found it… but it didnt help
Is your code the same? Did you try my sample? If not, can you share what is inside of your RESOLUTIONS variable, how it is defined?
I did change it inspired by your suggestions, it is now:
func _on_item_selected(index: int) -> void:
# Calculate the new window position.
var screen_size = DisplayServer.screen_get_size()
var screen_position = DisplayServer.screen_get_position()
var window_size = RESOLUTIONS.values()[index]
var left = (screen_size.x/2) - (window_size.x/2)
var top = (screen_size.y/2) - (window_size.y/2)
# Resize and reposition the window..
get_window().size = window_size
get_window().position = Vector2(screen_position.x + left,screen_position.y +to
The RESOLUTIONS dictionary is as follows, but this is not the problem since it works fine on the Mac.
(I have not yet had a chance to check this updated code on Windows)
const RESOLUTIONS:Dictionary ={
"1152 x 648": Vector2i(1152,648),
"1280 x 720": Vector2i(1290,720),
"1600 x 900": Vector2i(1600,900),
"1920 x 1080": Vector2i(1920,1080),
"2560 x 1440": Vector2i(2560, 1440),
}
thanks
I am wondering if my problem with Linux is to do with the new PopOS Cosmic desktop.
Could be, window managers are allowed to do as they please with window requests. Do you know if that’s X11 or Wayland? You can install other desktop environments alongside your preferred one, maybe try out a basic fluxbox for X11 or, well Wayland doesn’t have many “basic” environments, maybe LXQt? Different desktops should be selectable on your login screen.