0 votes
by (170 points)

What I'd like to do:

At each story node, I'd like to have links pointing "up the tree" of story nodes back to the start node. Since twine is fundamentally a tree structure, my thought is that there should be some way to traverse that tree "backwards" up to the root node.

My original thought was that I could use the "back"/"history" functionality, but because there might be internal loops within the structure, going "back" isn't always equivalent to going "up" towards the "start" node.

Is there some way to traverse the tree of story nodes vertically?

Background:

I'm formatting a story that's less of a story and more of an investigation into a topic/book. This makes the structure more hierarchical: I start with a linear background/overview, then separate out into parts, and each part has multiple chapters, and each chapter has sub-sections (which may have sub-subsections). But because I'm not telling a chronological story, you could go to the different parts/chapters/sub-headings in any order, and may want to jump around: each section more or less stands independently but still may have links to other sections. After you've explored any given section sufficiently, I'd like to be able to go "up a level" back from sub-heading to chapter to part to index, or up multiple levels at once. Hard-coding these links for each node would be really obnoxious. I started on sugarcube, but would be perfectly willing to switch formats if that adds ease of use.

Potential Solutions:

A) If there's a way to access the connectivity map of the nodes, it wouldn't be that hard to search the tree from the root node down to the current node and use that path to make the links. But I don't know if there's a way to get at that node structure.

B) Alternatively, if there's a way to "post-process" your story, node-by-node, you could also implement some sort of "breadth-first" search over the nodes of the story, and mark mark which branch each story point lies on. This branch-marking could be used to generate the links needed to point back to the root node.

C) A completely different solution is if you can force links to be run multiple at a time, or if you can mess with the story history. Since the "back" button/history solution works fine to go up the tree unless you have created loops in the tree, I could eliminate loops by changing links that don't point "down" one level of the tree to act as if the user clicked "back" the appropriate amount of times and then re-entered the tree to go down to that other node. This would mean that I could always use the "back" button to go "up" the tree, level by level. This still comes back around to the connectivity question because I'd have to know how to traverse "down" to these other nodes, and would significantly change the behavior of links, so may be super-complicated to implement.

D) I could code the story to not have any loops and be structured exactly as a tree, so using "back" and "history", I should be able to make what I want work. This kind of destroys a bunch of the utility of using twine, but would still be a workable option and should be simplest to implement.

I'm completely new to twine, so this is more a matter of me poking around to see what might be able to be done for my amusement rather than as any sort of dedicated/professional project. As far as I can see, the options apart from (D) are not possible without downloading and messing with the source - which I'm totally fine doing. I haven't coded anything in javascript before, but peeking at the source it seems doable. (B) seems right now to be my best bet - when opening the story, I can edit the javascript to run through all the nodes and add the hierarchical set of links at the bottom or top of each story node, acting like a header or footer.

If there's an easier way, I'd love to hear feedback. Or if you think I shouldn't be using twine for this, it'd be good to know I'm barking up the wrong tree. I hope someone has some thoughts - would appreciate any insight/help!

2 Answers

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

Well, option A is out, because even the Twine developmen environment doesn't map node connections if they use the <<link>> macro or use a variable for the passage name.  Option C is dangerous and likely wouldn't work unless you had a very small or simple story so you could deal with the potential complications.  Option D shouldn't be necessary and even if you did it, that wouldn't make traversing the nodes any simpler.

That leaves option B, which won't work normally, because there are various ways to link passages, so parsing each one isn't feasable.  That said, there are ways to make it work.  Greyelf's suggestion of marking passages with tags which indicate their position in the hierarchy is one way of doing that.

However, another option is probably simpler.  Basically, you don't need to get the whole tree up front, you only need to be able to see the next level down and to remember the levels back up.  Thus you build the tree as you go.

I put together a simple bit of sample code showing one way that could be done here: "Tree_Test.html"  You can download that and import it into Twine 2 so you can see how the code works.  The "Global Widgets" passage gives you the <<AddChild>> and <<ShowLinks>> widgets, and in the JavaScript section you'll find the code for the AllChildrenComplete() function.  If that isn't quite what you want then hopefully that should give you enough information so you could modify that into what you want.

Hope that helps!

by (170 points)
Thank you - this is very helpful, especially your comments on each of the options I laid out. Unfortunately, I am getting an "access denied" message from that html file you included -if  you'd be able to make it public, I'll try to download it again.
by (44.7k points)
Whoops!  Sorry, forgot to make it sharable.  You should have access to it now.
by (170 points)
Excellent, that's incredibly helpful; I'll definitely use this as a jumping-off point. The navigation is very slick - thank you for your time to mock something up!
by (44.7k points)

No problem.  If you have any other questions, make sure you start a new thread, because I tend to not check for followup questions once the question has gone off the first Twine Q&A page.

Also, if you want to host a file like that yourself, it's just a combination of Google Drive (to store the files) and DriveToWeb (to make them accessible as web pages).

Have fun!  :-)

+1 vote
by (159k points)

>  is fundamentally a tree structure...

The Passages are stored as a simple list within the "store-area" of the story HTML file, and the only relationships between the passages and/or any potentual structures that they may form comes from how the Author links those passages together. eg. there is no inherent tree structure.

The story's State History system tracks each Passage that the Reader visits as they travel forward through the story, each step (Passage visited) along this path is stored within the History as a Moment in time. To complicate matters the History system actually only stores a pre-configured maximum number of Moments, and once this limit (100) is reached the adding of any new Moment will cause the oldest Moment in the History to be expired.


> to be able to go "up a level" back from sub-heading to chapter to part to index..

(untested) You may be able use Passages Tags to distinguish the 'level' (index > chapter > sub-heading > <no-tag>)  that each Passage occupies within your story sturcture, and then search backwards through the State History for the first Passage that was assigned the next higher tag.

...