0 votes

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?

asked Dec 12, 2019 in Engine by Beechside (33 points)

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.

1 Answer

0 votes
Best answer

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))
    print(typeof(vec.x))

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

answered Dec 12, 2019 by Xtremezero (288 points)
selected Dec 13, 2019 by Beechside
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.