So... after a great many years (not really) of trying and failing to make the SugarCube Story.lookup work to retrieve tags, I finally ported an answer from GreyElf that was made for Harlowe.
It currently works and pulls in ALL the appropriate titles--in my case passages that are tagged with the word "book", but it doesn't make these titles appear as separate line items. They seem to be all concatenated into one long string: Title,Title,Title
They even have commas, and sometimes a space between the commas and sometimes not (depending on what I have tried). But they are not separate discrete items (it seems) for me to use to make links.
I feel like this is a basic array display problem I am having but I am not exactly sure (like: just add <br> or \n).
/*this is the SugarCube version*/
Story.lookup("tags", "book");
/*it gives me object Object object Object*/
/*This is the Harlowe version I have ported over to SugarCube*/
<<set $names to [GetBooks.getNamesOfPassagesWithTag('book')]>>
<<for _i to 0; _i lt $names.length; _i++>>
<<capture _i>>
$names
/* or $names[_i] or <<print $names>> or <<print $names[_i]>> */
<<linkreplace "Accept books" `passage()`>><<addToBookInv $names[_i]>>You now have <<bookInvWithLinks>><</linkreplace>>
<</capture>>
<</for>>
/*it gives me the correct titles, but they are all in one long line. I am trying to add line breaks or otherwise separate them to use them in variables*/
Here is the JavaScript (again, credit @Greyelf)
window.GetBooks = window.GetBooks || {};
window.GetBooks.getNamesOfPassagesWithTag = function(tag) {
var tagged = $('tw-storydata tw-passagedata[tags~="' + tag + '"]')
var names = tagged.map(function () {
return this.getAttribute('name');
}).get();
return names;
};
If this is unclear please let me know and I can perhaps further elucidate.
One last thing that may help. There is an answer here by both Greyelf and The Mad Exile about variables being initialized improperly and then being converted by JavaScript. I am not sure if that is the issue but in my case it is initialized in StoryInit, thus:
<<set $names = []>>
It seems to get reset here:
<<set $names to [GetBooks.getNamesOfPassagesWithTag('book')]>>
I say that because whether I try =0 or =[] I get the same result. I think that is because it gets set here (just above). Removing that from StoryInit does not solve the issue either.
I do hope that helps.
Thank you.
PS, Greyelf I will be reaching out to you next week.