Menu
CoddyTech

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

Let the hero pick up the key.

  1. In main.gd, add var has_key = false.
  2. In _ready, connect $Key.body_entered to _on_key_body_entered.
  3. There, set has_key to true, 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

Practice on your own: Godot playground