Area2D detection detects my character without the character being inside

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

I want to code it so that whenever my character enters the “trashboxes” Area2D CollisionArea

**func _on_trashboxes_body_entered(body):**

it checks whether it already is carrying Trash (by checking if the AmountOfTrash Label Node has a spacebar as it’s .text)

**if(AmountOfTrash.text == " "):**

if not then I want it to randomly choose a trash type (out of three) which I have in a list called “trashtype” in my Area2D node called “Trashbox”

	**var trash_num = rng.randf_range(0,2)
	AmountOfTrash.text = Trashbox.trashtype[trash_num]**

and reduce the number of trash the box is holding (max 3)

	**Trashbox.amountTrash += -1**

This function is written in the CharacterBody2D Script with the CharacterBody and the Area2D sharing the parent “map” in the main window and the Area2D node sending the signal “body_entered” to my CharacterBody2D

Now the problem I am having is that
when I launch the game it for some reason it generates a trashtype. I have no TileMap interacting with the Area2D and the CharacterBody2D doesn’t spawn anywhere near the Area2D.
I added a “print” line into my program and it tells me that for some reason my program thinks that the Raccoon-CharacterBody2D is in the Trashbox-Area2D and it triggers the “body_entered” function

Any idea why it triggers the function and thinks the Raccoon is in the Area? If I explained anything in a way that isn’t understandable I am totally willing to give more information on this. I am also new to using Godot so maybe I am using the way that Signals work just completely wrong…

full programm(with print line):

@onready var AmountOfTrash = $/root/Node2D/map/AmountOfTrash
@onready var Trashbox = $/root/Node2D/map/Trashboxes

func _ready():

func _on_trashboxes_body_entered(body):
	print("Raccoon in the area")
	if(AmountOfTrash.text == " "):
		var trash_num = rng.randf_range(0,2)
		AmountOfTrash.text = Trashbox.trashtype[trash_num]
		Trashbox.amountTrash += -1

My Layout:


I’m not sure I follow the scene tree from the description here although the print line will print on any body that enters, including itself. It looks like there are a few colliders in the image and assumed you’ve actually printed body rather than assuming only the raccoon can trigger the signal, so maybe I’ve got that wrong? Perhaps an example project may help as it seems like a configuration issue best viewed in the editor.

Typically, one would leverage godot’s layers and masks to exclude certain bodies/layers from triggering a signal, and maybe secondary check in code to test the body is what is expected. It says latest as your version so also ensure ‘Exclude Parent’ is checked.

spaceyjase | 2023-06-22 20:29

Omg I gave the Boxes a RigidBody as a child of the Area2D to have collisions for it!!! That’s what has been triggering that!! I thought by having the function in the Raccoons Script it would only work for that specific body… Thank you so much!! Gonna look into working with layers and masks as well, thanks for the big help!

Challedy | 2023-06-22 22:48