how to get a callback on orientation change ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By socheat.khauv
:warning: Old Version Published before Godot 3 was released.

Realy Orientation?

splite | 2016-07-26 11:33

In my case just a callback between portrait and landscape rotation should be okay.

It would be nice to have that orientation support.

socheat.khauv | 2016-07-26 12:40

Ah! :smiley: It never occurred to me that you mean screen orientation.

splite | 2016-07-26 12:45

:bust_in_silhouette: Reply From: splite

Just blind shot here, i dont know if screen orientation isnt fixed and i dont have Android nor iPhone to test it out, but i think you can hack it with simple script:

extends Node2D

signal orientation_changed(portrait)
var last_size

func _ready():
	set_process(true)
	last_size = get_viewport().get_rect().size

func _process(delta):
	var size = get_viewport().get_rect().size
	if size.x != last_size.x and size.y != last_size.y:
		emit_signal("orientation_changed", size.x > size.y) # maybe its < ? :)
	last_size = size

save it as (lets say) res://Scripts/Screen.gd.

Fire up project settings, add new AutoLoad (node name Screen, path res://Scripts/Screen.gd)

Connect to signal via

func _on_orientation_changed(portrait):
	if portrait:
		print("portrait mode")
	else:
		print("landscape mode")

func _ready():
	get_node("/root/Screen").connect("orientation_changed", self, "_on_orientation_changed")

Done.


edit: added delta param on _process func…

it would be better if we have signal for it with engine level

volzhs | 2016-07-26 16:59