Who Walked In?
Part of the Signals and Collisions section of Coddy's Godot journey. Lesson 3 of 27.
The area passes the body that came in to your function. That is the body in _on_body_entered(body). It is the node itself, so you can read anything on it.
body.name is the name of the body that walked in:
func _on_body_entered(body):
print(body.name)A plain name is the zone's own name, because the script is on the zone.
Two bodies cross this zone: you, the Player, and an amber Rover that rolls through on its own.
Challenge
EasyMake the zone say who walked in.
- In
_on_body_entered(body), change theprint()so it prints two values:body.nameand" entered the zone". - Put a comma between them.
Press Play: the Console shows Rover entered the zone, then Player entered the zone when you walk in. Then Check.
Try it yourself
extends Area2D
func _ready():
body_entered.connect(_on_body_entered)
func _on_body_entered(body):
print("Something entered the zone")
All lessons in Signals and Collisions
Practice on your own: Godot playground