+1 vote

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.

in Engine by (69 points)

2 Answers

+1 vote
Best answer

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
by (206 points)
selected by
+5 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
by (46 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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.