User Tools

Site Tools


This is an old revision of the document!

About Expressions

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 Math.random(), which evaluates to a random decimal between 0 and 1. (More documentation on the Math object, which includes basic mathematical functions like sine and absolute values, can be found here.) Combine this with Math.round(), which rounds a number to the nearest integer, and you can have a gun with a random number of bullets in it:

:: You have found a pistol
It's got <<print Math.round(Math.random() * 6)>> bullets.

You can try working with expressions below. Clicking the button will evaluate what you enter. If it doesn't seem to react, then you've made a mistake in your expression – for example, forgetting to put quotation marks around a string.


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