Leaving an Area
Part of the Signals and Collisions section of Coddy's Godot journey. Lesson 5 of 27.
An area sends a second signal when a body leaves: body_exited. Its function also gets the body.
Connect each signal on its own line, to its own function:
func _ready():
body_entered.connect(_on_body_entered)
body_exited.connect(_on_body_exited)
func _on_body_exited(body):
print(body.name, " left")With both signals you know when someone is inside: from body_entered until body_exited.
Challenge
EasyThe lamp turns on when you walk into the room. Make it turn off when you walk out:
- Write a function
_on_body_exited(body)that hides$Lamp. - Connect
body_exitedto it in_ready().
Try it yourself
extends Area2D
func _ready():
body_entered.connect(_on_body_entered)
func _on_body_entered(body):
$Lamp.visible = true
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Signals and Collisions
Practice on your own: Godot playground