0 votes

Why?

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?

asked Nov 28, 2019 in Engine by The_Black_Chess_King (167 points)

1 Answer

+1 vote
Best answer

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.

answered Nov 28, 2019 by kidscancode (13,084 points)
selected Nov 28, 2019 by The_Black_Chess_King
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.