hello all,
I would like to use the animated dice script (a javascript from long ago

) inside my Twine game. If I execute test.html (a plain html structure) everything works as expected (see .zip file), but if I copy paste the html table containing all the code (inside a twine passage) I get an error (see .jpg file). I think this has something to do with globals? but I'm not sure and I'm puzzled how to fix it and get it to work in my game.
I've included all the files (including the error message screen shot). I use twine 1.4.2 in combination with sugarcube-2 (the latest version).
*****
page 2 of my twine game:
<<script type='text/javascript' src='d6.js'>><</script>>
<table width="90%" align="center">
<tr>
<td align="center">
Press button to roll dice:
<<script>>
function check(result) {
document.getElementById("total").textContent = result;
}
D6.dice(2, check);
<</script>>
<span id="total"></span>
</td>
</tr>
</table>
****
I have also tested loading the external js file via a "Config" script file in twine with this syntax (TheMadExile tip):
(function ($) {
$(document.head)
.append('<script type="text/javascript" src="d6.js"></script>');
})(jQuery);
but same error: "bad evaluation: D6 is not defined"
I hope someone can help me out with this?
With kind regards, Jeroen Wolf
Comments
(Emphasis mine) There's your biggest problem right there. That d6.js script appears to be ancient, to say the least. It uses document.write() to overwrite the entire page, which means you cannot use it as-is within any web app, as it overwrites the app—all story formats are web apps, BTW.
That is not the correct syntax for either the <<script>> macro or the <script> HTML tag—you should remove it.
Beyond that. The former won't work without editing d6.js in several places. The latter won't work just put into a passage like that—you'd have to use jQuery to inject it into the page head.
Then you've done something wrong somewhere. That code should cause the script to be loaded, as long as you put it into a script-tagged passage and the file is within the same directory as the compiled HTML file.
----
Anyway, using the head injection code above I was able to get the script, at least partially, running after a small edit to the script and a modification to the way you were attempting to call its D6.dice() method. By "partially" I mean that it didn't break the story format and produced a couple of animated dice on-screen as, I suspect, was intended—I did no testing beyond that.
Change your invocation to something like the following:
Then within the d6.js file itself you need to make the following change:
FIND: (within the D6.dice() function, around line 434) REPLACE WITH:
With kind regards, Jeroen Wolf
As a guess, you probably just didn't rebuild it or loaded an old build, something like that.
my current situation:
Google Chrome: v51.0.2704.103 m
Firefox: v45.2.0
With kind regards,
Jeroen Wolf
If you eventually plan to put this up on a server someplace, then you will be serving it over the network, so this will not be an issue in production.
On the other hand, if you plan to distribute your project as-is, so that players would be opening the files locally, then you'll probably need to inline the animated dice code—i.e. refactor it a bit so that it works when placed in your script section (Twine 1/Twee: script-tagged passage; Twine 2: Story JavaScript).
----
The changes required to inline the code aren't major if you wanted to go that route, consisting of one additional fix to the d6.js code and the addition of a wrapper. In fact, I'll just go ahead and outline them here.
FIND: (around line 59) REPLACE WITH: Basically, you simply add the var keyword before the assignment to whichDie.
And now for the wrapper:
I used twine 1.4.2 in combination with sugarcube-2.
Jeroen