Invalid operands 'Nil' and 'Vector2' in operator '-'

Godot Version

godot 4

Question

the var direction is always reading seek_player() as Nil

func _physics_process(delta):
	seek_player()
	
	if is_idle == true:
		var direction = (seek_player() - global_position).normalized() # this is the problem child
	# the goal of this is to move the enemy towards the player if is_idle is true. even though the moving part isnt there yet, i am hardstuck on debugging the f***ing direction variable.

	move_and_slide()
	

however, a similar operation was done somewhere else without a problem.

## simple function to teleport minos next to the player
func teleport_to_player():
	global_position = seek_player() + Vector2(randi_range(-20, 20), randi_range(-20, 20)) # here!
	blue_flash.rotation_degrees += randi_range(0, 360) * 1 * randi_range(1, 3)
	blue_flash_2.rotation_degrees += randi_range(0, 360) * -1 * randi_range(1, 3)
	print(self.global_position)

what on god’s green earth is causing this?

seek_player() has returned null, what does the function look like?

it returns target as a Vector2

# checks whether or not the player is within aggro range. if it is, updates target position and returns it.
# target is used for minos's teleportation.
func seek_player():
	var target = playerdetectionzone.player_pos
	return target

should the line be var direction = (seek_player() - self.global_position.normalized())

Also does Seek_player return a V2?

I have something really basic for movement for a enemy that moves towards the player. It goes like this.


look_at(player.position)
		self.position += (Vector2(player.position.x - self.position.x, player.position.y - self.position.y).normalized()) * speed * delta

yes it does. if i print seek_player() like print(seek_player()) it would print out a vector of the player’s current position

i am going to try this. i will update if it works or not

UPDATE:
it seems like the issue was fixed by simply doing the operation in a different function and then calling it in _physics_process.

and as for the suggested alternative:
im not sure whether i implemented it wrong or not, but it didnt work. but thanks for helping!

1 Like

Nice work!!