An Elegant Way to Enforce Proportional Window Resizing and Aspect Ratio Constraints

Finally, I found a way to lock the window aspect ratio and make it scale properly across platforms.

This isn’t a tutorial on how it works — just a little story about how I ended up writing it. If you want to use it, just grab the link below Github Link.

I’m quite new to the Godot engine. I started my first project just three days ago by following this awesome YouTube tutorial, which I found super useful and clear:
:backhand_index_pointing_right: How to make a Video Game - Godot Beginner Tutorial

After finishing it, I wanted to improve it a bit — mostly just to share something cool with my friends. So I began working on platform adaptation.

But then I hit a problem. Maybe it’s a bit of OCD? :sweat_smile: I realized it was surprisingly hard to manage the window size and viewport size properly, and especially to lock the aspect ratio. There was no direct setting for it in the project settings. Bugs were showing up, possibly related to the root viewport node — but I had no idea why.

So I spent dozens of hours experimenting in Godot, digging into the issue. Thankfully, I got inspired by this post:
:backhand_index_pointing_right:Is there a way to lock the window or root aspect ratio?

I rewrote the code many times, got some help from ChatGPT while learning, though sometimes the info was a bit outdated. I even wrote a function to set the minimum window size — only to discover Godot already had that feature built-in :joy:

Anyway — feel free to use the code in your project. :smiley:Just set it to autoload and it should work right away.

Enjoy coding!