Function on setup object to get passage's wikified text

- Posted in Coding by
setup.getWikifiedText = function(passageName){

      if(Story.has(passageName)){
          var passage = Story.get(passageName);
          return "<wiki>" + passage.text + "</wiki>";
      }else{
       return ""; 
      }
 };

Ways to use a single Twine passage repeatedly

- Posted in Coding by

We can use a passage repeatedly by incrementing a story variable that is used with a <<switch>> to select what the passage should display.

The way I do this is to define a widget like so (be sure the passage in which you define the following widget has a widget tag:

<<widget lb>>
<<goto $p>>
<</widget>>

Then, in special passage PassageDone I put this code:

<<set $p to passage()>>

The above sets story variable $p to the name of the most recently rendered passage.

Using the above, we can then do the following in the passage we want to repeatedly use (assume the passage name is MyPsg):

<<switch visited()>>
<<case 1>>
     Case 1 text goes <<link here>><<lb>><</link>>.
<<case 2>>
     Case 2 text goes <<link here>><<lb>><</link>>.
<<case 3>>
     Case 3 text goes <<link here>><<lb>><</link>>.
<<default>>
     Text for after the third passage visit.
<</switch>>

Here's another way you can accomplish this (I prefer the method used above):

<span id="start">
This text will be displayed when the passage is first loaded.
</span>

<<set _count to 0>>

<<link "Continue">>
  <<set _count++>>
  <<replace "#start">>
    <<switch _count>>
      <<case 1>>
      This text will be displayed when the "Continue" Link is clicked the first time.
      <<case 2>>
      This text will be displayed when the "Continue" Link is clicked the second time.
      <<case 3>>
      This text will be displayed when the "Continue" Link is clicked the third time.
    <</switch>>
  <</replace>>
<</link>>

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.