Connect a Signal
Part of the Signals and Collisions section of Coddy's Godot journey. Lesson 2 of 27.
A signal is a message a node sends when something happens. An Area2D sends body_entered when a body walks in. Nothing happens until some code listens.
To listen, you connect the signal to a function. Do it once, in _ready():
func _ready():
body_entered.connect(_on_body_entered)This reads: when body_entered happens, call _on_body_entered. Godot then calls it every time a body enters.
Write the function name with no brackets. _on_body_entered() would call the function right away.
Challenge
EasyMake the zone notice the player.
- In
_ready(), replacepasswith a line that connectsbody_enteredto_on_body_entered. - Write the function name with no brackets after it.
Press Play and walk into the zone: the Console shows Someone came in!. Then Check.
Try it yourself
extends Area2D
func _ready():
pass
func _on_body_entered(body):
print("Someone came in!")
All lessons in Signals and Collisions
Practice on your own: Godot playground