How to make the exit unlock

ZodiacStories
17 Apr 2020, 17:41Hello again, I have an exit that is locked, so what commanded to I have to use to make my key unlock it? Or is there a way I can trigger it to unlock when a certain item is picked up
mrangel
17 Apr 2020, 19:54In any script, you can unlock an exit by doing something like:
name_of_exit.locked = false
Note that you have to give an exit a name before that will work.
It is also possible to give the exit an "unlock" verb; but "lock" and "unlock" are designed for containers, it requires a little extra work.
First, you'd need to make the unlock
verb work on exits as well as objects, by giving it script attribute named changecommandscope
containing the script:
foreach (exit, ScopeExits()) {
list add (items, exit)
}
Then you'd want to give the exit attributes like autounlock
, keycount
, key
, lockmessage
, and so on. I'm not sure, but I think that making the exit inherit container_lockable
might add the tab to edit these values in the GUI.

ZodiacStories
17 Apr 2020, 20:04Thank you, but do you know of a video that I could watch as well?
XanMag
17 Apr 2020, 20:06Not sure if this would be that helpful but I pulled this from my template and tutorial "game". Ask if you have questions! And I ASSUME you are using the downloaded version.
- Whenever you want to unlock an exit, you need to choose an 'unlock exit' script from the pop-up menu and select it. It is that simple. So, for moost of these doors, I added an 'unlock exit' script when the key was used on the door. If you go to the 'use/give' tab of the key, you'll see the script.
- Be sure to add a default which is especially important if you have more than one key in your game.
- Usually when I unlock a door or gate, I like to also add a flag to that barrier. I just added a flag named 'unlocked' after each barrier was effectively unlocked.
- I then added 'IF' descriptions to each barrier. 'If' object old door has flag name 'unlocked' I printed a message indicating the door has already been unlocked. If you want a good game, be sure to do this with all locks. It seems kind of silly to unlock a door and the description indicates that it is still locked!
- The locking button - I simply reversed everything I did above. I locked all exits in these four rooms and unset all flags. I did this just so you can see that you can also lock exits with certain events!
- The triangular door - this lock I dealt with a little differently. If the player tries to move in the direction of the door and has possession of the key, I just let them pass. This is nice if you want save the player from typing in use key b on door, etc, etc.
- To do this, I simply used an 'IF' script. If object player is carrying object triangular key, then print a message that acknowledges that the player has teh key. Also, add a move object script to move player to desired room. For the 'Else' part of this script, I printed a message telling the player they do not have the proper key to access this area.

ZodiacStories
17 Apr 2020, 23:07So, I want to have exit unlock once the player has a book in their inventory. I have the script for the exit that says "If a player carries object" then the exit is to unlock but nothing happens.
XanMag
17 Apr 2020, 23:55On the book, try ticking script when object is picked up. Add to that script:
- Add object book to inventory.
- Make exit [name] visible
- Unlock exit [name]
mrangel
18 Apr 2020, 10:02So, I want to have exit unlock once the player has a book in their inventory. I have the script for the exit that says "If a player carries object" then the exit is to unlock but nothing happens.
The script on an exit runs only if the exit is unlocked, instead of going through it.
So you'd want to make the exit not locked, but give it a script like:
if (Got (book)) {
MovePlayer (this.to)
}
else {
msg ("You push at the door, but it doesn't open")
}
(changing book to the name of the book object)

ZodiacStories
18 Apr 2020, 12:38One last thing, all of it works, but the player can't get to the next room. I type in "go outside," and the message plays about leaving the first room, but the player is still in the first room.
XanMag
18 Apr 2020, 15:14Hm. A simple solution would be to add a script:
- move object player to room 2
when they type "go outside"
mrangel
18 Apr 2020, 15:15One last thing, all of it works, but the player can't get to the next room. I type in "go outside," and the message plays about leaving the first room, but the player is still in the first room.
Did you use the method I suggested, or the method XanMag suggested?
If mine, did you remember to include the MovePlayer
line?
If you're still having problems, it might be worth including the script that you're using (code view is good for copying and pasting excerpts here, even if you prefer to edit in the GUI); or a link to the game so we can see the problem.

