The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+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

    #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 (73 points)

2 Answers

+2 votes
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
by (208 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 (47 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 Frequently asked questions and 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 [email protected] with your username.