Godot Lesson 9: Keys and Locked Doors

Summary

This lesson sets up pickups and allows a player to hold keys and use them to unlock doors.

Prerequisites

  • A player (Lesson 2) and HUD (Lesson 8)

  • Everything will make more sense with Lessons 1 through 8

Video

Code

Repository: https://github.com/los-alamos-steam-lab/godot-tutorial/tree/9-keys-and-locked-doors

Key Code

extends StaticBody2D

# Called when the node enters the scene tree for the first time.
func _ready():
    $area.connect("body_entered", self, "body_entered")

func body_entered(body):
    # I replace body.get(keys) with body.keys because I want this
    # to fail if the player does not have a keys variable
    if body.name == "player" && body.keys > 0:
        # Use a key and then delete the door.
        body.keys -= 1
        queue_free()

Key Door Code

extends StaticBody2D

# Called when the node enters the scene tree for the first time.
func _ready():
    $area.connect("body_entered", self, "body_entered")

func body_entered(body):
    # I replace body.get(keys) with body.keys because I want this
    # to fail if the player does not have a keys variable
    if body.name == "player" && body.keys > 0:
        # Use a key and then delete the door.
        body.keys -= 1
        queue_free()