Howdy, Stranger!

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

How to do something when the passage is left?

I need to stop playing sound when the user leaves the passage, but there may be more tasks which should be executed when the passage is left, regardless of the way it was left (not only by clicking links, but all forms of goto as well). I think there should be some passage.OnExit() function? How should the code of the exit handler look?

Comments

  • Again you need to state which story format you are using when you ask a question, because you can't rely on the person giving an answer remembering the story format stated in one of your previous questions.

    I don't believe Sugarcane has a passage.OnExit() function but if you look at the passage render source code you will see that it does have the ability to supply a prerender (and postrender) handler which you may be able to use to do what you want (depending on exactly what you want to do) before the next passage is shown to the Reader.

    The Prerender and postrender in Twine article may help you understand Sugarcane's prerender and postrender handlers.
  • Does javascript for such basic things really differ for different story formats? Yes, I use Sugarcane.
    Prerender and postrender are called when the passage is entered, not left!
    And I am not an expert in javascript, so sending me to study some large source code (which probably doesn't describe passage leaving BTW) is not a good idea. I'm looking for something like "insert this code to your script passage, replacing this line with your function call".
    I can think a workaround like starting setInterval(check(), 100), where check() will check if the current passage name have changed and run my own "onExit" for the previous passage, but this is a very ugly (and probably CPU-loading) solution. I hardly can believe there is no normal onExit handler.
  • edited February 2016
    yun wrote: »
    Does javascript for such basic things really differ for different story formats?
    Considering that all story formats, other than the Twine 1 vanilla story formats, are separate applications written by different people and there are no standards for them to follow. Yes.

    yun wrote: »
    Prerender and postrender are called when the passage is entered, not left!
    In your case, a prerender task should work for stopping playback of your audio—since it would be called before the incoming passage is rendered, let alone displayed (the outgoing passage will still be onscreen at this point).

    For example:
    prerender['runBeforeTheNextPassage'] = function () {
    	/* Stop audio playback. */
    };
    
    As a specific example, showing how to use a Wikifer call to run macros:
    prerender['runBeforeTheNextPassage'] = function () {
    	new Wikifier(document.createElement('div'), '<<stopallsounds>>')
    };
    

    If you really need to run a callback before anything is done for the incoming passage, then that is possible by wrapping the <History>.display() method. However, I suggest trying the prerender task first.
  • yun wrote: »
    And I am not an expert in javascript ...
    I have no way of knowing your level of javascript expertise, but your last couple of questions have mostly been about modifying javascript code and they have included javascript code examples that you wrote. So it made sense to me that you must have at least some experience.
    yun wrote: »
    Prerender and postrender are called when the passage is entered, not left!
    My earlier post stated that both of those events were "before the next passage is shown".
    yun wrote: »
    looking for something like "insert this code to your script passage"
    Which is why I included a link to an article that explained how to use the Sugarcane's prerender and postrender handlers.
    yun wrote: »
    I hardly can believe there is no normal onExit handler.
    Two people have now told you that there isn't one, and TheMadExile definitely knows what he is talking about even if I may not.
  • prerender['runBeforeTheNextPassage'] = function () {
    	new Wikifier(document.createElement('div'), '<<stopallsounds>>')
    };
    
    No, this doesn't work well. When I leave the passage in the middle of the sound, the sound plays to the end and only then stops. If I write <<stopallsound>> in the beginning of the next passage manually, the sound stops immediately (which is what I want, but adding <<stopallsound>> line into every passage is not a good idea).

    As for my javascript experience, it's mostly based on my knowledge of C++ - that is, I understand the syntax, but unfamiliar with many javascript-specific concepts. I managed to adapt several scripts for my tasks, but it was mostly trial and error method ;)
  • edited February 2016
    yun wrote: »
    prerender['runBeforeTheNextPassage'] = function () {
    	new Wikifier(document.createElement('div'), '<<stopallsounds>>')
    };
    
    No, this doesn't work well. When I leave the passage in the middle of the sound, the sound plays to the end and only then stops. If I write <<stopallsound>> in the beginning of the next passage manually, the sound stops immediately (which is what I want, but adding <<stopallsound>> line into every passage is not a good idea).
    Sadly, I misspelled the name of the macro ('<<stopallsounds>>' versus the correct '<<stopallsound>>'; I accidentally pluralized the name), so that's why it's not stopping playback. Correct the name of the macro within the task and it will work.
  • yun wrote: »
    prerender['runBeforeTheNextPassage'] = function () {
    	new Wikifier(document.createElement('div'), '<<stopallsounds>>')
    };
    
    No, this doesn't work well. When I leave the passage in the middle of the sound, the sound plays to the end and only then stops. If I write <<stopallsound>> in the beginning of the next passage manually, the sound stops immediately (which is what I want, but adding <<stopallsound>> line into every passage is not a good idea).
    Sadly, I misspelled the name of the macro ('<<stopallsounds>>' versus the correct '<<stopallsound>>'; I accidentally pluralized the name), so that's why it's not stopping playback. Correct the name of the macro within the task and it will work.
    It does. Thanks!
    Actually, I came to conclusion that for some passage it is needed and for some it is not. So, they can be marked with "stopsound" tag, and the code will be
    function InArray (array, value)
      {for (var i in array) {if(array[i]==value) return true;}
       return false; 
      };
    
    prerender['runBeforeTheNextPassage'] = function () {
    	if (InArray(state.history[0].passage.tags,"stopsound")) new Wikifier(document.createElement('div'), '<<stopallsound>>');
    };
    
Sign In or Register to comment.