Skip to reading
GDScript Programming & Godot

27. Build a one-time key-and-door interaction

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView illustrations
Code example
func run() -> bool:
    if bool(Variable.door_open.value):
        await ShowMessage({"text": "It is already unlocked.", "duration": 1.5})
        return true
    if not HasInventory("key"):
        await ShowMessage({"text": "I need the archive key.", "duration": 1.5})
        return false
    Variable.door_open.value = true
    await AddScore({"amount": 5})
    await PlaySound({"assetId": "chime", "channel": "puzzle"})
    await ShowMessage({"text": "The archive is unlocked.", "duration": 2})
    return true

Figure 9. The native interaction response calls a script; the object still owns its hotspot, verb and approach settings.

1. Create the key inventory definition, a Boolean door_open variable initially false, and a numeric score variable initially 0.

2. Create the native script unlock_archive. Validate, apply and save it.

3. In the door’s Use response, add Run Visual Script / Run Script and choose unlock_archive.

4. Test without the key, with the key and with a second click. Restore the door’s appearance from door_open whenever the room loads.

Set a one-time flag before long speech or movement so repeated clicks cannot award another reward. If completion can fail after the state changes, decide deliberately whether to keep the fact or roll it back.

Page 36 of 378Permalink
Go to page