How to change screen orientation in Godot with code?

  • Thread starter Darkmisc
  • Start date
  • Tags
    godot
In summary, to change screen orientation in Godot using code, you can set the `display` properties within the `_ready()` function of a script. Use `OS.window_size` to adjust the dimensions according to the desired orientation (landscape or portrait) and employ `OS.window_fullscreen` if you wish to enable fullscreen mode. Additionally, you can listen for orientation changes using the `OS` singleton to dynamically adjust the layout during gameplay.
  • #1
Darkmisc
220
31
TL;DR Summary
I need to change the screen orientation using code, but it's not working.
Hi everyone

My app has two mini-games. One uses portrait orientation (which I've set in project settings). The other uses landscape. I need to use code to switch from portrait to landscape. This is what I've tried.

change to landscape:
func _ready():
    OS.set_screen_orientation(OS.SCREEN_ORIENTATION_LANDSCAPE)

When I click on the button to run the landscape game, the orientation stays in portrait.

Does anyone know the right way to do this?

Thanks

EDIT:
I've also tried:
landscape2:
OS.screen_orientation = OS.SCREEN_ORIENTATION_LANDSCAPE

This doesn't work either.
 
Last edited:

FAQ: How to change screen orientation in Godot with code?

How can I change the screen orientation in Godot using code?

To change the screen orientation in Godot using code, you can simply set the "screen_orientation" property of the OS class to the desired orientation. For example, to set the screen orientation to landscape, you can use the following code:

OS.set_screen_orientation(OS.SCREEN_ORIENTATION_LANDSCAPE)

Can I change the screen orientation dynamically during runtime?

Yes, you can change the screen orientation dynamically during runtime in Godot. Simply call the OS.set_screen_orientation() method with the desired orientation value whenever you want to change the screen orientation. For example, to switch the screen orientation to portrait mode at runtime, you can use the following code:

OS.set_screen_orientation(OS.SCREEN_ORIENTATION_PORTRAIT)

How do I detect the current screen orientation in Godot?

To detect the current screen orientation in Godot, you can use the OS.get_screen_orientation() method. This method returns the current screen orientation as an integer value, which you can compare with the predefined constants in the OS class to determine the orientation. For example, to check if the screen is currently in landscape orientation, you can use the following code:

if OS.get_screen_orientation() == OS.SCREEN_ORIENTATION_LANDSCAPE:    # Screen is in landscape orientation

Can I lock the screen orientation to a specific mode in Godot?

Yes, you can lock the screen orientation to a specific mode in Godot by setting the "screen_orientation" property of the OS class to the desired orientation. Once you set the screen orientation, it will remain locked in that mode until you change it again. For example, to lock the screen orientation to landscape mode, you can use the following code:

OS.set_screen_orientation(OS.SCREEN_ORIENTATION_LANDSCAPE)

Are there any limitations to changing the screen orientation in Godot?

While you can change the screen orientation in Godot using code, it's important to note that not all devices support all screen orientations. Some devices may have limitations on which orientations are available or may not support dynamic orientation changes. It's recommended to test your game on different devices to ensure that the screen orientation behaves as expected.

Back
Top