Login
Remember
IF Answers
Ask a Question

# How can I use an object count to determine a door's destination

0 votes
56 views
asked Jun 16 in Authoring by anonymous
retagged Jun 17 by Alex

I'm writing a text game where a door has several keys. The door may be opened after at least one key is used, but for every additional key used, the door will lead to a different location with no backtracking.

For example.
Person A finds one key, uses it on the door, and then opens the door to Room A

Person B finds two keys, uses them on the door, and then opens the door to Room B

Person C finds three keys, uses them on the door, and then opens the door to Room C.

Note that the number of keys used is the qualifier, not which of the keys or what order they're in.

How would I go about scripting this?
(I was looking into switches, but I'm pretty confused)

- quest

## 1 Answer

0 votes
answered Jun 17 by hegemonkhan (161 points)
edited Jun 17 by hegemonkhan

the 'switch' Script is just a different and arguably easier design method of doing multiple 'if~else if' Scripts (but more limited in functionality):

http://docs.textadventures.co.uk/quest/scripts/if.html
http://docs.textadventures.co.uk/quest/scripts/switch.html

an example (in code, hopefully this doesn't scare you too much):

    <function name="sex_function">
      msg ("Sex?")
      msg ("(1) male or (2) female")
      get input {
        // the quest engine automatically (hidden from you) sets: result = your_typed_in_input
        if (result = "1") {
          player.sex="male"
        } else if (result = "2") {
          player.sex="female"
        } else {
          sex_function
        }
      }
    </function>

vs

    <function name="sex_function">
      msg ("Sex?")
      msg ("(1) male or (2) female")
      get input {
        // the quest engine automatically (hidden from you) sets: result = your_typed_in_input
        switch (result) {
          case ("1") {
            player.sex="male"
          }
          case ("2") {
            player.sex="female"
          }
          // if the person types in anything other than '1' or '2', then you got a problem.
        }
      }
    </function>

as for what you want to do:

the easiest would be using the 'Got' Function:

http://docs.textadventures.co.uk/quest/functions/corelibrary/got.html

    player.parent = room

    if (Got(key1)) {
      player.parent = room2
    } else if (Got(key1) and Got (key2)) {
      player.parent = room3
    } else if (Got (key1) and Got (key2) and Got (key3)) {
      player.parent = room4
    }
    // etc etc etc
    else {
      msg ("You need keys to open up this door.")
    }

otherwise, you got to work with lists:

http://docs.textadventures.co.uk/quest/guides/using_lists.html
http://docs.textadventures.co.uk/quest/functions/corelibrary/scopeinventory.html
http://forum.textadventures.co.uk/viewtopic.php?f=18&t=5137 (my guide on using lists and dictionaries)

using either:

ListCount ( http://docs.textadventures.co.uk/quest/functions/listcount.html )
~OR~
ListContains ( http://docs.textadventures.co.uk/quest/functions/listcontains.html )
// this is actually what the 'got' Function does for you (using ScopeInventory list, anyways).

if you want to use lists, let me know, and I'll help you in using them.

commented Jun 17 by anonymous
Thank you soooo much!
I've pretty much been stuck without being able to cross this, and it's imperative to my game's plot, and making the whole thing worthwhile. Otherwise it would feel empty in winning...
Can't have that! Hahaha
commented Jun 19 by jaynabonne (141 points)
You will need to reverse the order of your "if"s if you go this route, from the more specific to the more general. For example, if "got(key1)" is true, then you'll never get to the later if's to check the "and" versions.
if (Got(key1)) {
player.parent = room2
} else if (Got(key1) and Got (key2)) {
player.parent = room3 <--- never gets here
} else...
So flip them around where you check the "ands" first. To be honest, you might be better off with nested "if"s anyway, to avoid re-checking got(key1) over and over.
If it's just the number of keys that matters, then you could have something more like:
count = 0
if (got(key1)) {
count = count + 1
}
if (got(key2)) {
count = count + 1
}
if (got(key3)) {
count = count + 1
}
etc.
Then you switch on "count" for 1, 2, 3, etc.
commented Jun 19 by anonymous
I actually made it a bit easier on myself and just made the use of the key itself a +1 count, so every time a key is used, the count goes up. Upon using the door, the player will be moved to the room with the corresponding count.
It's probably along the same thing as your final explanation, but the use of the keys is the important part less so than just having them in the inventory. :)

## Related questions

- How do I give an object to a character and receive something back?
- How do I set up an "If" for a container to have multiple objects?
- How can I prevent appending (open), (closed) etc. to an object in Inform 7?
- Quest Gamebook: How to use AND OR and NOT inside an "if" command?
- How can I make use of the health variable in Quest through the editor

...