User Tools

Site Tools


if

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
if [2013/12/17 04:26]
l
if [2017/10/10 00:39] (current)
Line 1: Line 1:
 +<- [[remember|Remembering Things With Variables]] --------- [[nobr|Removing Line Breaks]]->​
 +
 =====<<​if>>​===== =====<<​if>>​=====
  
Line 13: Line 15:
 >​%%<<​%%if //​expression//​%%>>​%% //Text// %%<<​%%else if //​expression//​%%>>​%% //Text// ... %%<<​%%endif%%>>​%% >​%%<<​%%if //​expression//​%%>>​%% //Text// %%<<​%%else if //​expression//​%%>>​%% //Text// ... %%<<​%%endif%%>>​%%
  
-//condition// is an [[expression]] that can evaluate to true or false. //Text// is any amount of passage text that you wish to display only if the condition is //true//. <<​endif>>​ is a macro tag indicating the end of the <<​if>>​ macro invocation.+//expression// is an [[expression]] that can evaluate to true or false. //Text// is any amount of passage text that you wish to display only if the condition is //true//. <<​endif>>​ is a macro tag indicating the end of the <<​if>>​ macro invocation. 
 + 
 +Note that the //Text// can contain any Twine code, including an inner <<​if>>​ invocation:​ 
 + 
 +  <<if $body is "​wounded">>​You are <<if $blood <= 5>>​about to die<<​else>>​bleeding<<​endif>>​. Seek help!<<​endif>>​
  
 ====Motivating example==== ====Motivating example====
Line 23: Line 29:
 (Steve Jackson and Ian Livingstone,​ //Scorpion Swamp//) (Steve Jackson and Ian Livingstone,​ //Scorpion Swamp//)
  
-It would be nice if the story could track whether the protagonist found the berry or not, and branch accordingly. ​In order to do this, we need to [[<<​set>>​]] a [[variable]] to indicate whether ​the protagonist found the berries:+It would be nice if the story could track whether the protagonist found the berry or not, and branch accordingly. ​We can do this by simply noting ​the name of the passage where the berry was found:
  
 <​code>​ <​code>​
 +:: Antherica
 You have no doubt, from Selator'​s description,​ that you have found the You have no doubt, from Selator'​s description,​ that you have found the
 Antherica plant. Half your mission is completed. Now you must return to the Antherica plant. Half your mission is completed. Now you must return to the
-village with the precious berry. ​<<set $foundBerry = true>>​+village with the precious berry.
 </​code>​ </​code>​
  
-Then we can use the <<​if>>​ macro to display a passage indicating victory:+Then we can use the <<​if>>​ macro and the [[function|visited() function]] ​to display a passage indicating victory:
  
 <​code>​ <​code>​
Line 38: Line 45:
 "Have you got the berry?"​ "Have you got the berry?"​
  
-<<​if ​$foundBerry>>+<<​if ​visited("​Antherica"​)>>
 "​Wonderful!"​ he exclaims... "​Wonderful!"​ he exclaims...
 <<​endif>>​ <<​endif>>​
 </​code>​ </​code>​
  
-====Invalid usage==== 
- 
-You should take care when writing <<​if>>​ macros that you write correct expressions,​ and that you remember what the "​and"​ and "​or"​ keywords are capable of. 
- 
-<​code>​ 
-<<if $health > 2 and < 4>> 
-</​code>​ 
-This is invalid because the ''>''​ and ''<''​ operators (and indeed, all such operators) require a distinct value to be on both sides of it. In a sense, it is internally interpreted as ''<<​if ($health > 2) and ( < 4)>>'',​ which is obviously nonsensical. So, you must rewrite this as ''<<​if $health > 2 and $health < 4>>''​. 
- 
-<​code>​ 
-<<if $name is "​Perone"​ or "​Pavone">>​ 
-</​code>​ 
-This is also invalid, for a different reason: it will be interpreted as ''<<​if ($name is "​Perone"​) or ("​Pavone"​)>>''​ - which is to say, it is true if $name is "​Perone",​ or if the string "​Pavone"​ is not false. This, of course, means that it's always true - an undesirable outcome. You must rewrite it as ''<<​if $name is "​Perone"​ or $name is "​Pavone">>''​. 
  
 ====A note about line breaks==== ====A note about line breaks====
Line 105: Line 99:
 (Note: if you prefer, you can also write "else if" as "​elseif"​.) (Note: if you prefer, you can also write "else if" as "​elseif"​.)
 else if else if
 +
 +<- [[remember|Remembering Things With Variables]] --------- [[nobr|Removing Line Breaks]]->​
 +
if.1387254409.txt.gz · Last modified: 2017/10/10 00:37 (external edit)