This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
function [2014/06/16 00:05] l |
function [2017/10/10 00:39] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | <- [[expression|About Expressions]] --------- [[print|Printing Variables and Functions]] -> | ||
+ | |||
===== Functions ===== | ===== Functions ===== | ||
When writing code in your stories, functions are special devices that can be used in [[about_expressions|expressions]] in place of variables or values. | When writing code in your stories, functions are special devices that can be used in [[about_expressions|expressions]] in place of variables or values. | ||
- | Syntactically, they consist of: a name, which is subject to the same rules that [[variable]] names are under, followed by a ''('' left bracket, followed by zero or more other expressions separated by commas, followed by a '')'' right bracket. | + | Syntactically, they consist of: |
+ | * a name, which is subject to the same rules that [[variable]] names are under, | ||
+ | * followed by a ''('' left bracket, | ||
+ | * followed by zero or more other [[expression]]s separated by commas, | ||
+ | * followed by a '')'' right bracket. | ||
- | Technically speaking, any Javascript built-in function is available to the Twine author: parseInt(), JSON.stringify(), history.replaceState() and all the rest are accessible, as are any functions that have been added by [[script]]s. However, you should find the most commonly useful functions are among these Twine exclusives and basic browser functions: | + | Technically speaking, any Javascript built-in function is available to the Twine author: ''parseInt()'', ''JSON.stringify()'', ''document.createElement()'' and all the rest are accessible, as are any functions that have been added by [[script]]s. However, you should find the most commonly useful functions are among these Twine exclusives and basic browser functions: |
====either(value, value, ...)==== | ====either(value, value, ...)==== | ||
Line 11: | Line 17: | ||
Give the either() function several string or number values, separated by commas, and it will pick one of them randomly. This allows a good degree of randomness to be inserted into the story, while still being fairly readable. | Give the either() function several string or number values, separated by commas, and it will pick one of them randomly. This allows a good degree of randomness to be inserted into the story, while still being fairly readable. | ||
- | You can use either() with [[<<print>>]] to print a random message or phrase: | + | You can use either() with [[<<print>>]] to print a random message or phrase... |
<code> | <code> | ||
"I sentence you to be buried alive in <<print either("rhinoceros","buffalo","triceratops")>> | "I sentence you to be buried alive in <<print either("rhinoceros","buffalo","triceratops")>> | ||
<<print either("vomit", "sweat", "snot")>>!" the JudgeBot crackles noisily. | <<print either("vomit", "sweat", "snot")>>!" the JudgeBot crackles noisily. | ||
</code> | </code> | ||
+ | ...or with [[<<display>>]] to display one of a set of passages. | ||
You can also use either() with [[<<set>>]] to set variables to random values: | You can also use either() with [[<<set>>]] to set variables to random values: | ||
Line 25: | Line 32: | ||
</code> | </code> | ||
- | You can also use either() with the [[link]] syntax to make a link that goes to a random passage: | + | And, in addition to macros, you can use either() with the [[link]] syntax to make a link that goes to a random passage: |
<code> | <code> | ||
You plunge into the [[glowing vortex|either("12000 BC","The Future","2AM Yesterday")]]. | You plunge into the [[glowing vortex|either("12000 BC","The Future","2AM Yesterday")]]. | ||
</code> | </code> | ||
- | |||
====random(value, value)==== | ====random(value, value)==== | ||
Line 67: | Line 73: | ||
With your sword and hat, nothing can stop you! | With your sword and hat, nothing can stop you! | ||
<<endif>>\</code> | <<endif>>\</code> | ||
+ | |||
+ | **Advanced use**: if you want to display something on every third time you visit a passage (no matter if you visit it 3 times, 10 times, or 100 times), then you can use the [[expression|modulo operator]] ''%'' to transform the number: | ||
+ | <code><<if visited() % 3 is 0>>\ | ||
+ | "Every 3 visits to this passage, I walk the Earth again," croons Count Dracula. | ||
+ | <<endif>>\</code> | ||
+ | Feel free to modify the "3" to any number you wish, to make something happen on every four visits, every ten visits, etc. | ||
====visitedTag(string, string...)==== | ====visitedTag(string, string...)==== | ||
Line 88: | Line 100: | ||
====tags()==== | ====tags()==== | ||
- | //New in version 1.4.1// | ||
Has a value equal to an array containing the current passage's tags. The meaning of "current passage" is the same as it is for passage(). Since this is a Javascript array, you will need to use built-in array functions to obtain values from it. Usually, you'd simply want to do something like this: | Has a value equal to an array containing the current passage's tags. The meaning of "current passage" is the same as it is for passage(). Since this is a Javascript array, you will need to use built-in array functions to obtain values from it. Usually, you'd simply want to do something like this: | ||
Line 125: | Line 136: | ||
Displays an alert box, with the given text string displayed. This is a browser built-in. It has no value - feel free to use it by simply writing <<set alert("message")>>. | Displays an alert box, with the given text string displayed. This is a browser built-in. It has no value - feel free to use it by simply writing <<set alert("message")>>. | ||
+ | ====open(string)==== | ||
+ | |||
+ | When given a URL in string form, it opens a new browser tab containing that web page. This is a browser built-in. It has no value - feel free to use it by simply writing <<set open("url")>>. | ||
+ | |||
+ | |||
+ | <- [[expression|About Expressions]] --------- [[print|Printing Variables and Functions]] -> | ||