0 votes

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 movespeed = 100
export(float) var run
speed = 200
export(float) var waypointarriveddistance = 10
export(bool) var facesright = true
export(Array, NodePath) var waypoints
export(int) var starting
waypoint = 0

var waypointposition
var waypoint
index setget setwaypointindex
var velocity = Vector2.ZERO

onready var animated_sprite = $AnimatedSprite

func ready():
index = starting_waypoint

func physicsprocess(delta):
var direction = self.position.directionto(waypointposition)
var distancex = Vector2(self.position.x, 0).directionto(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 setwaypointindex(value):
waypointindex = value
position = get_node(waypoints[value]).position

Godot version 3.5
in Engine by (15 points)

1 Answer

+1 vote
Best answer

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?

by (21,662 points)
selected by

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()):

thanks man for your help but it did'nt worked

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):

yes thank you very very much

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.