Unlocking doors?

Skyheart033
29 Jun 2017, 17:01

I'm not sure how to make it so that a player can unlock a door (if they have the key). I have created the key object and the door exit, just not sure how to make them connect.


XanMag
29 Jun 2017, 18:54
  1. Create an exit like you would for any other unlocked exit.
  2. Tick the 'Locked' box.
  3. Give the this Locked exit a name like: doorlock1
    --- option1 --- note - option 1 requires player to type 'use key on door' to unlock the door. You CAN certainly use commands to account for other options the player might try... it just depends on the type of game you are making.
    There are multiple ways to do this, but I find the first the most error-proof (many may disagree with me...)
  4. On the Features tab of the key, tick the 'use/give' box.
  5. Select that tab.
  6. Click the little +Add button by the Use this on (other object) and select handle objects individually and select the door.
  7. For the Scripts, I choose to run an If script. If object 'door' has flag 'unlock1' then print message "The door is already unlocked." In the Else, I printed a "You unlock the door" message, an unlock exit 'doorlock1' script, and a set flag on object 'door' named 'unlock1'. In the look at description for the door, I used an If script to write a proper description for the door IF it had the flag name 'unlock1' and ELSE a message indicating it was still locked. I also copy-pasted the door to my other room so that it can be seen from both rooms (makes sense, right?).
    --- option 2 --- I've always hated containers... but you could...
  8. Do 1-3 at the top - create door and key.
  9. On the features tab of the door, click container and choose the closed container option.
  10. Scroll down to switch lock type to 'lockable'.
  11. Choose '1' for the number of keys to unlock and select the key object for this door.
  12. I left the auto unlock and auto open boxes ticked.
  13. And I ran an unlock exit script in the section 'After unlocking the object' at the very bottom.

For option 2, there are other things you could do like run scripts instead of automatically doing things. All the player has to do with this door is type 'unlock door' and if the player has the key, it will automatically do the rest.

Let me know if this helps.

Below is the code I used if you want to copy-paste it and take a look.

<asl version="550">
  <include ref="English.aslx" />
  <include ref="Core.aslx" />
  <game name="Locking and Unlocking Test">
    <gameid>c3e0e639-ed0d-42a2-92c4-39acd2ceca0a</gameid>
    <version>1.0</version>
    <firstpublished>2017</firstpublished>
  </game>
  <object name="room1">
    <inherit name="editor_room" />
    <object name="player">
      <inherit name="editor_object" />
      <inherit name="editor_player" />
    </object>
    <exit name="doorlock1" alias="south" to="room2">
      <inherit name="southdirection" />
      <locked />
    </exit>
    <object name="door">
      <inherit name="editor_object" />
      <look type="script">
        if (GetBoolean(door, "unlock1")) {
          msg ("The door has been unlocked and you can go south.")
        }
        else {
          msg ("The door is locked and blocks your path.")
        }
      </look>
    </object>
    <object name="key">
      <inherit name="editor_object" />
      <look>It's a key and can be used to unlock doors.</look>
      <feature_usegive />
      <selfuseon type="scriptdictionary">
        <item key="door">
          if (GetBoolean(door, "unlock1")) {
            msg ("The door is already unlocked.")
          }
          else {
            msg ("You use the key to unlock the door.")
            SetObjectFlagOn (door, "unlock1")
            UnlockExit (doorlock1)
          }
        </item>
      </selfuseon>
      <take />
    </object>
  </object>
  <object name="room2">
    <inherit name="editor_room" />
    <exit alias="north" to="room1">
      <inherit name="northdirection" />
    </exit>
    <object name="door1">
      <inherit name="editor_object" />
      <look type="script">
        if (GetBoolean(door, "unlock1")) {
          msg ("The door has been unlocked and you can go north.")
        }
        else {
          msg ("The door is locked and blocks your path.")
        }
      </look>
      <alias>door</alias>
    </object>
    <exit alias="south" to="room3">
      <inherit name="southdirection" />
    </exit>
  </object>
  <object name="room3">
    <inherit name="editor_room" />
    <exit name="doorlock2" alias="south" to="room4">
      <inherit name="southdirection" />
      <locked />
    </exit>
    <object name="key2">
      <inherit name="editor_object" />
      <alias>gold key</alias>
      <look>It's a key.</look>
      <take />
    </object>
    <object name="door2">
      <inherit name="editor_object" />
      <inherit name="container_lockable" />
      <inherit name="container_closed" />
      <alias>door</alias>
      <look type="script">
        if (door2.isopen) {
          msg ("The door is open and you can travel south if you wish.")
        }
        else {
          msg ("The door is locked it appears.")
        }
      </look>
      <feature_container />
      <keycount type="int">1</keycount>
      <key type="object">key2</key>
      <onunlock type="script">
        UnlockExit (doorlock2)
      </onunlock>
      <open />
      <close />
    </object>
  </object>
  <object name="room4">
    <inherit name="editor_room" />
  </object>
</asl>