Resolution vs Screen Size

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

I want my project to handle different resolutions, for several devices. There are a lot of questions asking this out there, so I was able to setup a code to do it:

extends Control

var defH = 600
var defW = 1024

func _ready():
	#Change size of parent control to that of the complete viewport
	#Using this MARGINS will work. Also, no need to resize everything, only options
	set_size(get_tree().get_root().get_rect().size) 
	
	#Get the scale
	var newH = get_size().y
	var scale = newH / defH
	
	var options = get_node("options") #Get the buttons to resize
	
	options.set_scale(scale * options.get_scale()) #Scale to new resolution
	#Scale the margin so it keeps the proportions
		
	options.set_margin(MARGIN_LEFT, options.get_margin(MARGIN_LEFT) * scale)
	options.set_margin(MARGIN_TOP, options.get_margin(MARGIN_TOP) * scale)

This works pretty well when I go to Project Settings > Display and I change width and height. My buttons are re-scaled and re-positioned correctly.

However, when I export the app to Android, they are not correctly positioned. The scale works well though. However, is too much space between the right side of the screen and the buttons. My phone has a resolution of 800x480 px (in landscape) and screen size of 4.0’'.

I set resolution of 800x480 in PC and everything works well. I wonder why it is not the same in Android. My best bet is that a device with the same screen size as mine can have a different resolution, and I should take that in account when repositioning. However, I don’t know how to handle this. Any ideas? Thank you.

:bust_in_silhouette: Reply From: VitaZheltyakov

This is because the device has a taskbar.
Use:

	view_width = get_viewport().get_visible_rect().size.width
view_height = get_viewport().get_visible_rect().size.height
:bust_in_silhouette: Reply From: victordevtb

In Godot 3, the righ way to do this is:

var screenSize = Vector2(0,0)
screenSize.x = get_viewport().get_visible_rect().size.x # Get Width
screenSize.y = get_viewport().get_visible_rect().size.y # Get Height

Or, more simply, you could just do:

onready var screenSize = get_viewport().get_visible_rect().size

since get_viewport().get_visible_rect().size already returns a Vector2.
Just remember you can’t get viewport size before _ready.

Necco | 2018-07-19 16:09