Only the Player
Part of the Signals and Collisions section of Coddy's Godot journey. Lesson 4 of 27.
An area notices every body that comes in. Often only one should count, like a door that opens only for the hero.
Use if to check who it is:
func _on_body_entered(body):
if body.name == "Player":
print("Hello, player")== asks whether two values are equal. The lines under the if, one tab further in, run only when they are. Names must match exactly. "player" with a small p is a different name.
Here the rover rolls over a pressure plate first, then you walk onto it.
Challenge
EasyMake the plate react only to the player.
- In
_on_body_entered(body), put themodulateline inside anif. - Make the
ifask whetherbody.nameis"Player", with==.
Press Play: the plate keeps its color for the rover and turns mint when you step on it. Then Check.
Try it yourself
extends Area2D
func _ready():
body_entered.connect(_on_body_entered)
func _on_body_entered(body):
modulate = Color(0.49, 0.95, 0.75)
All lessons in Signals and Collisions
Practice on your own: Godot playground