Menu
CoddyTech

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 = true

The 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 icon

Challenge

Easy

In main.gd:

  1. Add var has_key = false.
  2. In _ready, connect $Key.body_entered to _on_key_body_entered.
  3. In it, set has_key to true, remove the key and show $HUD/KeyIcon.

Try it yourself

extends Node2D

func _ready():
	pass
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Projects

Practice on your own: Godot playground