nesting rooms - leaving a room inside another for the outside world

I have several rooms inside a holding cell room. When the PC leaves one of these rooms in a direction into a different holdingcell room that puts the PC outside of the first holdingcell room, why are they not also "leaving" the first holding cell room?
I want to make a script that reacts to leaving the holdingcell which is a carrier of these other rooms whenever the character leaves the holding cell, which has no exits or entrances of its own.
Why would that not work?


The player (like every object) has an attribute parent, which points to the room they are in.

The script attribute changedparent is run automatically whenever an object's parent changes.

In this case, the player's changedparent runs the exit script for the old value of parent, and the enter scripts and room description for the new value.

Unfortunately, there is no built-in function that keeps track of rooms inside rooms. If you want to do something like this, you could have all of the leave scripts for the individual rooms also check if the player has left the outer room, and run its exit script if appropriate.

Or, if this is something you would use in a lot of places, you could use the roomenter script (on the game object - the one that runs whenever the player changes rooms) to handle it… something like:

if (HasAttribute (game.pov, "previousparents")) {
  foreach (room, game.pov.previousparents) {
    if (HasScript (room, "onexit") and not Contains (room, game.pov)) {
      do (room, "onexit")
    }
  }
}
// and then set up the list of containing rooms for the new room:
game.pov.previousparents = NewObjectList()
room = game.pov.parent
while (not room.parent = null) {
  room = room.parent
  list add (game.pov.previousparents, room)
}

This should make the room's exit script run whenever the player was inside a contained room, and no longer is.


Thats amazing. Thanks. I have one question: Where would I set up the list of containing rooms for the new room?
And, also, I was able to add a script to the entering a room (a parent room of rooms / without an entrance) but not add a script to leaving the room, so I wonder if something unrelated was in the way, instead.


Thats amazing. Thanks. I have one question: Where would I set up the list of containing rooms for the new room?

That code does two things when the player enters a new room:

  1. Looks at the list of containing rooms for the old room, and runs their onexit scripts
  2. Replaces that list with a list of all the rooms that contain the current one.

The comment indicates what the following bit of code is for; it's to make it easier for somebody else to understand if they need to alter it later.


An alternative method, slightly more robust, would be to make flags so that we always know which rooms the player is in. For example, you could make a turnscript that does:

foreach (room, AllRooms()) {
  room.playerhere = Contains (room, game.pov)
}

And then you could give any room a script attribute named changedplayerhere which looks like:

if (this.playerhere) {
  // Put code here to run it when the player enters this room
}
else {
  // Put code here to run it when the player leaves this room
}

This would be a little more flexible in some circumstances. For example, it would even trigger the scripts if the room the player is in moves to a different containing room. (This is probably rare, but I can imagine things like a room representing a caravan or something that an NPC can move, and a script that needs to be triggered when the player enters a new town whether or not the player is actually moving to a different room at the time).


That's excellent! Thank you.


I feel like I read somewhere, I can't remember now. Isn't there a way to do something like game.pov.parent.parent to reference a room's containing room?


I almost feel as if I've seen that, too. I wish I knew where, though. I like to know what I am doing before I mess things up too much. Still, thanks for the hope. I might experiment.


Cloak of Darkness Tutorial!
https://docs.textadventures.co.uk/quest/cloak_of_darkness.html

There's a section doing it for the cloak object. cloak.parent.parent which makes me think room.parent or game.pov.parent.parent should work.

"The Darkness
So now we have to handle the darkness. Note that Quest has a built-in light/dark system, but it is not so useful here, where the darkness will depend on where an object is.
A room is dark if the cloak is in it, and in the bar it is so dark nothing but the exit can be seen. We will be checking if the cloak is present a lot, so it is a good idea to do this with a function.
The CloakHere function
Create a function, call it “CloakHere”, set it to return a Boolean, and paste in this code:
return (cloak.parent = player.parent or cloak.parent.parent = player.parent)
If you are not familiar with Boolean algebra, that might not mean anything to you, so I will quickly break it down. Firstly, is the cloak in the same room as the player? That is, do they have the same parent?
Does cloak.parent = player.parent?
Then we check if either the player or the hook have the cloak, and if the hook does, is it in the same room as the player. We can do all that in one question.
Does cloak.parent.parent = player.parent?
Is either of these is true, then we want the function to return true. So we could do it like this:
if (cloak.parent = player.parent or cloak.parent.parent = player.parent) {
return (true)
}
else {
return (false)
}
But the bit in the if condition is already true or false, so we can just return that.
"


Thank you, Ip Man, that helps me a lot, for future games. The game I asked about this for is published. I just did east, central, and west border sectors separately for each tiny nation.


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums