Changing Viewport Size in GDScript
Asked Answered
V

3

0

I'd like to be able to change the default resolution of my game's viewport during runtime, to allow the player to toggle between "widescreen" and "4x3" modes.

I've already worked out how to change the window size in GScript, but that's not creating the result I'm looking for. The viewport is retaining its base resolution, and thus, its aspect ratio. Which, yes, I understand, is how the engine is designed to be used. But I'd rather be able to toggle between those two base resolutions so the game knows where to arrange things on the screen, than to rely on scaling and leave the game with no idea whether or not it's in widescreen mode.

Given how easy it is to set that "base" resolution in the project settings, I find it hard to believe there's no way to change it in code. I've been trawling the docs and Google searches, and hitting my head against my project, for hours now, and can't find an answer anywhere.

Does anyone know the command to change the viewport resolution in GDScript? Is it even possible?

Vamoose answered 12/10, 2022 at 19:17 Comment(0)
B
0

What have you tried so far? (so I won't make suggestions that you're already tried)

Blackstock answered 12/10, 2022 at 21:30 Comment(0)
A
0
var viewport = get_viewport()
viewport.size = Vector2(640, 480)
Apparition answered 12/10, 2022 at 21:51 Comment(0)
V
0

Blackstock Previously, I was working on the window size. That was before I realised the (slightly obvious) difference between window size and viewport size. Since the end user can just resize the window themselves, this is a little redundant, other than for making some quality-of-life features.

After that, I scoured the docs for ways to change the viewport size, but could find no clear answer. That's when I started this thread.

Apparition Thanks for the suggestion. I've tried it, but it seems not to be acting upon the base viewport, which is still retaining its default widescreen aspect ratio, resulting in a 4x3 window with black bars - the same result as if I'd still been setting just the window size.

It's also overriding the scaling settings; in fullscreen or a maximised window, the viewport no longer stretches to fit the screen, though I'm guessing I could re-add the scaling settings after setting viewport.size.

For science, I've also tried using viewport.set_size() as both a Vector2 and a Vector2i, and getting get_tree().get_root().get_viewport instead, and the result is still the same in all cases.

Vamoose answered 19/10, 2022 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.