0 votes
asked by (490 points)

Hello,

I'm trying to build a kind of engine where, instead of jumping from one passage to another, the game loops through the same engine passage, embedding content from other passages.

So the engine prints, through story.render, certain passages. Those passages contain option links. When you click one link, some variables are changed and the engine prints another passage.

Where's the problem? What I asked in a question posted before. When the engine passage renders the first content passage, the links inside that passage, with code inside <% ... %>, do work. But when you click on a link and a second passage is rendered, the links inside that passage don't work. For example, a link like this:

<% $(function () { 
	$('#id').click(function () {
		s.variable="value";
		$('#passage').append(story.render("main"));
	});  
}); %>

When you click on #id, it changes a variable and appends the engine passage ('main') to #passage. The engine, in turn, embeds other passages through story.render. This works only the first time that I call the engine. The second, it doesn't work.

I guess this is perhaps related to this Snowman feature:

> $, which acts like jQuery's $ method but with one exception. If you pass it a single function, this function is run when the passage appears onscreen, with it bound to the passage DOM element.

The solution in the linked question was using <script> instead of <% %>. That works initially, but it seems to create other problems. For example, I added some HTML structure to the engine passage and it spat a syntax error.

I have uploaded a test to philome.la.

Do you think there is any way of working around this Snowman behaviour? Thanks!

1 Answer

0 votes
answered by (55.6k points)
selected by
 
Best answer

I imagine one way to get around this if the problem is indeed related to the ready function would be to use a delegated handler. 

$(document).one('click', '#id', function () {...});

A little more overhead but I'd imagine it's negligible. This also makes it so you don't need to use the ready function at all. 

commented by (490 points)
Thanks! This works fine as far as I can tell from the first tests.
Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...