User Tools

Site Tools


variable

Differences

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

Link to this comparison view

Next revision
Previous revision
variable [2013/12/17 19:53]
l created
variable [2017/10/09 20:39] (current)
Line 1: Line 1:
 ===== Variables ===== ===== Variables =====
  
-[[Expression]]s by themselves aren'​t ​much use to your storiesUnless you're writing a mathematical thrilleryou probably don't need to evaluate 2 + 2 too often. Where they do come in handyhoweveris when **variables** ​enter the picture.+[[Expression]]s by themselves aren'​t ​particularly powerful[[Function]]s like previous()either() and visited() can help in making [[link]]s[[<<​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 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.+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 ==== ==== Variable names ====
Line 30: Line 30:
 </​code>​ </​code>​
  
-==== <<​set>>​ ==== +To manipulate variablessee the articles for the [[<<​set>>​]] macro and the [[<<​remember>>​]] macro, as well as the [[link]] syntax.
- +
-Variables are a good way to keep track of what a reader has chosen in a storyor 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 gameuse 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 += 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>​+
variable.1387328010.txt.gz · Last modified: 2017/10/09 20:38 (external edit)