0 votes
by (630 points)

When I transition from Passage A to Passage C in my story, I would like to include a Passage B that is completely automated in animation, etc, with an auto-advance to Passage C at the end via the (go-to:) macro.

Only problem is, if I "Undo" using the Sidebar in Passage C, it sends me on a loop to Passage B and back to C because the (go-to:) is automated.

I would like to make the Undo icon on Passage C link to Passage A instead of serving as a true Undo.

My clunky understanding of HTML / Harlowe resulted in this code:

<tw-sidebar>
	<tw-link tabindex="0" passage-name="PASSAGE A" data-raw="">↶</tw-link>
	</tw-sidebar>

This results in an addition undo arrow icon next to the standard undo icon.  How can I modify the standard icon to become a link, for Passage C only?

I'm on Mac OS Sierra, Twine 2.1.3, Harlow 2.0.1

1 Answer

+1 vote
by (63.1k points)
selected by
 
Best answer

Try putting this in 'Passage C':

{
<script>
$(document).ready(function () {
    var $undo = $('tw-icon.undo');    
    var $link = $(document.createElement('tw-link'));

    $undo.off('click'); // remove click event handlers from undo button

    $link // create invisible link
        .attr('passage-name', 'Passage A') // your passage name here
        .addClass('custom-undo')
        .append('hidden link')
        .appendTo('tw-sidebar')
        .css('display', 'none');
	
    $undo // bind undo button to our link
        .on('click', function (e) {
            e.preventDefault();
            $link.trigger('click');
        });
});
</script>
}

This code strips the normal event handlers from the undo button, then creates an invisible link to 'Passage A'.  The last bit of code makes it so that the undo button fires our custom link element.

A few things:

  1. Harlowe is kind of hard to mess with like this because it's difficult to get at it's internals via pure JS, which is why I wound up creating an invisible link.  There are probably better ways to do this, so be on the lookout for some better answers, and don't use this solution as a general way of overriding other links.
  2. Test the undo button thoroughly.  I checked to make sure it's default event handlers were restored following this script, and it appears that everything returns to normal on the next passage from my simple tests.  You're going to want to double check this.
by (630 points)

Perfect.  Thank you so much for putting together this code.

For future searchers of this forum, this worked very well, but I needed to remove the Javascript comments (anything after //)  to avoid syntax errors in Twine.

I also needed to turn back time on a few variables to manually recreate the "undo" state since "undo" was turned off temporarily to make this work.  I'm sure there's a more elegant code solution, but my Twine solution was to create a "Passage Undo".

I modified Chapel's code to point to "Passage Undo".  "Passage Undo" had code like this to "undo" the Page Number variable and bridge to the previous passage:

(set: $PageNumber to $PageNumber - 1)
(go-to: "PASSAGE A")

While playing, the transition was instant between C and A, as if Passage Undo never existed.  

Thanks again Chapel!

by (159k points)
edited by

Note to others:

Undo doesn't just transition you to the previous passage and undo variables changes.

1. Undo winds the History system backwards one step which means:

a. The current passage (and all it's state changes) is removed from History as if the passage was never visited in the first place, in the OP's example this means Passage C was never seen.

b. The previous passage is also removed from History and re-executed, in the OP's example this means Passage B was only visited one time.

History Path before Undo was used:
	Passage A -> Passage B -> Passage C

History Path after Undo was used:
	Passage A -> Passage B

2. Chapel's solution transitions the Reader/Player forward to Passage A, this means:

a. Passage A was visited twice.

b. Passage B & C were visited once.

c. All the state changes done in Passages B & C are not undone.

History Path before 'Custom' Undo was used:
	Passage A -> Passage B -> Passage C

History Path after 'Custom' Undo was used:
	Passage A -> Passage B -> Passage C -> Passage A

@Chapel: The 'side-bar' and its children are actually part of the current passage, so they are recreated each time passage traversal occurs.

by (63.1k points)

Chapel's solution transitions the Reader/Player forward to Passage A, this means... 

 That's a good point, I didn't even think about the History system. 

@Chapel: The 'side-bar' and its children are actually part of the current passage, so they are recreated each time passage traversal occurs.

Thanks. I thought so, but I wasn't certain. 

...