Pick Up the Key
Part of the Projects section of Coddy's Godot journey. Lesson 30 of 36.
The key is in the top left corner. It is an Area2D called Key, so it knows when the hero walks into it. main.gd listens:
func _on_key_body_entered(body):
has_key = true
$Key.queue_free()
$HUD/KeyIcon.visible = trueThe game remembers the key in has_key. The key disappears from the floor, and a key icon shows in the top bar.
The hero is the only body that moves, so you do not need to check which body it was.
Challenge
EasyLet the hero pick up the key.
- In
main.gd, addvar has_key = false. - In
_ready, connect$Key.body_enteredto_on_key_body_entered. - There, set
has_keytotrue, remove the key and show$HUD/KeyIcon.
Press Play and walk into the key: it shows in the top bar. Then Check.
Try it yourself
extends Node2D
func _ready():
pass
All lessons in Projects
1Project: Breakout
Breakout PaddleKeep the Paddle on Screen4Project: Dungeon Crawler
Walk the DungeonPick Up the KeyPractice on your own: Godot playground