Pick Up the Key
Part of the Projects section of Coddy's Godot journey. Lesson 27 of 33.
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
EasyIn main.gd:
- Add
var has_key = false. - In
_ready, connect$Key.body_enteredto_on_key_body_entered. - In it, set
has_keytotrue, remove the key and show$HUD/KeyIcon.
Try it yourself
extends Node2D
func _ready():
pass
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
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