Coding

All things computer programming related...

Linking to passages by URL

- Posted in Coding by

Put the following code into the Javascript section of your Twine project:

/* Anchor Link to Passage - Start */
if ("onhashchange" in window) {  // event supported
    window.onhashchange = function () {
            hashChanged();
    };
} else {  // event not supported
    window.setInterval(function () {
            if (window.location.hash != setup.storedHash) {
                    hashChanged();
            }
    }, 100);
}
function hashChanged() {
    if (Engine.isIdle()) {
            if (window.location.hash && (setup.storedHash != window.location.hash)) {
                    setup.storedHash = window.location.hash;
                    var anchor = decodeURI(window.location.hash.substring(1));
                    if (Story.has(anchor) && (passage() !== anchor)) {
                            Engine.play(anchor);
                    }
            } else {
                    // Comment out the following line of code if you don't want the
                    // anchor link of the current passage displayed in the URL bar.
                    window.location.hash = encodeURI(passage());
            }
            // Comment out the following line of code if you don't want the
            // title of the page set to the passage name.
            document.title = passage();
    } else {
            setTimeout(hashChanged, 100);
    }
}
$(document).on(':passageend', function () { hashChanged(); });
/* Anchor Link to Passage - End */

Next, put the following into a header passage, but feel free to restrict its use with passage tags.

Iterate over passages to filter them

- Posted in Coding by
setup.getPassageNames = function() {
            const excludedNames = ['StoryInit', 'Main']; 
            const excludedTags = ['widget', 'nonono']; 
            const passageNames = [];
            
            $('tw-passagedata').each(function() {
                const name = $(this).attr('name');
                const tags = $(this).attr('tags');
                let safeTags = true;
                
                    if (excludedNames.indexOf(name) === -1) {
                      
                      for (let i = 0; i < excludedTags.length; i++) {
                        if (tags.includes(excludedTags[i])) {
                            safeTags = false;
                            break;
                        }
                      }
                      
                      if (safeTags) {
                        passageNames.push(name);
                      }  
                }
            });
        
            return passageNames; 
        };
        
        
    <<set _possiblePlaces = setup.getPassageNames()>>
    <<set _randomPassage = _possiblePlaces.random()>>

Primary Twine story formats and essential tooling

- Posted in Coding by

Primary Twine story formats can be downloaded with this link, or with this one. Here is another download link.

And of course I must link to the three most important resources (ChapelR's Tweego, Chris Klimas' Twine, and Visual Studio Code. These are bundled together and can be downloaded here and here and here.

Hiev's browser storage tool is a must-have. The same author produced a nice collection of sample code, which I make downloadable here.

Here is Gwentastic's Twee File Splitter, and here is another download link. I've also backed it up to my OpenDrive and it's located here on my Onedrive account.

Finally, my go-to SugarCube template for getting new projects initiated.

Tweego compiler installer for Windows

- Posted in Coding by

Here is the download link for the Tweego compiler for Windows. It can also be downloaded here and here. Here is my template to hit the ground running with Twee3/Tweego dev. However, I recommend that, instead, you install Tweego for Windows using ChapelR's installer, and then download the much slimmer template I made here.

Remove side bar in Sugarcube 2

- Posted in Coding by

Add the following to the javascript portion of your twee file:

$('#ui-bar').remove(); 
$(document.head).find('#style-ui-bar').remove();

Dev notes for Twine story entitled Dorn

- Posted in Coding by
dev.tw
        passworded access to Developer Notes
dorn_head_foot.tw
        contains header and footer passage 
          for use with hbar and bbar tags\
dorn_intro.tw
        contains passages comprising the Intro to the game/story
dorn_2.tw
        contains ?# passages that form the beginning of this hyperfic
dorn_last.tw
        contains scripts and special passages
        contains code for the playtime macro
        contains code for the fade-in macro
        contains code for hashing to enable passwording
        contains code to implement [nosave] tag for passages
        contains Config data:
          maxSlotSaves = 3
          maxStates = 50
          saves.isAllowed conditions
        contains code to check for [noreturn] tag, for menuing
        contains code to add visits to passage(s) programmatically 
               during initialization (for testing)
        contains code in PassageReady to track total number of passage visits

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>>
Page 4 of 11