# variables, and random outcomes.

first of all sorry if the is just a repost of what someone has already asked i looked around in the forum and didn’t find something that would help, although i didn’t look to long.

but my problem is that i am trying to have a point value or something, that effects the results of a random number between 1 and 10. then based of result then it would do something like this.

if result is greater then 8 then you performed excellently
if result is greater then 4 and less then 9 then your did ok
if result is less than 5 then you have failed

but i don’t know how to have that activate during the scenario, for what i am doing it is supposed to be that your trying to open the lock, if you do excellent you save the key and open the door,if ok then door opens but key is gone, and fail key is gone and door is still locked.

agian, if this is a question already asked maybe just point me in the right direction, and thanks for the help.

The simplest way to do this is with a local variable:

```let X be a random number from 1 to 10; if X > 8 begin; say "The key turns in the lock. Clunk."; now the iron door is unlocked; otherwise if X > 4; say "The key turns in the lock, but then snaps off as you try to remove it."; now the iron door is unlocked; remove iron key from play; otherwise; say "The key snaps in the lock. Who designed this awful puzzle, anyway?"; remove iron key from play; end if;```
(There are two different ways of phrasing if/otherwise statements; this is just the way I prefer.)

thanks, now what would i do if i wanted to make it a little more complicated, lets say by adding a lock picking stat, or what i was thinking is just doing it by the persons lvl or something. how would i change the code to account for the variations, i know it would have to be something like.
x is a random number between 1-10 +[caracter lvl] or something like that. maybe instead of lvl do something with type of keys.

``` let X be (a random number from 1 to 10) plus skill level ```, where skill level is a number that varies which has previously been defined in your code.

having troubles with the key not being removed from play, and that it doesn’t recognize unlocking as an action to start the random outcome.

my code:
instead of unlocking door: <deosn’t work
let x be a random number from 1 to 3;
if x is 1:
say “the key catches and breaks off in the lock. ”;
remove metal key from play;
if x is 2:
say “you unlock the door but the key seems to be stuck, while fiddleing with it for a while the key snaps and breaks in your hands. ”;
now the metal door is unlocked;
remove metal key from play;
if x is 3:
say “the door opens easily, with a slight creaking you push open the door and enter the next room.”;
now the metal door is unlocked.

Here’s the best way I could figure to do this. Note that it’s a little more complicated, but I think this is the “right way” in that it works with the existing unlocking action instead of trying to rewrite all of its logic with a single “instead” rule:

[code]Room A is a room. Room B is a room.

An imposing door is a door. It is east of Room A and west of Room B. It is locked and lockable.

The player carries a small steel key. The steel key unlocks the imposing door.

Outcome is a kind of value. The outcomes are total failure, partial success, and success.

The unlocking action has an outcome called the result of attempt.

Setting action variables for unlocking:
now result of attempt of unlocking action is a random outcome;
say “–> result of attempt = [result of attempt of unlocking action].”

Instead of unlocking a door with a key while the result of attempt of the unlocking action is total failure:
remove the second noun from play;
now the noun is not lockable;
say “[The second noun] catches and breaks off in the lock of [the noun]. Now it is jammed shut!”

After unlocking a door with a key while the result of attempt of the unlocking action is partial success:
remove the second noun from play;
now the noun is not lockable;
say “You unlock [the noun] but [the second noun] seems to be stuck. You fiddle with it back and forth a few times, and [the second noun] snaps and breaks in your hands.”

After unlocking a door with a key while the result of attempt of the unlocking action is success:
now the noun is open;
say “The lock slides open smoothly. You push it open, a slight creaking sound announcing your presence.”

Test me with “unlock door with key / showme door / showme key”.[/code]

Note that an instead rule remains for situations where the door is not actually unlocked; this will generate a failure for the unlocking action. The two after rules cover the cases where it was unlocked but the key breaks or unlocking works without an issue; both of these generate successes for the unlocking action.

while fiddling with it i got it to work by changing -instead of unlocking door- to -before opening door- then the rest of the code worked, key disappears when its supposed to and everything. thanks for the above version though i will look back on it when i become better.

fund a bug,

```before opening door: let x be a random number from 1 to 3; if x is 1: say "the key catches and breaks off in the lock. :("; remove metal key from play; if x is 2: say "you unlock the door but the key seems to be stuck, while fiddleing with it for a while the key snaps and breaks in your hands. :("; now the metal door is unlocked; remove metal key from play; if x is 3: say "the door opens easily, with a slight creaking you push open the door and enter the next room."; now the metal door is unlocked.```

when i did this, if i said unlock metal door, before i said open metal door, it would ignore my rules and unlock the door. then if i opened door i got the the key broke off in the door its still locked message, but was in room 2.

The reason this doesn’t compile is that the name of the action is really ‘unlocking it with’.
This works:

``Instead of unlocking door with: say "Boo-hoo!"``

But be warned that Inform reads this as applying to all doors in general and regardless of what you try to open a door with!
If you want a rule for what happens when the player tries to unlock the metal door with the metal key, you should say so in the rule preamble:

``Instead of unlocking the metal door with the metal key: say "Bah!"``

otistdog’s code sees to it that anything with ‘key’ in its name has a chance to break and get stuck in any lock when you try to unlock any door with it.

Please note that my example above contains a significant bug, as pointed out by zarf (and similarly noted by Felix in this thread). I improperly referenced the “unlocking” action, when the name of the action provided within I7 is “unlocking it with”.

The example only works because a thing is being created called “the unlocking action”. This was not what was intended, though it does function correctly.

Not too difficult to fix, but nonetheless an example of where little details matter.