0 votes

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.

in Engine by (12 points)

1 Answer

0 votes

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...

by (213 points)
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 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 webmaster@godotengine.org with your username.