+1 vote

Using the helpful tutorial at https://docs.godotengine.org/en/stable/tutorials/viewports/multiple_resolutions.html, I successfully made my game render at 1080p no matter the window size. (It has stretch_mode = 2d and strecth_aspect = keep). I can change the values in the project settings dialog and indeed the aspect ratio changes.

However, I'd like to allow the user to pick an aspect ratio at runtime. I tried changing the root viewport size (which didn't work the way I expected), but then realized that there was a way to change the project settings dynmically.

I can call the ProjectSettings class ( https://docs.godotengine.org/en/stable/classes/class_projectsettings.html ) and use set_setting on the base size ("display/window/size/width" and "display/window/size/height"), but the rendering does not change. Calls to get_setting show that in fact, the setting is set.

Is there something I am missing or is this an engine bug? If I'm missing something, how do I properly change the aspect ratio at runtime.

Godot version 3.2
in Engine by (20 points)

2 Answers

0 votes
Best answer

So after some expirementation, I found that calling

GetTree().SetScreenStretch(SceneTree.StretchMode.Mode2d, SceneTree.StretchAspect.Expand, aspectRatioSize);
    GetTree().SetScreenStretch(SceneTree.StretchMode.Mode2d, SceneTree.StretchAspect.Keep, aspectRatioSize);

(I'm using C#, but this should translate to GDScript pretty nicely) caused the changes to be applied and allowed the user to select the render resolution at runtime.

by (20 points)
0 votes

To support multiple aspect ratios, set the stretch aspect to expand instead of keep. Then configure your UI nodes to have correct anchors (otherwise, UI elements will be at the wrong location when the aspect ratio changes).

by (9,370 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.