Change betwen portrait/landscape mode in code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Ragnar Brynjúlfsson

I’ve made a simple 2D board game in 16:9 (landscape) aspect ratio for PC, that I want to add support for 9:16 (portrait) mode for mobile. I know I can change the Project Settings in the editor, but I would really like to be able to do this in code. Moving the game pieces around to match the new orientation is not a problem, but I can’t seem to figure out how to change the screen orientation.

I’ve tried OS.set_screen_orientation, OS.set_size, OS.set_window_size, ProjectSettings (width/height, test_width/height, orientation), get_viewport().set_size/set_size_override etc., but no matter what I try I’m not getting the result that I want.

Any suggestions on how to solve this?

p.s. For ultra wide/tall screens I’m fine with black bars, I just want to add support for 16:9 and 9:16.

:bust_in_silhouette: Reply From: ibrahim.semab

To change the screen orientation in code, you can use the Viewport node’s size_override property. Here’s an example code snippet that sets the viewport’s size override to the desired size for portrait mode:

var viewport = get_viewport()
viewport.size_override = true
viewport.size = Vector2(720, 1280)

This sets the viewport to a size of 720x1280 pixels, which is a typical portrait resolution for mobile devices.

You can use a similar approach to set the viewport size for landscape mode:

var viewport = get_viewport()
viewport.size_override = true
viewport.size = Vector2(1280, 720)

This sets the viewport to a size of 1280x720 pixels, which is a typical landscape resolution for mobile devices.

You may also need to adjust the position and size of other nodes in your scene to fit the new viewport size and aspect ratio. For example, you may need to adjust the position and size of your game board and game pieces to fit within the new viewport.

This (again) appears to be a completely unvetted ChatGPT (or similar) response that’s not valid.

@ibrahim.semab - I assume you’re trying to be helpful with this (and other) recent posts, but ChatGPT will happily (and confidently) offer complete garbage in response to many questions. Please attempt to validate any such responses prior to posting them here.

jgodfrey | 2023-05-02 15:50

You are almost right basically I am no longer a game developer and I was trying to be helpful plus I was just checking if ChatGPT’s information is correct but now I have realized that it’s just rubbish thanks for mentioning I just wasted my time

ibrahim.semab | 2023-05-02 16:28

I am not too Old I am just 15.

ibrahim.semab | 2023-05-02 16:30

I was just surprised that how youtubers used it to boost their productivity

ibrahim.semab | 2023-05-02 16:33

I just wasted my time

Not only have you wasted some of your time, but you’ve likely caused the relevant questions to no longer get any additional input. Generally, questions that are marked as having already been answered on the main page don’t get much additional attention… :frowning:

jgodfrey | 2023-05-02 17:15

Thanks for pointing this out. I did try this suggestion with no luck, and eventually gave up. I was going to pick it up again this week, which is why I’m back at my original post.

If I wanted a reply from ChatGPT, I would have used it myself. I hope this doesn’t become a trend, where people start replying on forums using AI to generate their answers trying to be “helpful”.

Ragnar Brynjúlfsson | 2023-06-11 17:52