0 votes
by (150 points)

I've seen some similar threads to this, but not an answer that will work for me. At the end of my story, I would like to give the reader a summary of everything that happened in the story and the order it happened in. I was hoping to just create a for loop with history, but that doesn't look like it's possible at least not while maintaining the order of events.

Right now I'm doing this:

(for: each _passage, ...(history:))[
	(if: _passage is 'TakeSword')[You picked up the sword.]
    (if: _passage is 'SwamLake')[You swam across the lake.]
]

This works, in so far as the appropriate text appears. However, if you managed to swim the lake before taking the sword, it still shows the sword first. Is there a better way to do this? 

Note, I'm prepared to manage the differences in player choices and states, I think I've kept the story simple enough that that won't get too out of hand.

Thanks!

1 Answer

0 votes
by (149k points)
selected by
 
Best answer

The array generated by the (history:) macro contains the names of each of the Passages visited by the Reader (excluding the current Passage) in the order that those passages were visited, and if a specific Passage was visited more than once then it's name will appear in the Array multiple times.

note: the rest of this explanation will be based on the following test code example, which is written in TWEE notation, the Start passage is the first passage of the project.

:: Start

:: Passage A

:: Passage B

:: Passage C

:: Links [footer]
(unless: (passage:)'s name is "View Path")[\
[[Passage A]]
[[Passage B]]
[[Passage C]]

[[View Path]]\
]

:: View Path
You have visited: {
(for: each _passagename, ...(history:))[
	<br>_passagename
]}


If you view the above story and visit the passages in the order A > B > C then visit the View Path passage you should see the following output.

You have visited: 
Start 
Passage A 
Passage B 
Passage C 

And if you restart the story and visit the passages in this order A > C > B > C > View Path instead then you should see the following, and notice that the Passage C passage appears multipe times in the output.

You have visited: 
Start 
Passage A 
Passage C 
Passage B 
Passage C 

So as you can see the order that passages were visited is maintained within the Array generated by the (history:) macro.

If you need a list of passage names that never contains a specific Passage more than once then you will need to use code like the following to generated it.

{
(set: $visited to (a:))
(for: each _passagename, ...(history:))[
	(unless: $visited contains _passagename)[
		(set: $visited to it + (a: _passagename))
	]
]

You have visited:
(for: each _passagename, ...$visited)[
	<br>_passagename
]
}


Now for the summary part of your question, and there are a number of ways you could implement this but I will expand on the method you were trying to use. The following example includes both the reducing of the History to a list of unique Passage names as well as using a named hook as the target of the generated summary.

|summary>[]
{
(set: $visited to (a:))
(for: each _passagename, ...(history:))[
	(unless: $visited contains _passagename)[
		(set: $visited to it + (a: _passagename))

		(if: _passagename is "Passage A")[
			(append: ?summary)[Passage A's associated Text<br>]
		]
		(else-if: _passagename is "Passage B")[
			(append: ?summary)[Passage B's associated Text<br>]
		]
		(else-if: _passagename is "Passage C")[
			(append: ?summary)[Passage C's associated Text<br>]
		]
	]
]
}

 

by (150 points)
Thanks for this, greyelf. Your script is far enough beyond my knowledge that I'm having to do a lot of reading to figure out how it works, but it looks like it should solve my issue. Thanks so much!
Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...