Hello,
This is a pretty basic question, but I've been struggling to come up with a clean answer to it. If I have the title of a passage I'd like to link the player to in Javascript, is there an easy way of building a link to that passage? I know enough jQuery to potentially grab the anchor element and manually alter its data-passage attribute, but it seems like there has to be an easier way.
For example, given a passage which starts with this:
<<script>>
var nextPassage = state.peek(10).title;
<</script>>
...is there an easy way to generate a link that sends the user to the passage they visited 10 steps ago? I was hoping it would be as easy as this:
[[Step into the time machine|$nextPassage]]
...but it seems it's not quite that simple. Again, I have enough jQuery under my belt to grab elements and alter them (and if I were just trying to spit out some text, that's what I'd do: define a <div> in the passage and then, after setting nextPassage, use jQuery to grab the div and insert the appropriate text), but I assume there's a simpler way.
Thank you!
Comments
The one thing I wanted to mention is that the link-creation markup version of this didn't work for me, I had to use the link macro. Specifically, using this:
...linked to a new passage called "$backTen", instead of evaluating $backTen as a variable. The SugarCube documentation says you can use variable names in the link markup, so I'm not sure why this didn't work for me.
I don't know why I can only find the answers to my questions after I post them. Hopefully this helps someone else!
However, if you absolutely wanted to use a $nextPassage variable for some reason, then you could accomplish that like so: Still, I'd advise against creating unnecessary story $variables.
And that's even easier than I what I found. I didn't realize you could invoke Javascript quite so easily.
Thank you!
edit: Ah, I actually just tried those both, and neither worked for me. This is using the Twine 2 web tool, version 2.0.8. This:
...creates a link to a passage called "state.peek(10).title", while this:
...first throws an error because "state" is undefined, then creates a link to a passage called "$nextPassage". Is this the result of a difference in versions?
You will need to edit each of the new passages with invalid names and rename them.