I apologise for asking this since I know very little Javascript to work with Snowman, but I've done great advances porting my Harlowe game to Snowman.
I have these two passages:
::render
<%= story.render("content") %>
::content
<a style="color:red" id="hook">Link</a>
<script>
$('#hook').click(function () {
story.show("render")
})
</script>
You run ::render, it displays ::content, with a link that when clicked runs render again. It works perfectly.
Now change ::content like this:
::content
<a style="color:red" id="hook">Link</a>
<script>
$('#hook').click(function () {
story.show("render")
})
</script>
Some other text.
And now, the story.render() function fails with an
Unexpected token message.
Can you tell the reason for this? My final goal is building a loop that renders several passages like ::content next to each other, but it fails because printing anything after the </script> tag breaks something with story.render.
Thanks!
Comments
warning: your Javascript is missing two semi-colons and is relying on the web-browser's parser to add them, this is not a good idea because it could get it wrong.
Try the following:
note: In Twee Notation there is generally a space character between the double colons (::) and the Passage Name.
From Snowman's documentation:
More specifically, I can't move the script to the end of the passage because in the real game (this is an example) I story.render a series of passages, each one being a link and a script.
What my game does is: I have multiple room passages. I have an array for each room with the choices each room offers at that moment of the game. Each choice is a passage like the one here: a link and the actions run on clicking the link. Those choice passages get story.rendered one after another, so the result is like this example: text + script + text + script + text + script.
So does the following example which uses TheMadExile's solution.
@Rokiyo yes, I added the suggested semicolons.
I can add that in my first test I wrote the script within underscore's <% %> instead of <script> tags, and the link simply didn't do anything when clicked.
* I tried to update Twine when the new version came out but I ran into a problem with the permissions for the working files folder (on Linux), and Twine couldn't save anything. Actually it doesn't create a Twine document folder. I'll have another go.
When I made my original post, I overlooked the fact that you were using a <script> tag, rather than <% … %>—because who does that. You should definitely be using <% … %>, rather than a <script> tag. In particular, the $(function () { … }) is Snowman's post-display callback within <% … %>, but will be jQuery's ready callback within a <script> tag—which, as I've posted numerous times, is patently worthless when used within a story format. Additionally, the special s alias variable, also only works within <% … %>.
Beyond that, Twine 2 v2.0.11 came preinstalled with Snowman 2 v1.3.0—v1.2 last appeared in Twine 2 v2.0.10. Exactly which version of Twine 2 are you using?
Anyway, I downloaded Twine 2 v2.0.10, extracted Snowman 2 v1.2 from it and installed it into Twine 2 v2.1.0. I then tested the following code, which performed exactly as expected:
It seems that I can move forward now. Thanks again! I apologise again for asking this kind of things, but something like "post-display callback" is beyond my Javascript knowledge.
It's Twine 2.0.10 I'm using. I wasn't aware of 2.0.11 since I really use twee2 instead of Twine, and I've failed to make 2.1 work.