Implementing passworded content in a Twine work

- Posted in Coding by

It's not terribly difficult to implement passwording in a Twine work. First, you'd drop this into your story javascript passage:

/* hashStr - Start */
window.hashStr = function(txt) {
    var hash = 0, i, chr;
    if (txt.length === 0) return hash;
    for (i = 0; i < txt.length; i++) {
            chr   = txt.charCodeAt(i);
            hash  = ((hash << 5) - hash) + chr;
            hash |= 0; // Convert to 32bit integer
    }
    return hash;
};
/* hashStr - End */

Next, you'd need to go here (or message me and I'll help you) and use the Enter text here textbox to enter the desired password. For example, if your desired password is redrum, the hash will return -934876071 Copy that number to the clipboard, because we'll use it in the next step.

Step 3: Finally, you'll need to put the following code into a Twine passage and specify the number you got in step 2. This is a textbox where users can try to enter the correct password:

You can view the raw text here, online...

The convoluted nature of learning

- Posted in Coding by

I get a bit frustrated that I don't find myself making dazzling progress in coding works of Twine. However, I realized this afternoon while doing a lot of reading on http://intfiction.org, https://twinery.org, https://twinelab.net/twine-resources/#/, and other forums that it is this reading, interspersed with trying out snippets of Twinescript and Javascript, that make learning possible.

I know enough about learning theory to appreciate that sometimes periods of apparent non-productivity are in fact allowing the brain to percolate and make mental connections between concepts — even to have occasional epiphanies.

And so, I'll continue to read Reddit posts that are Twine-related. I'll read them trolling for valuable snippets, but also because they encourage me when I see that many other people have (1) many of the same questions I have, and (2) have creative aspirations in common with my own.

Implementation of menu system in SugarCube 2

- Posted in Coding by

The preferred way to implement menus in SugarCube 2 is to add the following code to your javascript section:

/* Trigger the following code at the start of navigation to a new passage. */
$(document).on(":passagestart", function (event) {
    /* Make sure the current passage doesn't have a "noreturn" tag. */
    if (!tags().includes("noreturn")) {
        /* If it doesn't, then set $return to the current passage name. */
        State.variables.return = passage();
    }
});

And then, tag any menu passages (however deeply nested they may be) with 'noreturn', adding the following link to those menu passages:

<<link "Return" $return>><</link>>