User Tools

Site Tools


remembering_things_with_variables

Differences

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

Link to this comparison view

remembering_things_with_variables [2013/12/12 21:33]
l [<<remember>> and <<forget>>]
remembering_things_with_variables [2017/10/09 20:39]
Line 1: Line 1:
-===== Remembering Things with Variables ===== 
  
-Expressions by themselves aren't much use to your stories. Unless you're writing a mathematical thriller, you probably don't need to evaluate 2 + 2 too often. Where they do come in handy, however, is when **variables** enter the picture. 
- 
-A variable is a place to store a value so that you can remember it later. You can store both strings 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: 
- 
-<​code>​ 
-$housesDestroyed 
-$_my_favorite_color 
-$AN_EXTREMELY_IMPORTANT_NUMBER 
-$street8 
-$i 
-$_ 
-</​code>​ 
- 
-Some bad variable names: 
- 
-<​code>​ 
-$what was it called 
-$Idon'​tRemember 
-$3.95 
-I_Forgot_Something_Important 
-$8thSurprise 
-$$$make_money_fast$$$ 
-</​code>​ 
- 
-==== <<​set>>​ ==== 
- 
-Variables are a good way to keep track of what a reader has chosen in a story, or to manage some other part of the story state. For example, many gamebooks start off with something like this: 
- 
-> All you possess is an Axe (note under Weapons on your Action Chart) and a Backpack containing 1 Meal (note under Meals on your Action Chart). 
- 
-(Joe Dever, //Flight from the Dark//) 
- 
-You can keep track of the number of meals that the protagonist carries with the ''<<​[[set]]>>''​ macro, like so: 
- 
-<​code>​ 
-All you possess is an Axe and a Backpack containing 1 Meal. 
-<<set $meals to 1>> 
-</​code>​ 
- 
-The left side of the equals sign has a single variable, and the expression to set it to is on the right. It's on the same line as the real text of the story so that extra whitespace doesn'​t appear in the story text. 
- 
-Later on in the story, you can change the value of a variable with another ''<<​set>>''​ statement. 
- 
-<​code>​ 
-You are feeling tired and hungry and you must stop to eat. <<set $meals -= 1>> 
-</​code>​ 
- 
-The ''​-=''​ operator lowers the variable on the left by the value on the right. There is also a ''​+=''​ operator that does the opposite. 
- 
-If you make a mistake with ''<<​set>>'',​ a pink highlighted message will appear where you invoked it. Here's a sample error message, in this case forgetting the sigil before the variable ''​$meals'':​ 
- 
-<​code>​ 
-bad expression: meals is not defined 
-</​code>​ 
- 
-==== <<​remember>>​ and <<​forget>>​ ==== 
-Variables are reset every time the reader restarts a story. If you'd like to remember the value of a variable after the game, use the ''<<​remember>>''​ macro. It works exactly the same way as ''<<​set>>'',​ except it stores the variable in browser storage that remains between sessions. 
- 
-<​code>​ 
-At long last, you have escaped the dungeons. <<​remember $escapes = $escapes + 1>> 
- 
-So far, you have escaped the dungeons <<​print $escapes>>​ times. 
-</​code>​ 
- 
-If you are only interested in remembering the current state of a variable, you don't need to include an assignment: 
- 
-<​code>​ 
-<<​remember $meals>>​ 
-</​code>​ 
- 
-If you want to reset the remembered value, you can use the <<​forget>>​ macro: 
- 
-<​code>​ 
-<<​forget $meals>>​ 
-</​code>​ 
remembering_things_with_variables.txt · Last modified: 2017/10/09 20:39 (external edit)