I can't figure out how to get code put into the Edit Story JavaScript text box to do anything. Can anyone provide something simple I can paste into there to get me started on the right track? For example maybe some code to print('tick') when passages are rendered? I've been mostly using Snowman, so I've been trying to make a showpassage:after event handler, but I haven't had any luck.
[Edit: Appended [Snowman] to the thread title to avoid confusion.]
Comments
_startPassage (this passage is flagged as the start passage for variable initialization): _turnEvents (a passage to be triggered at the start of all regular passages): StoryStart: This seems to work, but there's the tedious aspect of pasting the line of code to render() the _turnEvents into the start of every story passage textbox. I first tried implementing it in a "showpassage" event listener defined in _startPassage, but it had the problem of seemingly adding duplicate listeners if the browser back arrowed to the _startPassage.
[EDIT: On second thought it's probably not a bad thing to specifically mark (with a line of code) which passages I want to trigger the _turnEvents in. For example, I wouldn't want to have it triggered in passages that only exist to be embedded and rendered inside other passages. So perhaps the code above is actually a decent solution for now. ]
One possibility is to replace the passage source after it's rendered once. Here's _startPassage again with that change (it's the last line): Another option is to replace _startPassage's id in the first element of the window.history array with the passage id you actually want to be the start of the story: Also here's an example of the _turnEvents passage where it counts turns on passages you've tagged (in the editor) with "count-turn".
_turnEvents:
Another simple solution is to edit the generated HTML file and add your own <script> tag at the end of the <body> tag.
Snowman 2 uses jQuery's $.event.trigger() feature to generate custom events that the Author is able to listen for. Some of the events available are: hidepassage; showpassage; showpassage:after; checkpoint; save; restore; restorefailed; restore:after
The closest thing to an event system in Harlowe I personally was able to find was the newEnchantmentMacroFns function, but I don't know how Leon (the creator if Harlowe) wants the Author to use this feature so I suggest asking him.
Browse to /Twine 2.0/storyformats/Snowman/ and backup format.js. Then in your text editor ctrl-F for "twine-javascript". That should bring you to this bit of code: Change it to:
[EDIT: actually, it looks like this was addressed officially with this commit: https://bitbucket.org/klembot/snowman-2/commits/7ac878c70321bf313810eba60f79695808abd9e0 . I'll switch to that instead of the modification I made. ]
No, but you can do a similar thing by doing the following:
1. Open the HTML file generated by the Publish to File feature in a text editor
2. Search for "</body>". (without the double quotes)
3. Add the following code just before the "</body>" text. NOTE: You could do a similar edit in the Harlowe/format.js file instead but I would advise against it as it is easy to get wrong and end up with a broken header file.
The new version of the Harlowe story format that comes with Twine 2.0.1 processes the special Stylesheet and Javascript passages, so there should be no need to edit the generated HTML file if you upgrade to 2.0.1.