invalid operand types(Vector2 and "int") to operator "<"

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

I’ve been following a tutorial and i am not sure why my code doesn’t work and i don’t know why the “actor” is an integer value and how do I change this?
also I’m not sure how to insert images as I can’t copy and paste so here is the code:
yes it is a mess I’m planning to separate the different states into their own scripts with managers

also I have no idea why not all the code is going into the format its supposed to on this text box thing

signal state_changed(new_state)

enum State {
PATROL,
ENGAGE
}

onready var player_detection_zone = $Player_detection_zone
onready var patrol_timer = $PatrolTimer

var current_state: int = State.PATROL setget set_state
var actor: KinematicBody2D = null
var player: Player = null
var weapon: Weapon = null

var origin: Vector2 = Vector2.ZERO
var patrol_location: Vector2 = Vector2.ZERO
var patrol_location_reached := false
var actor_velocity: Vector2 = Vector2.ZERO

func _process(delta):
match current_state:
State.PATROL:
if not patrol_location_reached:
actor.move_and_slide(actor_velocity)
if actor.global_position.direction_to(patrol_location) < 5:
patrol_location = true
actor_velocity = Vector2.ZERO
patrol_timer.start()

	State.ENGAGE:
		if player != null and weapon != null:
			var angle_to_player = actor.global_position.direction_to(player.global_position).angle()
			actor.rotation = lerp(actor.rotation, angle_to_player, 0.05)
			if abs(actor.rotation - angle_to_player) < 0.1:
				weapon.shoot()
		else:
			print("in the engage state but no weapon or player")
	_:
		print("Error: found a state for our enemy that should not exist")

func initialize(actor, weapon: Weapon):
self.actor = actor
self.weapon = weapon

func set_state(new_state: int):
if new_state == current_state:
return

if new_state == State.PATROL:
	origin = actor.global_position
	patrol_timer.start()
	patrol_location_reached = true

current_state = new_state
emit_signal("state_changed")

func _on_Player_detection_zone_body_shape_entered(body_rid, body, body_shape_index, local_shape_index):
if body.is_in_group(“player”):
set_state(State.ENGAGE)
player = body

func _on_Player_detection_zone_body_exited(body):
if player and body == player:
set_state(State.PATROL)
player = null

func _on_PatrolTimer_timeout():
var patrol_range = 50
var random_x = rand_range(-patrol_range, patrol_range)
var random_y = rand_range(-patrol_range, patrol_range)
patrol_location = Vector2(random_x, random_y) + origin
patrol_location_reached = false
actor_velocity = actor.global_position.direction_to(patrol_location) * 100

:bust_in_silhouette: Reply From: LeslieS
if actor.globalposition.directionto(patrollocation) < 5:

The function direction_to() returns a Vector2. That is two values ex (6,3).
is (6, 3) < 5 ? Impossible to say since the comparison is invalid. How do you compare a set to a single number?
Its really hard to read the code to give you a fix but if you are checking a single axis you can do something like this:

if actor.globalposition.directionto(patrollocation).x < 5:   # to check the x axis 

or

if actor.globalposition.directionto(patrollocation).y < 5:   # to check the y axis    

Regarding the post formatting here:
The best way to post code in this format is to copy/paste from the editor into the text box then select all the code and press the {} on the toolbar above the text box.
There is a preview window right below the text box to make sure your post and code look correct.
A common problem is editing the code after you have selected and {} it. If you remove the gap line between the non code text and the first line of the code you get problems.

sorry for the wait in response I went on holiday ill post it again with the right format
also If this is the case i have no idea why the tutorial code works??
because i thought about it and yes that does not make any sense how would it be smaller than 5 when the location is a Vector2 i literally have no idea why the tutorial’s code works. i have looked over it again and again. here is the tutorial I’m doing this on so maybe you could see if I’m missing something? https://www.youtube.com/watch?v=Ml2uUKBn3Bg&t=1072s

     extends Node2D
        signal state_changed(new_state)
        enum State {
        	PATROL,
        	ENGAGE
    
    }
    
        onready var player_detection_zone = $Player_detection_zone
        onready var patrol_timer = $PatrolTimer
        var current_state: int = State.PATROL setget set_state
        var actor: KinematicBody2D = null
        var player: Player = null
        var weapon: Weapon = null
        #Patrol State
        var origin: Vector2 = Vector2.ZERO
    
    var patrol_location: Vector2 = Vector2.ZERO
    var patrol_location_reached := false
    var actor_velocity: Vector2 = Vector2.ZERO
    
    
    func _process(delta):
    	match current_state:
    		State.PATROL:
    			if not patrol_location_reached:
    				actor.move_and_slide(actor_velocity)
    				if actor.global_position.direction_to(patrol_location) < 5:
    					patrol_location = true
    					actor_velocity = Vector2.ZERO
    					patrol_timer.start()
    					
    		State.ENGAGE:
    			if player != null and weapon != null:
    				var angle_to_player = actor.global_position.direction_to(player.global_position).angle()
    				actor.rotation = lerp(actor.rotation, angle_to_player, 0.05)
    				if abs(actor.rotation - angle_to_player) < 0.1:
    					weapon.shoot()
    			else:
    				print("in the engage state but no weapon or player")
    		_:
    			print("Error: found a state for our enemy that should not exist")
    
    
    func initialize(actor, weapon: Weapon):
    	self.actor = actor
    	self.weapon = weapon
    
    
        func set_state(new_state: int):
        	if new_state == current_state:
        		return
        
        	if new_state == State.PATROL:
        		origin = actor.global_position
        		patrol_timer.start()
        		patrol_location_reached = true
        
        	current_state = new_state
        	emit_signal("state_changed")
        
        
        func _on_Player_detection_zone_body_shape_entered(body_rid, body, body_shape_index, local_shape_index):
        	if body.is_in_group("player"):
        		set_state(State.ENGAGE)
        		player = body
        
        
        func _on_Player_detection_zone_body_exited(body):
        	if player and body == player:
        		set_state(State.PATROL)
        		player = null
        
        
        func _on_PatrolTimer_timeout():
        	var patrol_range = 50
        	var random_x = rand_range(-patrol_range, patrol_range)
        	var random_y = rand_range(-patrol_range, patrol_range)
        	patrol_location = Vector2(random_x, random_y) + origin
        	patrol_location_reached = false
        	actor_velocity = actor.global_po

sition.direction_to(patrol_location) * 100

LeoJ | 2023-01-13 08:58

In the tutorial he is using distance_to() not direction_to() in the if statement.
Looks like a good tutorial. I am going to do that one at some point.

LeslieS | 2023-01-14 02:41