ZodiacStories
18 Apr 2020, 16:21That was it!! thank you both

ZodiacStories
18 Apr 2020, 22:40I do have one more question. is there a way to make it where the exit can only be unlocked when you have three certain objects in your inventory?
XanMag
18 Apr 2020, 22:57The following is an example from my tutorial that I use. I used puzzle pieces and a jigsaw puzzle. Use what makes sense to you.
explain room
This is a frequent problem I see on the forums and one that I struggled with for the longest time. Once I figured this out, it is most likely the most helpful bit of coding I have learned. It really opens up gaming options! So, I will try to make this simple.
You will use something like this whenever you want multiple things to get accomplished before it triggers a new event or singular accomplishment. Want to have multiple keys used to open a door? Or, multiple pieces of a password collected before punching in a code? You want to wear multiple different pieces of clothing before you can sneak in to a top secret lab?? This is your answer.
- You will need to add an attribute to an object. (found in the bottom half of the attribute tab of whatever object you want to set this on). Make it an integer attribute and set the integer equal to the number of sub-events (In your case, 3). In this room, I added the attribute to the jigsaw puzzle. You need to use four puzzle pieces to complete the puzzle, so I set the integer to 4.
- To do so, click on the object, click on the attribute tab. At the bottom box, click on 'Add'. I always name mine [something]Count. In this case, I named it 'MissingPieceCount'. (You could name it HeldKeysCount)
3a. Next, click on the 'Add change script'. Under this, I added an 'If' script. Select the 'If object attribute equals' from the drop down.
3b. Select the object the attribute is set on. In this case, it was the jigsaw puzzle. (Yours May be the door name?)
3c. Type in the title of your attribute. Here it was 'MissingPieceCount' and set it to value zero (0).
3d. For the then, I printed a 'Congratulations, the puzzle is complete' message and set a flag titled 'Complete' on the puzzle. I did this so I could look at the puzzle and get a different response for a complete puzzle and one that wasn't complete.
4a. Wherever the subevent is completed... in this case it was use puzzle piece with puzzle.... you need to adjust the integer variable on the object in question.
4b. To do this, I chose the 'set a variable or attribute' script under the heading 'Variables'.
4c. You need to type into that first box the following: object.attribute - In the second box you need to leave it as 'expression'. In the third box you need to adjust the variable by typing the following: object.attribute - 1
4d. So, in my example, it looks like this: Set variable: jigsaw puzzle.MissingPieceCount = expression jigsaw puzzle.MissingPieceCount -1 - Now, back over to the changedMissingPieceCount script if you choose. I didn't, but you could have added a bunch of Else Ifs to the changedMissingPieceCount attribute. Else If, object has attribute equal to 4 (or 3, or 2, or 1) print a proper message. If you choose to do this, each time a sub-event is comleted, you can send a message to the player that there are three (or two, or one) more events needed to complete it.
- Optional: In the look at description, you can add a switch or a bunch of If/Else Ifs to describe the object (puzzle) that is changing.
I know this is long winded and I hope it makes sense. You just have to change the name of your objects to whatever you named them and change the name of your attribute to whatever you named it.
Ask if you have questions!
mrangel
19 Apr 2020, 08:58is there a way to make it where the exit can only be unlocked when you have three certain objects in your inventory?
You could modify the existing script:
if (Got (bell) and Got (book) and Got (candle)) {
msg ("You wave three items at the door and it opens!")
MovePlayer (this.to)
}
else {
msg ("You push at the door, but it doesn't open")
}
Or if you want to make the messages give more of a clue:
if (not Got (bell)) {
msg ("You don't feel comfortable leaving your bell behind.")
}
else if (not Got (book)) {
msg ("You haven't got anything to read yet.")
}
else if (not Got (candle)) {
msg ("It's too dark to go out without a light source.")
}
else {
msg ("You wave three items at the door and it opens!")
MovePlayer (this.to)
}
If you want the exit to 'unlock' so that the player doesn't need the items once they've been through once, you can add the line:
this.script = null
before the MovePlayer
line; so that it removes the script and turns the exit into a normal one.

ZodiacStories
19 Apr 2020, 13:18mrangel, how do I modify the existing script? is it expression? or is it the code?