Why is my 'position' variable being detected as an integer?

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

I’m trying to have a certain thing happen when the player gets close enough to an object:

if position.distance_to(g.playerpos) <= 200:
	sf = true

(‘g’ is my global script’s variable.)

Here’s the error I get:

Invalid type in function 'distance_to' in base 'Vector2'. Cannot convert argument 1 from int to Vector2.

But… position is already a Vector2!

Could somebody help me out here? Thanks.

So, position is Vector2, but what about g.playerpos? Can you show us the code where you defined that in the global script, and any line of code where you may modify it?

p7f | 2020-07-22 01:31

:bust_in_silhouette: Reply From: BrunoFreezee

exactly, position is vector2(), you’re saying
if Vector2(x, y) <= 200… it’s not possible to know if 200 is in x or y axis
you can define position as x or y (position.x <= 200) or position <= Vector2(200, 0)

:bust_in_silhouette: Reply From: whiteshampoo

g.playerpos IS an integer. Can’t tell you why. Not enough code to find the problem.
Somwhere you define playerpos. Do it like this:

var playerpos : Vector2 = ...

Godot will then warn you, if you try to assign an integer to it.

:bust_in_silhouette: Reply From: Magso
Invalid type in function 'distance_to' in base 'Vector2'

This is basically telling you which method the error is in.

Cannot convert argument 1 from int to Vector2.

This means theres a wrong datatype in the brackets so g.playerpos is an int but it needs to be a Vector2.