Invalid operands 'int' and 'float' in operator '%'

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

I have two variables:

# source of variable - AStar2D id (type: int)
var point = 604
# Vector2 of two ints
var map_size = Vector2(100, 100) 

When I try

return point % map_size.x

I get this error (Invalid operands ‘int’ and ‘float’ in operator ‘%’)

:bust_in_silhouette: Reply From: Zylann

Vector2 uses float numbers. x and y are float. Using ints to make one does not determine its type.

% only works on integers.
So if you want to use this operator you must cast to integer:

return point % int(map_size.x)

note if the map_size is not exactly integer sized this will simply cut off anything after the decimal point. which is probably what you want anyway but something to keep in mind.

nonchip | 2020-04-24 15:29

Well this is unexpected, Should’ve checked docs.
I hoped duck typing did all the job for me :stuck_out_tongue: Thank you!

Korinin | 2020-04-25 08:21