about_expressions

**This is an old revision of the document!**

Before you learn about the rest of the macros available, you need to talk about a new concept called an **expression**. An expression is a lot like a mathematical formula. When a computer sees an expression, it simplifies it into a single value. This is a very simple expression:

2 + 2

When a computer processes it, it results in the number 4. This process is called **evaluation**. This isn't algebra; everything you start with has to be a known quantity. You can do all the basic mathematical things you'd expect in an expression.

(1 + 2) * 4 + (3 + 2) / 5

This expression evaluates to the number 13. The computer follows the normal order of operations in mathematics: first multiplying and dividing, then adding and subtracting. You can group **subexpressions** together and force them to be evaluated first with parentheses.

You can also use strings in an expression. As noted before, a string is a bunch of characters strung together, demarcated by either double or single quotes. You can use strings in expressions:

"Hello" + " " + "sailor"

This expression pushes the strings together, and evaluates to “Hello sailor”. Notice that a space had to be added between the words; computers aren't smart enough to do that for us. Also, you can only add strings together. You can't subtract them, much less multiply or divide them.

You can print out an expression in a passage using the «print» macro. This, for example, shows the number of rounds in a pistol in a roundabout fashion:

You have found a pistol! It's got <<print 2 * 3>> bullets.

By themselves, expressions are not terribly interesting. The one exception is when you would like to add an element of randomness to your story. You can call a built-in function named `either()`

, which picks one of the values given to it. Using this, you can have a gun with a random number of bullets in it:

You have found a pistol! It's got <<print either(1,2,3,4,5,6)>> bullets.

You can also combine either() with «set» to set variables to random values:

<<set $playerMoxie to either(2, 4, 6)>> <<set $playerAttire to either("green", "black", "rainbow")>> You have <<print $playerMoxie>> moxie points, and <<print $playerAttire>> armour.

For a list of the most useful functions, see the function article.

about_expressions.1386899127.txt.gz · Last modified: 2017/10/09 20:36 (external edit)