checking a range of rooms?

Is there anyway I can check that the player is not in a range of rooms? In this case I need to check if the player is not in the Dense Woods which are rooms Dense Woods 1, Dense Woods 2...Dense Woods 6
The only way I know of to do this is with a lot if 'IF' statements so I was wondering if there was an alternative code for this check.


If you give those rooms an attribute to identify them, you can check that. Say you gave all those rooms an attribute named "densewoods" with the value true, then you could do:

if (GetBoolean (game.pov.parent, "densewoods")) {
  // your code
}

Alternatively, you can check if game.pov.parent (the current room) is in a list. The easiest way is probably by getting its name, so that you can check if it's in a stringlist. Like this:

if (ListContains (Split("Dense Woods 1;Dense Woods 2;Dense Woods 3;Dense Woods 4;Dense Woods 5;Dense Woods 6"), game.pov.parent.name)) {
  // your code
}

That method is nice because it lets you check against any list of room names.

But if they all start with the same text, you could do:

if (StartsWith (game.pov.parent.name, "Dense Woods ")) {
  // your code
}

Thanks mrangel.

Good to know there are several options I can chose from.


Oh, there's also switch, which might be more efficient in many cases.

For example, if I want to do one thing when the player is upstairs in a house and another when they're downstairs, I could write:

switch (game.pov.parent) {
  case (Master Bedroom, Alice Bedroom, Spare Room, Bathroom) {
    // do one thing
  }
  case (Lounge, Kitchen, Hallway) {
    // do  the other thing
  }
  case (Basement) {
    // another thing
  }
  default {
    // the player isn't in the house
  }
}

Alternatively, you could put all of the rooms inside another room which the player never sees. Then you can just do:

if (Contains (House, game.pov)) {
  // your code
}

This is probably the easiest to write, as you can move the rooms around using the GUI. However, it doesn't work if you have non-geographic lists of rooms (for example "haunted rooms" or "dark rooms"), because it doesn't work when a room can be in 2 different lists for different situations.


Hi mrangel
I have not used 'switch' in my code but I can see how useful it can be.


Log in to post a reply.

Support

Forums