Global Before Leaving a room Script

Is there a way to have a script fire right before anyone leaves a room, I'm trying to get a patrol NPCS that will interact with the player if they leave before the NPC does(Ie something like running from police)?

There is a "Before entering the room", but that would be connected to the next room.
You could change the exit so that it runs a script instead of moving the player, then in that script, use that for the NPCs to interact with the player.
Just use an "if" to let the player move normally, or stop the player from moving.
The you will need to add a MoveObject (player, next_room) to move the player.

Hmm that doesn't cover my problem really, I'd like it to work on any room, I suppose i could attach it to all the rooms in the game, but when thats around 200 or so atm, it feels really "Wrong" from a technical aspect

Then, it sounds like what you want is a function...
But what calls the function would be the question.
There is a tab called "Scripts"
There is a Start script:
Script when entering a room:
and a Turn script:
You may want the turn script or the Script when entering a room
and put your code there...
Those sound like a global script things that you are looking for.

You could call a function from the script that moves the NPCs. (I assume they don't move around on their own. Ha-ha!)

If you post a sample of that script, we can most assuredly help you out.


You could make a Boolean attribute for each NPC that should be in the room with the player during said scene. Call it gangmember and set it to true.

You can enable this turn script or disable it, whenever you please:


foreach (npc, AllObjects()) {
  if (HasAttribute(npc, "gangmember")) {
    if (npc.gangmember) {
      if (not npc.parent = player.parent) {
        msg (GetDisplayName(npc) + " just made a break for it!")
        npc.gangmember = false

You are in a room.
You can see npc1 and npc2.

> z
Time passes.

> z
Time passes.

> z
Time passes.

> z
Time passes.
npc2 just made a break for it!

> z
Time passes.

Are you wanting this to fire when the player goes to a new room or an NPC?

If the player, then go to the Scripts tab of the game object. There is a script there that runs whenever the player enters a new room.

Oh, whoops! Did I misread that? (Sorry about that!)

For the player:

This script will add whatever script you put in place of msg ("IT WORKS!") to the After exit script on every room in the game:


foreach (o, AllObjects()) {
  foreach (r, ScopeExitsForRoom(o)) {
    o.enter => {
      msg ("Enter a script or message here! (scopeExits)")

If you want it set to run after entering, substitute o.onexit with o.enter.

If you want it set to run before entering, substitute o.onexit with o.beforeenter.

NOTE: Before enter - prints the message before printing the room description, and after prints it after (of course). Point being, neither print the message before you actually leave the room.

I can't find a Before exit anywhere, but you can put this in the start script (instead of anything else), and it would make the player press any key between printing your message and actually moving the player to any room while game.copsAreComing = true.

If you don't have a Boolean attribute set on the game with the name copsAreComing set to true, it would move the player as it normally would.

exitScript => {
  game.this = this
  if (HasAttribute(game, "copsAreComing")) {
    if (game.copsAreComing) {
      msg ("You have left while... whatever is going on is going on.<br><br>PRESS ANY KEY TO CONTINUE.")
      wait {
        MoveObject (player,
  else {
    MoveObject (player,
foreach (o, AllExits()) {
  o.runscript = true
  o.script = exitScript

You should not use the various editor types, such as editor_room, in your game, as Quest will strip them out when you publish. They are there purely tolet Quest know which tabs to display. Best way to check if an object is a room is probably to see if it has any exits or to check if it has a description attribute.

The first code block you game makes me wonder; the documentation says that the editor_room type is only used within the editor, and is removed when the game is published. So would publishing stop that script from working? That's what I assumed when I was previously looking for a way to distinguish rooms from other objects.


I wish I could see those posts' times, down to the second!


The whole editor_ part should have been a dead giveaway, huh?

I'm on it, though! Taking the check-for-exit route, just in case a description doesn't exist.

I'll be back to edit things...

I edited that bit.

(I'd have NEVER figured out why that disappeared on my own!)

I have a very similar requirement.. If I'm in a fight, I want the NPC enemies to get added to a list of monsters which are "following" the player... Then on a timer, the monsters move "towards" the player... I do this by keeping track of the rooms the player's in, and adding it to the "follow" list of any monsters that are following the player...

OK, so to do it, I thought about one of two ways:

  1. Create a "gameroom" base type, and ensure that all rooms are inherited from it.. In this "gameroom", I set the onexit to do my bidding... which will be inherited by all rooms.

  2. Create a game script attribute (called globalroomexitscript) with my logic, then create another script which runs right at the beginning of the game which goes

foreach(room, AllRooms()) {
room.onexit = game.globalroomexitscript
or something similar....

In the end I went with the inheritance route, because that fit better with how I was controlling everything else :)

Hope that helps!

You could also modify the player.changedparent script attribute, which is run whenever the player moves to another room.

By default, it is set to:

      if (game.pov = this) {
        if (IsDefined("oldvalue")) {
        else {
        if (game.gridmap) {
      this.hasbeenmoved = true

You could override that and add your own functionality there.

(The function OnEnterRoom(oldroom) is responsible for calling the onexit script of the old room, and the onenter script for the new one, and printing the new room's description)

Those were some big help KV..and i learned alot of what the start script could do. Initially I thought it fixed up everything..however noticed some odd bugs, then i realized what the code was doing, changing the "Before enter" scripts i had already placed in various rooms as well(for random flavor texts dedicated to the room)

Sorry about being so slow to reply, was trying to figure out why these bugs were happening on my own first,

edit- though far from perfect, managed to get something working by having the timer add a player flag when the officer enters their room, and removes it when they leave(just checking parents), mixing this with a turnscript on the game that checks for the presence, if its there the script fires.. it won't work if the player stumbles on the officer mid path, but pretty functional otherwise

Log in to post a reply.