First things first: my deepest thanks to the whole forum and specially
@greyelf and
@TheMadExile who have helped me again and again. I've just ported 100% of my Harlowe game to Snowman, despite not knowing Javascript, and it works all right.
Now, one of those stupid questions related to not knowing Javascript.
Sometimes instead of using
<%= %> to print a variable I use
<% print() %>, if it's in the middle of another block of code (an if for example). However, I don't know how to create a link that prints something after the passage has been rendered. Something like this:
<a id="link">Link</a>
<%
$(function () {
$('#link').click(function () {
variable = 1;
print(variable);
});
});
%>
When I click this link, statements like
variable=1 are executed,
but print() does nothing. I have some fuzzy notion that this is because the passage has already finished rendering, but I have zero understanding of why and how to go forward. Any suggestion?
Thanks!
PS: porting the game to Snowman has been more than worth it. The Harlowe version literally killed my mobile browser, while the Snowman version feels like a snappy web app. Now I hope that I get to finally end writing the story and release it, and people can reuse my code if they like.
Comments
An example of one of those events being triggered can been seen on line 383 of the show() function, which is triggering the 'showpassage:after' event you may be interested in.
The following example show how to monitor for that event in the current Passage, it uses the jQuery one() function to associate an anonymous function to be called when the event fires. It includes your link element and on-click handler except I have changed it to output to the console instead for demonstration purposes.
For example: TIP: Enabling strict mode will catch errors like that for you. For example: All in all, I'd suggest staying away from auto-globals as much as possible.
The print() function is part of the template system, so that's exactly why it doesn't work in this instance—template rendering is long complete by the time the player activates the link.
You need to create a target element, which you may add your content to. For example: That will add the text I'm a little teapot! to the #link-target element when the link is activated.
Ideally, you'll probably want to leverage Snowman's renderer, so that you may use markup. Happily, if you're using Snowman v1.3.0, you can. For example:
FYI. While greyelf is entirely correct, Snowman's in-template ready function provides the exact same functionality without requiring users to manually handle the showpassage:after event.
In other words, as long as your code is rendered by Snowman's template renderer—i.e. it's within <%…%> & <%=…%>—then the following code from greyelf's example: Could be written more simply like so: