NPC AI interferes with duplicates of the same NPC

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

My AI uses a system with an child Area2D and a circular collision shape to detect whether the player is near the NPC. If this happens, the a boolean variable in the Area2D script changes to “true”, and the NPC heads in the opposite direction from the player. Also, the collision circle’s radius is doubled as a countermeasure, so that the NPC doesn’t escape the player and immediately turn back around towards them. Once the player is out of range, its radius returns to normal. This is where the problem arises. When there is more than one of the NPC in the world, only the original NPC’s circular collision shapes seems to be affected (doubling the radius) by the player entering it. Also, this seems to double the radius of all other of the same NPCs simultaneously, while other NPCs Area2Ds do not change size if entered, even though they are exactly the same.

Sorry this was so wordy, but it’s a really strange problem that really confuses a newbie to Godot like me. I would provide the code, but there is a lot to it, so I thought it would be more practical to just explain.

Without seeing any code, my guess is that you’ve used the editor to connect a signal, which is only connected to the one NPC node

Eric Ellingson | 2019-08-23 05:16

if $Bubble.get("invaded") == false:
	direction = destination - self.position
	$Bubble/CollisionShape2D.shape.radius = 80
	direction = self.position - get_node("/root/World/Player").position
	$Bubble/CollisionShape2D.shape.radius = 160

This is the code for the parent NPC that changes the size of the collision shape of the “bubble”, which is the Area2D that detects whether the player is in it.

func _physics_process(delta):
#If player is detected inside of this area, send signal to flee
var bodies = get_overlapping_bodies()
invaded = false
for body in bodies:
	if body.get_collision_mask_bit(0) == true:
		invaded = true

This is the script for the “Bubble”, detecting if the player is inside of it and changing the variable invaded to true accordingly.

Sn8 | 2019-08-23 19:03

:bust_in_silhouette: Reply From: Sn8

I figured it out. I was setting the radius of the same circle shape file that every single NPC used. Instead, I used $Bubble/CollisionShape2D.set_scale(Vector2(2, 2)) and this seemed to work for each NPC independently.