0 votes

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

    #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.

asked Sep 16, 2017 in Engine by VictorSeven (67 points)

2 Answers

+1 vote
Best answer

This is because the device has a taskbar.

    view_width = get_viewport().get_visible_rect().size.width
view_height = get_viewport().get_visible_rect().size.height
answered Sep 16, 2017 by VitaZheltyakov (206 points)
selected Sep 17, 2017 by VictorSeven
+4 votes

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
answered Mar 2, 2018 by victordevtb (44 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.