Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Go to passage name stored in a variable?

Hi,

New to Twine, I'm using Snowman in 2.0 because I'm a heavy JavaScript programmer.

I want to to be able to store in a variable what the next passage should be, so that I can reuse a recurring passage. For example, say I want Passage A to happen periodically throughout the story. However, I want to be able to go from passage C -> passage A -> passage F or G -> A -> B or any other combination. I don't want the next passage following passage A to always be the same-- I want to keep track of what should be next in a variable.

This is my attempt, which results in an error.

In the project JavaScript:
document.twine.next_ = '';

document.twine.getNext = function() {
	return document.twine.next_;
}

document.twine.setNext = function(id) {
	document.twine.next_ = id;	
}

In the passage I want to recur:
[[linkToNextPassage-><% document.twine.getNext() %>]]

In the passage immediately before that:
<% document.twine.setNext('test') %>

And I've created a passage named 'test'.

Why doesn't this work? Is there a way to do this differently?

Comments

  • note: The version of Snowman 2 that comes with Twine 2.0.6 is currently broken, so the following was done using Twine 2.0.4 but once Snowman 2 is fixed the following will work in that version as well.

    Snowman allows access to it's state within underscore's <% .. %> and <%= .. %> blocks via the 's' variable, so you don't need to create your own accessor methods to store values.

    The following example consists of three passages (first, second, and third) and is written using Twine notation where a line starting with a double colon indicates a new passage with the text on the same line as the double colon representing the passage's name.
    :: first
    This is the first passage.
    <% s.nextPassage = 'third' %>
    
    [[Next|second]]
    
    :: second
    This is the second passage.
    [[Next|<%= s.nextPassage %>]]
    
    :: third
    This is the third passage
    
    Note the usage of an underscore <% .. %> block when executing a javascript expression (like the assignment of a variable) but the usage of a <%= .. %> block when outputting the result of an expression (like the value of a variable).
Sign In or Register to comment.