User Tools

Site Tools


twine1:variable

Variables

Expressions by themselves aren't particularly powerful. Functions like previous(), either() and visited() can help in making links, <<print>> and <<if>> macros more interesting, but if you want anything more complex than those, variables will be necessary.

A variable is a place to store a value so that you can remember it later. You can store strings, booleans and numbers in variables. The variable $name might contain the main character's name, “Agatha Christie”, or the variable $money might contain the amount of money Agatha has in her pocket – the number 15.75.

Variable names

Variables have a few restrictions on their names. They must start with a $ sign. That's called a sigil — it tells the computer that what's coming next is a variable, not a number or string.

After the initial $ sign, a variable name can begin with a letter, either uppercase or lowercase, or an underscore _. After the first letter, you can have any combination of letters, numbers, or underscores. Punctuation and spaces aren't allowed anywhere.

Here are some legitimate variable names:

$housesDestroyed
$_my_favorite_color
$AN_EXTREMELY_IMPORTANT_NUMBER
$street8
$i

Some bad variable names:

$what was it called
$Idon'tRemember
$3.95
$8thSurprise
$$$make_money_fast$$$

To manipulate variables, see the articles for the <<set>> macro and the <<remember>> macro, as well as the link syntax.

twine1/variable.txt · Last modified: 2016/05/09 16:28 (external edit)