Good Way to Handle Exit Locking and Unlocking in a Maze? [Solved]

I'm working on a maze where threats appear randomly. When a threat turns up I'd like to prevent the player leaving the location until the threat had been handled. Specifically, I'd like to run a piece of code that locks all exits that are not currently locked and later release just those locks. It feels like there should be a neat way to do this but I don't know it!

Idea 1

Edit: temporarily remove the script, so that moving the player back to the room they came from doesn't trigger it again.

player.changedparent => {
  msg ("The {game.currentthreat} won't let you leave!")
  thisscript = this.changedparent
  this.changedparent = null
  this.parent = oldvalue
  this.changedparent = thisscript

(all objects have the same changedparent script by default; which only does something if the object is the player. So you can just do player.changedparent = some_other_object.changedparent when your threat is dealt with.

Idea 2

Make a command with the same pattern as the go command. Move it into the current room when you want to stop the player moving, and put it in a box somewhere when you don't need it.

Idea 3

Make an attribute to indicate if you can move or not.

Modify either player.changdparent or go.script to check it:

if (HasAttribute (game, "immobilised_reason")) {
  msg (game.immobilised_reason)
else {
  // put the default code here

Then you can set game.immobilised_reason whenever you want to stop the player moving.

**Actually locking the exits**

To lock:

foreach (exit, AllExits()) {
  if (exit.parent = game.pov.parent and not GetBoolean(exit, "locked")) {
    exit.locked = true
    exit.temporary_locked = true

and to unlock them again:

foreach (exit, FilterByAttribute (AllExits(), "temporary_locked", true)) {
  exit.locked = false
  exit.temporary_locked = null

Wow, mrangel, not just the answer I was looking for but a tutorial on other possibilities! The exit locking/unlocking code works perfectly. I also tried Idea 1, as it seems the simplest/best solution, but my game crashed when I tried to leave the room. I'll go with what I can understand! Many thanks as always.

I would say the 3rd one is the best, as it's a single modification that can then be triggered by setting a single attribute. Depends on your game, though.

I fixed the first one; that was me being careless.

Log in to post a reply.