scripte error in enemy movement

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

hey every one, I need help for this script it shows no error on compiler but when I run the game there is a error : Invalid operands’Vector2’ and ‘int’ in operator’>='.

here is the script , error line 23:
extends KinematicBody2D
export(float) var move_speed = 100
export(float) var run_speed = 200
export(float) var waypoint_arrived_distance = 10
export(bool) var faces_right = true
export(Array, NodePath) var waypoints
export(int) var starting_waypoint = 0

var waypoint_position
var waypoint_index setget set_waypoint_index
var velocity = Vector2.ZERO

onready var animated_sprite = $AnimatedSprite

func _ready():
self.waypoint_index = starting_waypoint

func _physics_process(delta):
var direction = self.position.direction_to(waypoint_position)
var distance_x = Vector2(self.position.x, 0).direction_to(Vector2(waypoint_position.x, 0))

if(distance_x >= waypoint_arrived_distance):
	var direction_x_sign = sign(direction.x)
	velocity = Vector2(
		move_speed * direction_x_sign,
		min(velocity.y + GameSetting.gravity, GameSetting.terminal_velocity)
	if(direction_x_sign == -1):
		animated_sprite.flip_h = faces_right
	elif(direction_x_sign == 1):
		animated_sprite.flip_h = faces_right
	move_and_slide(velocity, Vector2.UP)
	var num_waypoints = waypoints.size()
	#loop through waypoints
	if(waypoint_index < num_waypoints - 1):
		self.waypoint_index += 1
		waypoint_index = 0

func set_waypoint_index(value):
waypoint_index = value
waypoint_position = get_node(waypoints[value]).position

:bust_in_silhouette: Reply From: jgodfrey

Assuming the error is in the code you posted, it must be here:

if (distance_x >= waypoint_arrived_distance):

You’ve define distance_x as a Vector2 here:

var distance_x = Vector2(self.position.x, 0).direction_to(Vector2(waypoint_position.x, 0))

I can’t tell what data type waypoint_arrived_distance is, but I assume (from the error) that is must be an int.

It’s difficult to suggest what needs to be done to fix it, by you can’t compare an int to a Vector2, as the error suggests. Maybe you only want the x component of that Vector2?

Ah, I do see you’ve defined waypoint_arrived_distance as an int here.

export(float) var waypoint_arrived_distance = 10

Maybe you just want to compare the length of that Vector2 to this value? If so, you want something like:

if (distance_x >= waypoint_arrived_distance.length()):

jgodfrey | 2022-12-17 15:10

thanks man for your help but it did’nt worked

alink | 2022-12-17 15:41

Hmmm… Looks like I messed up the above code (I put the length() call on the int var instead of the Vector2). Try this instead…

if (distance_x.length() >= waypoint_arrived_distance):

jgodfrey | 2022-12-17 15:48

yes thank you very very much

alink | 2022-12-17 16:17