Why Area2D with just body_entered() in the script won't register anything?

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


If I write just the code, in my Area2D node:

func body_entered(body): #Also tried _body_entered():
      print("Something entered me.")

The thing won’t register the print, but if I go to the area2D signals in the inspector, and wire the body_entered() signal to the same func, it registers the print…

I don’t want to always have to wire my nodes manually, I can do with code! Why doesn’t just body_entered() work?

:bust_in_silhouette: Reply From: kidscancode

body_entered is a signal emitted by the node, it’s not a function, so just putting a function with that name isn’t going to accomplish anything.

Signals are used by connecting them to functions. These functions can reside on the emitting node or on any other node in the tree. When a signal is emitted, the connected function receives it. There may even be multiple nodes subscribed to a given node’s signal

To connect a signal, you can either in the Inspector, as you’ve done, or you can do it in code:

extends Area2D

func _ready():
    connect("body_entered", self, "_on_body_entered")

func _on_body_entered(body):
    print("Something entered me.")

For more information, see Signals in the Step-by-step docs.