User Tools

Site Tools


about_expressions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
about_expressions [2013/11/13 00:18]
klembot created
about_expressions [2017/10/09 20:39] (current)
Line 1: Line 1:
-===== About Expressions ===== +#​redirect ​expression
- +
-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:​ +
- +
-<​code>​ +
-2 + 2 +
-</​code>​ +
- +
-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. +
- +
-<​code>​ +
-(1 + 2) * 4 + (3 + 2) / 5 +
-</​code>​ +
- +
-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:​ +
- +
-<​code>​ +
-"​Hello"​ + " " + "​sailor"​ +
-</​code>​ +
- +
-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: +
- +
-<​code>​ +
-:: You have found a pistol +
-It's got <<​print 2 * 3>> bullets. +
-</​code>​ +
- +
-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 [[https://​developer.mozilla.org/​en-US/​docs/​Web/​JavaScript/​Reference/​Global_Objects/​Math|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: +
- +
-<​code>​ +
-:: You have found a pistol +
-It's got <<​print Math.round(Math.random() * 6)>> bullets. +
-</​code>​ +
- +
-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. +
- +
-FIXME+
about_expressions.1384319929.txt.gz · Last modified: 2017/10/09 20:36 (external edit)