Godot integer float problem

Godot Version

4.4

Question

I am facing an issue when is am using integer variables i have done everything also typing int after variables and using int function as well but when i assign a integer variable to any other variable may it be integer or float i remains as float and i want it to be integer so i can use the set_cell funciton but unable to as it takes even integer as floats. Need help fast game is upcoming.

HI,

You could just cast the value to an int before or when using it, value being the passed param. A code example would be useful…

  var newInt: int = int(value)

or just use

  int(value)

Regards

Ryn

This is a bit of a confusing question to be honest with you.

set_cell only takes integers (or integer vectors)

set_cell(layer: int, coords: Vector2i, source_id: int = -1, atlas_coords: Vector2i = Vector2i(-1, -1), alternative_tile: int = 0)
layer: int,
coords: Vector2i,  # Integer vector 
source_id: int
atlas_coords: Vector2i
alternative_tile: int

Hmm.

If you try to ‘assign’ an integer variable to a float variable you still get an integer, plus a warning that the decimal part has been discarded. For instance:

extends Node2D

var my_integer: int = 3
var my_float: float = 2.34567

func _ready():
	my_integer = my_float
	print(my_integer)

# Prints 2
# Gives warning:
#Narrowing conversion (float is converted to int and loses precision).

If you do it the other way around, assigning a float variable to an integer variable, you still get a float, except the .00000000… is not displayed of course.

func _ready():
	my_float = my_integer
	print(my_float)

# Prints 3
# You can also print typeof(my_float) and see it is still a float.

An integer variable cast as an integer can NOT be changed to a float. If you share the piece of code that you have that is turning an int into a float, I would be very interested to see it.

You might be possibly re-assigning an already used variable in another scope like this:

extends Node2D

var my_integer: int = 3
var my_float: float = 3.14

func _ready():
	var my_integer = my_float
	print(my_integer)

# Prints 3.14
# Shows warning 
# The local variable "my_integer" is shadowing an already-declared variable at line 3.

In this example, the warning occurs because the local variable my_integer declared inside the _ready() function is shadowing the class-level variable my_integer. Shadowing happens when a local variable has the same name as a variable in an outer scope (in this case, the class-level variable).

Give some more details of your issue and some code and perhaps we can help further.

`extends Node2D

func _ready() → void:
var tid:int = int(1)
print(typeof(1))

func _process(delta: float) → void:
pass`
in this simple script the output window prints 2 which is TYPE_REAL or float and not integer though the variable being integer. Game is about to launch soon and i am not able to fix this.Please help

Hi You’re printing

Typeof(1) 

not

typeof(tid)

Type 2 BTW is an Integer in the variant table:

● TYPE_NIL = 0
Variable is null.
● TYPE_BOOL = 1
Variable is of type bool.
● TYPE_INT = 2
Variable is of type int.
● TYPE_FLOAT = 3
Variable is of type float.
● TYPE_STRING = 4
Variable is of type String.

kindly

Ryn

3 Likes