Howdy, Stranger!

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

Code for doing something on every link click

Hi everyone,

This post contains some code and a few questions about how to make it more useful.

I recently released a game called Transfer Points, which uses some code that runs on every link click, essentially to control the "AI" (movement of the non-player character). My friend Kat wrote it. I figure it may be of use to others:


<br />// requires jquery<br />postrender.clicky = function( elem ) {<br /> $( elem ).find( &#039;a.internalLink&#039; ).click( function( e ) {<br /> v = state.history[0].variables; // access twine variables through this, e.g. v[&quot;some_var&quot;]<br /> alert(1); //change this to whatever JS code you want, including updating twine vars.<br /> } );<br />}


demo


My questions are these:


1) The code will run on every *link click*. This may not have the intended behavior if, say, you use it in conjunction with the <<replace>> macro and want it to run at the loading of each new *passage*. Is there a way to modify the script with that behavior?


2) It's not ideal to have to write straightforward Twine code through its embedding in JS. I suppose it wouldn't make sense for this to be a macro in the traditional sense, but does anyone have thoughts for how I could turn this into something more native? It seems like it'd want to be a separate passage type, a la "script" or "stylesheet".


Cheers!

Comments

  • Hi there,

    I'm sorry that I'm of no help for what you asked, but I wanted to give a feedback about your game.

    I liked it how the NPC moved as you try to reach the location. As I lack the sense of orientation, I had to look at the map many times, while the NPC was still moving ! I ended up desperate and clicking randomly on locations (this can tell how desperate I was), until I could (hopefully) finish the game. That was an original way of providing emotions. Smooth job !
  • chrisamaphone wrote:

    1) The code will run on every *link click*. This may not have the intended behavior if, say, you use it in conjunction with the <<replace>> macro and want it to run at the loading of each new *passage*. Is there a way to modify the script with that behavior?


    Depending on what your AI code is doing, simply dropping the click event handler might be enough since the prerender/postrender tasks are only run whenever a new passage is rendered (via passage navigation, <<display>> does not trigger them).  For example:

    /* To run your AI before an incoming passage has been rendered. */
    prerender["runAI"] = function (content) {
    /* AI code here */
    };

    /* To run your AI after an incoming passage has been rendered, but before it's displayed. */
    postrender["runAI"] = function (content) {
    /* AI code here */
    };
Sign In or Register to comment.