Setting variables as you follow a link

415 views
Skip to first unread message

Stijn Mostinckx

unread,
Aug 7, 2012, 11:24:30 AM8/7/12
to twee...@googlegroups.com
Hi,

I have just spent some time twiddling with Twee.  One thing I found frustrating is that I sometimes found myself creating intermediate pages whose sole task was to set a few variables and then use <<display>> to show a core page.  These intermediate pages all need to have different titles (which are shown in the Jonah story format) while the titles of the pages included via <<display>> are not.

What I wanted to be able to do was store/update some variables, but always proceed to the following page.  This is not the behavior you find in the example stories (where different actions lead to different outcome pages), but it can be useful if you want to progress through a fixed sequence of passages while memorizing information along the way.

Therefore I developed the included plugin file which provides 2 macro's <<linkAndSet>> and <<chooseAndSet>>. Both macro's expect at least 2 parameters: the passage to link to, a JSON object that enumerates the variables to set, and optionally the link text to use. 

Note: The scripts use eval("(" + theJSONString + ")"), rather than JSON.parse(theJSONString).  The use of "eval" has some repercussions w.r.t. security and stability (an incorrect script could wreak havoc and potentially crash your story), but it also provides you with the ability to update variables, instead of just assigning constants to them.

The included example file implements the class quiz in Morrowind, which is a generic example of why I would like this functionality in my stories.  You can find an explanation of how the quiz works at the URL below.   


regards,

Stijn Mostinckx

P.S. I have also included a version in which the scripts have been minified using uglifyjs.  This should save you about 2000 bytes of space and thus loadtime.

LinkChooseAndSet.tw
LinkChooseAndSet.min.tw
ClassQuiz.tw
ClassQuiz.html

Stijn Mostinckx

unread,
Aug 9, 2012, 11:22:48 AM8/9/12
to twee...@googlegroups.com
Hi,

After a more in-depth look at the Jonah code base, I decided to use Wikifier.parse instead of passing JSON objects.  This implies that you can now use any experssion you would normally put in a <<set>> macro (and you can still set multiple variables, if you separate the expressions using semi-colons).

As before, the plugin file, a minified version, a usage example and the compiled version are attached.

regards,

Stijn Mostinckx

P.S. I have become aware that use of my macro makes it impossible to replay a story sequence based on dot-separated passage ids.  This is what I will be looking into next.


LinkChooseAndSet.tw
LinkChooseAndSet.min.tw
ClassQuiz.tw
ClassQuiz.html

Abe Kinney

unread,
Aug 9, 2012, 5:33:17 PM8/9/12
to twee...@googlegroups.com
This is perfect for the project I'm working on. Thanks!

Stijn Mostinckx

unread,
Aug 10, 2012, 11:30:36 AM8/10/12
to twee...@googlegroups.com
Hi,

After having explored how the history works, I have adapted the <<linkAndSet>> and <<chooseAndSet>> macro's so that they keep track of which particular assignments have been carried out.  This implies adding a field to the history (the id that will be used in the history URL) and keeping this up to date as you move between pages.  This requires adapting History.prototype.save and History.prototype.display.  I've kept these changes in a separate plugin, which does not change the observable behavior of the Jonah story format.

Subsequently, I have adapted the plugin that provides <<linkAndSet>> and <<chooseAndSet>> so that they manipulate this added field whenever they are used to navigate to another page.  This ensures that their assignments can be played back.  In the new example file, you are able to open one of the bookmark links in a new browser window (or bookmark it obviously) and you would be able to continue the questionnaire later on.

As before, documented plugin files, minified versions, a usage example and the compiled version are attached.

regards,

Stijn Mostinckx

ClassQuiz.html
ClassQuiz.tw
LinkChooseAndSet.min.tw
LinkChooseAndSet.tw
RicherHistory.min.tw
RicherHistory.tw

Stijn Mostinckx

unread,
Aug 10, 2012, 2:12:59 PM8/10/12
to twee...@googlegroups.com
Hi,

The plugin file specified below alters the behavior of the <<choice>> macro, such that it not only replaces alternatives you didn't select with a <div>, but so that it also replaces the alternative you did select with a standard link. In the case of regular <<choice>> macro's, this ensures that no time is spent to repeat unneccesary traversal code.

In combination with the <<chooseAndSet>>, this behavior ensures that repeatedly clicking on an already selected option will not repeatedly update a value.  A corollary of this is that <<chooseAndSet>> is useful even when only a single option is provided.  In general, <<chooseAndSet>> should probably be preferred over <<linkAndSet>> whenever the associated assignments should not be repeated unless the reader rewinds to the passage where the choice|link is offered. 

The plugin provided also ensures that the expanded text is correctly written when a choice has been made.  At least on my system, this did not occur with the default macro.

Attached is the new documented RobustChoice plugin, and a minified version.  A compiled version of the running example is included as well, which uses the plugins provided above, in addition to the robust choice.

regards,

Stijn Mostinckx
ClassQuiz.robust.html
RobustChoice.min.tw
RobustChoice.tw

Katie Yuen

unread,
May 16, 2013, 4:40:26 PM5/16/13
to twee...@googlegroups.com
Just dropped by to say, thanks so much for this! I've been looking everywhere for something like this and yours is the most concise and easiest to implement. For anyone wishing to make variable changes within a link, this is the solution! 

Leon Arnott

unread,
May 17, 2013, 1:53:03 AM5/17/13
to twee...@googlegroups.com
This <<linkAndSet>> macro is useful, but it has a number of interface niggles that I don't really like. It has a forced camelCased name, counter to the trend of the built-in macros; the link title text and passage destination are in reverse order to the standard link syntax, replicating the blunder of the <<choice>> macro. And, the variable set code is required to be inside apostrophes, which is not just a somewhat unintuitive syntactic requirement, but I worry will cause unexpected problems for people who attempt to set string values inside them, not knowing that you can't nest similar apostrophe pairs.

I'm thinking of making an alternative script, one which extends the link syntax itself rather than being a separate macro.

Tobias Beer

unread,
Oct 2, 2013, 2:19:05 AM10/2/13
to twee...@googlegroups.com
Hi,

I tested your html a bit and noticed that rewinding two or more passages at once fails. Whenever you click on the next passage you'll end up with an "undefined" output.

Is doing that (rewinding 2 or more at once) a general no-no or is that something that isn't quite handled right? Would the twine core take care of all the rewinding or is it that macros being used need to take care of that when doing things differently?

- tobias.
Reply all
Reply to author
Forward
0 new messages