Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

[Sugarcube 2] List of all passages with tag

Hey I'm wondering if its possible to create a passage that dynamically links to every passage with a certain tag. Like an index page that will update as I add passages


  • The Story.lookup function will return an array of all the passages that meet a set condition, the example shows using it to look for all passages with the "forest" tag.

    If you combine that list with a <<for>> macro you should be able to create what you want.

    note: I am not current at a machine with SugarCube 2 installed, so the following has not been tested.
    <<set $list to Story.lookup("tags", "forest")>>
    <<for $i to 0; $i lt $list.length; $i++>>
    	<<set $title to $list[$i].title>>
  • edited October 2015
    Thanks! That seems to work great for populating the list but I'm seeing an issue with linking to actual passages. It autocreates a passage called $title and isn't interpreting the variable in the passage. I just get a number of broken links that all display $title

    However if I do <<print $title>> it does correctly show the variable value, lthough not as a link of course

    EDIT It looks like it works if i use <a data-passage=$title>$title</a> and publish to a file. It seems the error is coming from Twine's test browser.
  • edited October 2015
    Currently Twine 2 features like Auto Passage Creation and Broken Link Detection and Passage Map Connections only understands basic markup links like the following:
    [[Target Passage Name]]
    [[Link Text|Target Passage Name]]
    [[Link Text->Target Passage Name]]
    [[Target Passage Name<-Link Text]]
    So when you use advanced markup links (like ones containing $variables) or macro based links strange things happen like incorrectly named auto-created passages. Generally you can delete these passages.

    I did say my example was untested *smile*
  • As a workaround, you could do something like the following:
    <<set $list to Story.lookup("tags", "forest")>>\
    <<for $i to 0; $i lt $list.length; ++$i>>
    	<<print "[" + "[" + $list[$i].title + "]" + "]">>
    <<unset $list, $i>>

    Or, for a JavaScript-style solution, this:
    		.lookup("tags", "forest")
    		.map(function (passage) {
    			return "[" + "[" + passage.title + "]" + "]";

    Both of which should bypass Twine 2's less than ideal link handling.
Sign In or Register to comment.