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/11 09:36]
l
if [2017/10/10 00:39] (current)
Line 1: Line 1:
 +<- [[remember|Remembering Things With Variables]] --------- [[nobr|Removing Line Breaks]]->​
 +
 +=====<<​if>>​=====
 +
 The **<<​if>>​** [[macro]] is used to control what text the player sees when they visit a passage, or what macros are run when the [[passage]] loads. The **<<​if>>​** [[macro]] is used to control what text the player sees when they visit a passage, or what macros are run when the [[passage]] loads.
  
-=====Usage=====+====Usage====
  
 The <<​if>>​ macro can be used in formats like these: The <<​if>>​ macro can be used in formats like these:
  
-><<​if //condition//>> //Text// <<​endif>>​+>%%<<%%if //expression//%%>>%% //​Text// ​%%<<%%endif%%>>%%
  
-><<​if //condition//>> //Text// <<​else>>​ //Text// <<​endif>>​+>%%<<%%if //expression//%%>>%% //​Text// ​%%<<%%else%%>>%% //​Text// ​%%<<%%endif%%>>%%
  
-><<​if //condition//>> //Text// <<​else if //condition//>> //​Text// ​<<​else if //​condition//>> ​... <<​endif>>​+>%%<<%%if //expression//%%>>%% //​Text// ​%%<<%%else if //expression//%%>>%% //Text// ... %%<<%%endif%%>>%%
  
-//condition// is a code 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.
  
-=====A note about line breaks=====+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==== 
 + 
 +Consider a passage like this: 
 + 
 +> You return to Selator'​s hut. A merry fire is crackling in the kitchen, and something is cooking that smells delicious. He greets you warmly and asks, "Have you got the berry?"​ If you have got the purple berry of the Antherica plant, turn to 175. If not, turn to 52. 
 + 
 +(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. We can do this by simply noting the name of the passage where the berry was found: 
 + 
 +<​code>​ 
 +:: Antherica 
 +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 
 +village with the precious berry. 
 +</​code>​ 
 + 
 +Then we can use the <<​if>>​ macro and the [[function|visited() function]] to display a passage indicating victory: 
 + 
 +<​code>​ 
 +You return to Selator'​s hut. A merry fire is crackling in the kitchen, and 
 +something is cooking that smells delicious. He greets you warmly and asks, 
 +"Have you got the berry?"​ 
 + 
 +<<if visited("​Antherica"​)>>​ 
 +"​Wonderful!"​ he exclaims... 
 +<<​endif>>​ 
 +</​code>​ 
 + 
 + 
 +====A note about line breaks====
  
 When a passage containing an <<​if>>​ macro is displayed, the <<​if>>​ and <<​endif>>​ macro tags are removed from the text. But, the line on which the tags was placed will remain - thus, if it was by itself on a line, it will create a blank line. You can choose to fix this by ending the tags' lines with a [[syntax|backslash]]:​ When a passage containing an <<​if>>​ macro is displayed, the <<​if>>​ and <<​endif>>​ macro tags are removed from the text. But, the line on which the tags was placed will remain - thus, if it was by itself on a line, it will create a blank line. You can choose to fix this by ending the tags' lines with a [[syntax|backslash]]:​
Line 22: Line 60:
 She laughs. "Still not dead? How callous of you to scorn Death'​s loving embrace!"​ She laughs. "Still not dead? How callous of you to scorn Death'​s loving embrace!"​
 <<​endif>>​\ <<​endif>>​\
-<​code>​ 
- 
 </​code>​ </​code>​
  
 +You may, for larger <<​if>>​ constructions,​ use the <<​[[nobr]]>>​ macro instead:
  
-=====<<​else>>​=====+<​code>​ 
 +You glare at the alien control panel. <<​nobr>>​ 
 +<<if $red>>​ 
 +The red button blinks <<if $earplugs>>​ and hisses<<​endif>>​ at you. 
 +<<​endif>>​ 
 +<<​endnobr>>​ 
 +</​code>​ 
 + 
 +====<<​else>>​====
  
 <<​else>>​ is a macro that is used to indicate text that should be displayed if the condition is //false//. <<​else>>​ is a macro that is used to indicate text that should be displayed if the condition is //false//.
Line 43: Line 88:
  
 <​code>​ <​code>​
-<<if $health is 3>> +<<if $health is 3>>\ 
-You're in tip-top condition. No human or god can lick you! +You're in tip-top condition ​- fighting fit and frankly fearsome. No human or god can lick you! 
-<<​else if $health is 2>> +<<​else if $health is 2>>\ 
-DOOMED! You're DOOMED, puny player!! You've lost already! ​Give up+DOOMED! You're DOOMED, puny player!! You've lost already! ​YOU CANNOT WIN!! MWAHAHAHAHA
-<<​endif>>​+<<​endif>>​\
 </​code>​ </​code>​
  
 +You can insert as many <<​else if>>'​s as you want inside an <<​if>>​ / <<​endif>>​ pair.
 +
 +(Note: if you prefer, you can also write "else if" as "​elseif"​.)
 +else if
 +
 +<- [[remember|Remembering Things With Variables]] --------- [[nobr|Removing Line Breaks]]->​
  
if.1386754587.txt.gz · Last modified: 2017/10/10 00:37 (external edit)