_on_Area2D_body_entered signal fires when a body is instantiated, not when it enters the area

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

Hi Godot community.

I have something which has me totally stumped.

I have a project in which I instantiate characters which two teams fight one another. The issue is that one type of character uses the standard ‘body entered’ signal on an Area2D node to detect enemies coming into its ‘aggro’ range.

At seemingly random intervals this signal is triggered when an enemy is instantiated into the game; they are not yet in the Area2D range (or are nowhere near it).

I can spawn 10 characters (5 team red, 5 team blue) and anywhere from 1-3 on team red will instantly engage team blue (all of them) as soon as team blue is spawned.

The code can literally be boiled down to:

func _on_Area2D_body_entered(body):
    print(body)

And some characters using this script will instantly detect and print every body in the playing field, and others will just chill out until the body actually gets in range.

I hope someone can help and I hope I’ve given enough info to trigger a lightbulb for someone who has seen similar issues.

:bust_in_silhouette: Reply From: Firty

This happens when you move a body (change the parent of body) that is inside an area2D, for some reason area2D returns a second entry … I fixed this by doing a check like:

 func exit_entered(body,a,b):
    	if $YSort.has_node("Player")&&body==player:
    		Global.spawn = 1
    		Global.inland = false
    		get_parent().toggle_scene(str(a),str(b))

It checks if the player is still in “$Ysort/” and then calls the scene switch…