Why do get_viewport().size.x and .y return a float?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Beechside

I was just putting a bunch of common routines into a singleton and using static typing to future-proof myself. Without checking, I assumed that the viewport sizes would be returned as type integer but it’s apparently float and I got a “Narrowing conversion” warning.

Colour me surprised…

I can’t think why it has to be a float. Non-integral number of pixels? So many pixels that it overflows the max number that can be held in a 64-bit Integer? Rather unlikely…

If we are to use static typing in the future for performance reasons then I guess I need to understand. Some might call it a bug but one that it might not be possible to fix without breaking backwards compatibility. Wasn’t an issue before static typing. Values would get coerced when changing class but they will now raise errors.

Any thoughts?

It may be the nature of the “size” property in Viewport. The size property uses a Vector2, and its “x” and “y” properties use the “float” data type. When used in a Viewport, common sense would say that they’re integers. Maybe it’s used for backwards compatibility, or maybe it’s an oversight, though I don’t really know.

Ertain | 2019-12-12 16:41

:bust_in_silhouette: Reply From: Xtremezero

I gave the following code a try and found that Vectors are always storing numbers as float :

func _ready():
	var vec= Vector2(int(0),int(5))

the previous code returned 3 (which is a the constant for a float number although I stored the values as Integers

and since get_viewport().size returns a vector , it might be the reason their type is float