Area2D detects body entering and instantly detecting body exiting from it

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

I don’t know if this a bug or something but since today, an Area2D that I use so the character can interact with anything depending on the direction he’s watching has been working weirdly. Today, I just made little changes that don’t affect this Area2D. So here’s the thing, when I want to interact with something it doesn’t work. After testing with prints, I found out that the Area2D detects body_entered and then body_exited instantly after the first one, even does when the body stills on the Area2D collision shape making it unable to interact with it. Is this some bug or I need to check my code?

Edit: Forgot to mention that sometimes that I start the game it works has planned to do and other times does what I mentioned.

Definitely going to need more information haha. wanna show your script for the Area2D’s interaction?

This has only happened to me when I have freed nodes from within an Area2D and it emits the body_exited signal.

scrubswithnosleeves | 2021-02-12 04:49

The Area2D has not coded. But the player does about this and is basically this:

var object = null

func _body_entered(body):
if body.has_method("interact"):
  object = body

func _body_exited(body):
if body.has_method("interact"):
  object = null

Repertix | 2021-02-12 06:08

:bust_in_silhouette: Reply From: bloodsign

I myself has encountered something similar, but I’m not entirely sure if it’s the same situation.

Basically, Let’s look at this node I created:

>Player(KinematicBody2D)
  > CollisionShape2D
  > Sprite
  > Area2D
     >CollisionShape2D

The situation where I encountered this problem was when my Area2D’s collisionshape was overlapping my KinematicBody2D’s collisionshape and they were on the same Physics Layer and Mask. Thus, on initialization, Area2D’s body entered/exited is triggered. But in your case, I’m not entirely sure.

So a quick debug would be:

func _body_entered(body):
  print(body.name)

func _body_exited(body):
  print(body.name)

Which should tell you which body is being detected.

Using prints and if statements to see what’s happening, I can confirm what is happening, when the Area2D enters the CollisionShape2D, the Area2D detects body_entered and instantly body_exiting with the body the no exiting really. I used if statements so the object will not be null until the exiting body is the same as the stored object, so yeah, I think is a bug or something.

Edit: Found out what happened, in the connect function I put “body_entered” instead of “body_exiting”, but that’s not all, I didn’t modify that line since a lot of time ago, so It’s weird that the Area2D worked as intended until now.

Repertix | 2021-02-13 00:06

I have encountered almost same problem, what i have found so far is that when a object is instanciated to a scene area2d triggers enter and exit signal immediately. even though object is not inside the area2d