Skip to reading
PCA Script Programming

36. A complete, reusable interaction pattern

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 PDF page

This example puts the pieces together without hiding a large framework behind them. It reads saved state, checks inventory, prevents a second simultaneous attempt, calls gameplay actions and reports a useful return value.

Code example
global archive_open = false
global archive_busy = false
global score = 0
Code example
function unlock_archive(): boolean
    if archive_open or archive_busy
        return false
    end
    if not has_inventory("key")
        say "The archive key should fit this lock."
        return false
    end
    archive_busy = true
    try
        play_sound({"assetId": "chime", "channel": "puzzle"})
        archive_open = true
        score += 5
        show_message({"text": "Unlocked.", "duration": 1.5})
    catch problem
        warn(problem["message"])
    end
    archive_busy = false
    return archive_open
end
Code example
function run()
    return unlock_archive()
end

Use archive_open to restore the door’s appearance on room entry. Clear transient busy flags deliberately when loading or restarting a puzzle; saving in the middle of an interaction should not leave it locked forever. If an action fails after the fact is set, decide whether the story should stay completed or roll back. That is a design decision, not something try/catch chooses for you.

The later reference chapters are lookup material. Look up the exact function, property key or event phrase you need, then return to this small interaction pattern. Most adventure logic is still a requirement, a state change and a response.

Page 44 of 357Permalink
Go to page