0 votes
by (470 points)
edited by
<<set $curpas = passage()>>

<<if $part == "START">>
Start event description. 
 <<link "Choice 1">>
  <<set $part = "EVENT_1">>
  <<set $param_1 += 10>>
  <<set $param_2 -= 10>>
  <<goto $curpas>>
 <</link>>
 <<link "Choice 2">>
  <<set $part = "EVENT_2">>
  <<set $param_1 -= 10>>
  <<set $param_2 += 10>>
  <<goto $curpas>>
 <</link>>
<</if>>

<<if $part == "EVENT_1">>
Event 1 description.
 <<link "Choice_1_1">>
  <<set $part = "EVENT_1_1">>
  <<set $param_1 += 10>>
  <<goto $curpas>>
 <</link>>
 <<link "Choice_1_2">>
  <<set $part = "START">>
  <<set $param_2 += 10>>
  <<goto "Another passage">>
 <</link>>
<</if>>

<<if $part == "EVENT_2">>
Event 2 description.
 <<link "Choice_2_1">>
  <<set $part = "EVENT_2_1">>
  <<set $param_2 += 5>>
  <<goto $curpas>>
 <</link>>
 <<link "Choice_2_2">>
  <<set $part = "EVENT_2_2">>
  <<set $param_1 -= 7>>
  <<goto $curpas>>
 <</link>>
<</if>>

<<if $part == "EVENT_1_1">>
Event 1_1 description.
 <<link "Next">>
  <<set $part = "START">>
  <<goto "Next passage">>
 <</link>>
<</if>>

<<if $part == "EVENT_2_1">>
Event 2_1 description.
 <<link "Next">>
  <<set $part = "START">>
  <<set $param_2 += 5>>
  <<goto "Next passage">>
 <</link>>
<</if>>

<<if $part == "EVENT_2_2">>
Event 2_2 description.
 <<link "Next">>
  <<set $part = "START">>
  <<set $param_1 -= 10>>
  <<goto "Next passage">>
 <</link>>
<</if>>

Is there more effective method for organization this code?

If I will make passages for each event it will be too many passages (it can be almost hundred events in one passage). With method that I use now it is very hard to find mistakes like missed ">" or "<".

I use links, because I need to change parameters outside of text.

1 Answer

+1 vote
by (159k points)
selected by
 
Best answer

As you have surmised, you generally use either multiple Passages or multiple conditionals statements (, or both methods together) to selectively display content.

note: There is structural issue with how you are using the <<if>> macro in your example.
Because you are using a single $part variable to control which content is shown, and because only one of your <<if>> macros can be true at any one time then all but the first <<if>> macro should be an <<elseif>> macro instead.

<<if $part == "START">>
Start event description. 
...

<<elseif $part == "EVENT_1">>
Event 1 description.
...

<<elseif $part == "EVENT_2">>
Event 2 description.
...

<<elseif $part == "EVENT_1_1">>
Event 1_1 description.
...

<<elseif $part == "EVENT_2_1">>
Event 2_1 description.
...

<<elseif $part == "EVENT_2_2">>
Event 2_2 description.
...

<</if>>

Another possibility is to replace the <<if>> related macros with the <<switch>> macro like so:

<<switch $part>>
<<case "START">>
	Start event description.
	...

<<case "EVENT_1">>
	Event 1 description.
	...

<<case "EVENT_2">>
	Event 2 description.
	...

<</switch>>

warning: both of the above examples have not been tested, they may contain syntax errors. 

by (470 points)
edited by
Thank you very much for your answer. <<Switch>> looks more effective than <<if>> in my case.
Is there any advice about debugging in so long passages? Sometimes I look for missing "<" or ">" about half an hour. Even in debbuging mode Twine doesn't show WHERE I missed them.

Or something like this "Error: child tag <</if>> was found outside of a call to its parent macro <<if>>". In long passage with many conditionals statements it's very hard to find where I missed it.
by (159k points)

The "Debugging" functionality of each story format is implemented by that story format itself, all the Twine 2 application does is ask the story format to switch to debug mode if it has one.

You would need to contact the developer of a particular story format with any suggestions you have with improving its debugging functionality / interface, in your particular case that would be by creating a new issue of the SugarCube project repository's Issues page.

it's very hard to find where I missed it

Code indentation can help with making sure your structure is balanced (eg. start & end tags) and syntax highlighting (if supported) can help with invalid syntax.

...