0 votes
by (370 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

+1 vote
by (159k 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 (370 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!
by (370 points)

Just following up on this. The code works very well in the twine interface. However, because summarizing all of my passages is a complex operation with a lot of cutting and pasting, I went ahead and moved my project to Twee. That made the content editing vastly simpler. However, now the summary code doesn't work. For clarity, I can (and have) literally cut and paste it back into Twine and it will run without error, but when I compile with Twee there are errors when it runs. Here's the error:

Unexpected identifier '_passagename'. Expected either a closing ']' or a ',' following an array element.
Can't find variable: _passagename

I have repeatedly counted braces/brackets and everything that opens gets closed. Is there an issue with using 'for' in Twee?

I should probably specify my Twee version:

$ twee2 help
Twee2 0.5.0

 

by (159k points)

A fun fact: the examples I supplied were tested using a TWEE compiler and were never entered into the Twine 2 application, so I know they can be compiled using TWEE as long as the correct version(s) of the specific Story Format are used during that compile.

The Twee2 project has not been updated in a long time and I am unsure which series or versions of the main story formats it actually supports.
eg. Harlowe 1.x; Harlowe 2.x; Harlowe 3.x; SugarCube 1.x; SugarCube 2.x; Snowman.

I personally use (and would recommend) the Tweego TWEE compiler, as it is still being actively updated and it also has access to the latest versions of the main Story Formats.

by (370 points)

A fun fact: the examples I supplied were tested using a TWEE compiler and were never entered into the Twine 2 application, so I know they can be compiled using TWEE as long as the correct version(s) of the specific Story Format are used during that compile.

Thanks for the reply, Greyelf. I don't doubt it. Confident the error is mine. 

I did try Tweego. However, I cannot compile with it. It inevitably gives me this error:

error: Starting passage "Start" not found.

However, the very first lines of my code are:

::Start
[[Intro]]

::StoryTitle

No doubt, I've got some lingering problem from decompiling with Twee2 perhaps? Google turns up relatively few results for this particular error and none of them provide answers.

by (370 points)

I just tested decompiling my html with Tweego. It still gives:

error: Starting passage "Start" not found.
by (370 points)
I was able to fix this by recreating all of my files and then cutting and pasting the text over into new files. Not sure why that made any difference, but the error is now gone and I can compile with Tweego.
...