Template ‐ LOCKED_WITH, LOCKED_DOOR and KEY - ThePix/QuestJS GitHub Wiki

LOCKED_WITH

You can add the LOCKED_WITH template to a container or openable to have it lockable. The template can be give a string or an array of strings - the names of the key objects. If multiple names are given, the object can be locked by any one of them (this is different to Quest 5, which requires the player to have all the keys).

Here is an example of an object that is both openable and locked (but see later for a more comprehensive way to do doors).

createItem("garage_door", OPENABLE(false), LOCKED_WITH("garage_key"), {
  examine: "The door to the garage.",
  loc:"kitchen",
});

You can give the object a custom "testKeys" function if you have unusual requirements for the keys. This should be sent the character doing the unlocking, and optionally a Boolean indicating if the character is trying to lock the object. It should return true if the character has the required keys (or password or whatever).

Here is a simple example that will let anyone lock the door, but it can only be unlocked by a character with the password.

    testKeys:function(char, toLock) {
      if (toLock) return true;
      return char.hasPassword;
    }

Events

When an item is unlocked, if it has an "afterUnlock" attribute, this function will be run. Similarly, "afterLock" will run when locked.

KEY

You can use any object as a key for LOCKED_WITH; all the KEY template really does is give the item an icon in list of items at the side (but does automatically include TAKEABLE).

LOCKED_DOOR

A door can be tricky to implement as it straddles two rooms, and has to control an exit in both room. But not with this simple template!

It needs to be given the name of a key (or array of names), followed by the two rooms it is between.

createItem("garage_door", LOCKED_DOOR("garage_key", "kitchen", "garage"), {
  examine: "The door to the garage is very solid-looking.",
});

Note that you must create the door after both rooms have already been created (i.e., lower down the file) and obviously the two rooms need an exit going to the other. By default, a locked door is scenery.