#How can I notified about the device orientation?

5 messages · Page 1 of 1 (latest)

sudden grotto
#

Hi, in godot 4 there is a way to detect the change of device orientation whether landscape or portrait.

The idea would be to be notified by a signal.

sudden grotto
#

I was able to solve it with this code, I wanted to get the portrait and landscape resolution in the case of mobile and pc.

thanks @tropic nacelle @graceful wadi

# resolution_detector.gd
#

extends Node


func _ready():
    get_viewport().size_changed.connect(_on_size_changed)
    # The autoload Signals contains "signal viewport_change_orientation(orientation)"
    Signals.viewport_change_orientation.connect(_on_viewport_change_orientation)


func _on_size_changed():
    var size := DisplayServer.window_get_size()
    var orientation := DisplayServer.screen_get_orientation()
    
    # Landscape
    if size.x > size.y:
        match orientation:
            DisplayServer.ScreenOrientation.SCREEN_LANDSCAPE, DisplayServer.ScreenOrientation.SCREEN_REVERSE_LANDSCAPE, DisplayServer.ScreenOrientation.SCREEN_SENSOR_LANDSCAPE:
                Signals.emit_signal("viewport_change_orientation", orientation)
                return
        
        Signals.emit_signal("viewport_change_orientation", DisplayServer.ScreenOrientation.SCREEN_LANDSCAPE) 
    # Portrait
    if size.x < size.y:
        match orientation:
            DisplayServer.ScreenOrientation.SCREEN_PORTRAIT, DisplayServer.ScreenOrientation.SCREEN_REVERSE_PORTRAIT, DisplayServer.ScreenOrientation.SCREEN_SENSOR_PORTRAIT:
                Signals.emit_signal("viewport_change_orientation", orientation)
                return 

        Signals.emit_signal("viewport_change_orientation", DisplayServer.ScreenOrientation.SCREEN_PORTRAIT) 

func _on_viewport_change_orientation(orientation):
    print(orientation)
half pollen
#

Since the problem is solved can you please close this post?

half pollen