• Godot Help
  • How to change screen orientation with code

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.

I've tried this

func _ready():
    OS.set_screen_orientation(OS.SCREEN_ORIENTATION_LANDSCAPE)

and this
OS.screen_orientation = OS.SCREEN_ORIENTATION_LANDSCAPE

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

These are my project settings.

Does anyone know why it's not working?

Thanks

I changed the tag to the more appropriate "Help" tag. 👍

Megalomaniak

I should have mentioned that I'm using Godot 3.5.2, but I think the commands are the same.

I tested out the commands with the following code:

extends Control

func _ready():
print(OS.get_screen_orientation())

func _on_Button_pressed():
	OS.set_screen_orientation(0)
	var viewport = get_viewport()

	viewport.size = Vector2(1024, 600)
	print(OS.get_screen_orientation())

It prints out "1" and then prints out "0" whenever the button is pressed, but the orientation and viewport doesn't change at all.

Does anyone know what I've done wrong?

    OofStatement I should have mentioned that I'm using Godot 3.5.2

    Indeed, if you are using godot 3.x then it should be in the OS class. And the values seem to be indices of a sort(enum? edit: docs indicate enum yes) so numeric, yeah.

    OofStatement t prints out "1" and then prints out "0" whenever the button is pressed, but the orientation and viewport doesn't change at all.

    I wonder if in case of version 3.5 the application might not need a restart/relaunch perhaps for this to take effect..?

    https://docs.godotengine.org/en/3.6/classes/class_os.html#class-os-method-set-restart-on-exit