about_expressions

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

Both sides previous revision Previous revision Next revision | Previous revision | ||

about_expressions [2013/12/12 21:10] l [Functions] |
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. | + | |

- | | + | |

- | ==== <<print>> ==== | + | |

- | 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> | + | |

- | | + | |

- | ==== Functions ==== | + | |

- | 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: | + | |

- | | + | |

- | <code> | + | |

- | You have found a pistol! | + | |

- | It's got <<print either(1,2,3,4,5,6)>> bullets. | + | |

- | </code> | + | |

- | | + | |

- | You can also use either() with other macros, such as <<[[set]]>>, to set variables to random values: | + | |

- | | + | |

- | <code> | + | |

- | <<set $playerMoxie to either(2, 4, 6)>> | + | |

- | <<set $playerAttire to either("green", "black", "rainbow")>> | + | |

- | You have <<print $playerMoxie>> moxie points, and <<print $playerAttire>> armour. | + | |

- | </code> | + | |

- | | + | |

- | For a list of the most useful functions, see the [[function]] article. | + |

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