Area2D doesn't appear to be working.

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

I’m trying to create an enemy with a Area2D node. The idea is that when the player, which is a KinematicBody2D node, intersects the Area2D, the Area2D will send a signal to its parent, the enemy. However, whenever the player and enemy intersect, nothing happens. Does it have to do with the fact that both bodies are moving? I have tried using the body_enter signal, and even gave the player its own Area2D and used the area_enter signal. Neither method appears to work.

The Area2D node is connected to the enemy node by the following line of code in the _ready() method:

$Area2D.connect("area_entered", self, "_on_body_entered")

And the signal connects to the following method:

func _on_body_entered():

However, as evidenced by no output being printed to the console, this method is never called.

I tried placing the following code directly into the enemy’s Area2D, but still no results:

extends Area2D

func _ready():
    connect("area_entered", self, "_on_area_entered")

func _on_area_entered():

I also tried the above with body_entered.

Here are the collisions:

And here are the node trees:

You need to share more details of what you’ve tried and how you have things set up. By your description, that is the way it should work, so it’s likely you’re doing something wrong. body_entered is the correct signal. A couple of things to try:

  • Have you attached a CollisionShape2D to the Area2D? It won’t work unless it has a shape defined.
  • Did you connect the signal in the Inspector, or via code? If the latter, please share.
  • How are you checking to see if it works?

kidscancode | 2019-08-01 23:41

:bust_in_silhouette: Reply From: Othman2019
  1. try to have the same collision layer.
  2. if the area2d is a child of the enemy there is no need to have that connect method in ready function. Just send body_entered signal to the enemy. your signal function returns nothing. when you do it the way I mentioned it should have (body) parameter, that you should use to do your actions.

As it turns out, the problem was caused because the connected method did not accept any arguments. I assumed I could ignore this, as I didn’t necessarily need to know what was intersecting the Area2D, but I now realize that the method won’t work unless I provide arguments.

Thank you for your help.

Turnovus | 2019-08-02 01:45

:bust_in_silhouette: Reply From: st_phan

Because I stumbled upon this myself while searching “receiving method not being triggered by signal”:

As one comment mentions it might be due to a mismatch of arguments between signal and receiver method
Example: The area_entered-signal passes an area2D, but a custom function like _on_area_entered() has no arguments

When using the “Signals”-tab and you click “connect…”, then there is a toggle “Advanced” where you can “Unbind signal arguments” which did the trick for me

Screenshot of the edit signal modal