Reset: Code Snipets and Source
“So, can I trust you with my source code?”
In the interest of sharing resources with others using Twine to create, here’s the code snippets for all the things that used javascript in Reset:
Color Changing
This is what is used to create the color changing effects. The passage they were placed in was called “Style Changer”. The macro layout here was modified from the Timer script by Stefano Russo (right click to download).
The macros were then called by <<display “Style Changer”>> <<typical>>, <<redgreen>> etc…
<<silently>> <<set $StyleChanger = function() { macros['typical'] = { handler: function(){ document.getElementsByTagName("body")[0].style.backgroundColor="#333"; document.getElementById("passages").style.color="#ddd"; } } macros['redgreen'] = { handler: function(){ document.getElementsByTagName("body")[0].style.backgroundColor="#333"; document.getElementById("passages").style.color="#ddd"; } } macros['cb'] = { handler: function(){ document.getElementsByTagName("body")[0].style.backgroundColor="LightSteelBlue"; document.getElementById("passages").style.color="DarkSlateBlue"; } } macros['badcolor'] = { handler: function(){ document.getElementsByTagName("body")[0].style.backgroundColor="#b38201"; document.getElementById("passages").style.color="#009900"; } } }>> <<print $StyleChanger()>> <<endsilently>>
Prompt Box
Rather than just show off how to create a prompt box, this is the segment in which the result the user types in is actually used immediately to determine which of the color schemes should be displayed. Since the user is given one of the Ishihara plates and asked which number they see, the two common answers are 29(typical) and 70 (red-green deficiency), and I also included a guaranteed answer in case either they saw nothing or entered something other than the two options. This is from the “erozha gnuj” passage.
<<set $numberSeen = prompt('#?')>>Not-you see you. Not-noise. <<if $numberSeen eq 29>>[[Two touch things. Five-and-four touch things.|typical]]<<endif>> <<if $numberSeen eq 70>>[[Five-and-two touch things. No touch things.|redgreen]]<<endif>>
The useful thing to note about this is that you can use a variable collected in a prompt on the very same screen the prompt appears.
Alert Box
For the alerts, I wasn’t able to find a macro to simply display the alert. So I set it to dump the result into a variable which means it has to be evaluated as JS. You can find this in the “Plug it in.” passage.
<<set $alert = alert('Warning: Attaching device could allow unauthorized access to digital as well as biological function.')>>
I think(?) the value $alert will evaluate to is true if the player clicks “Ok” and false if the user escapes/closes the box without responding. But I’m not certain on that.
Confirm Box
Ah, yes. Turning yourself over to Alison’s control. I wanted to use this moment to mimic the normal experience of having to explicitly permit a different user to use administrator or root privileges.
The way this works is that clicking “Ok” sets the value returned from confirm() to true, while clicking “cancel” or “X” sets it to false.
<<set $confirm = confirm('You are about to grant user [Administratrix] ALL access privileges. Are you sure?')>> <<if $confirm eq false>><<display "wrong">><<else>><<display "surrender">><<endif>>
Again, just like with the prompt box, you can use the result of clicking “Ok” or “Cancel” right away. In this case, I have two other passages, and whichever gets displayed depends upon whether the user accepts Alison’s control or not.
Full Source
You can download the full source of Reset here.
With regard to the license, this was a tough one for me. 99% of the work of this lies in the writing, not the code, though the interaction between JS and Twee code makes some obvious things in JS not so obvious in Twine, which is why I wanted to share those insights here. As far as the “code” stuff, I don’t really have any particular attachment to it. Use the JS stuff as you see fit, since much of this is so trivial (from a development standpoint, not necessarily a learning standpoint) that protecting a function call under copyright seems a crass assertion of that power. The CC license is meant to protect the prose, setting, and story arc from unattributed use before I turned it loose.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.