Howdy, Stranger!

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

Accessing Passage Contents Via JavaScript in Sugarcane

I'm not sure if this is possible, but I've been playing around with adding a sidebar to the right of my passage through appending a div element, and it seems to work fine.
$(document.body).append('<div id="rightSidebar">/*Contents here*/</div>');

While I could probably just write HTML in there, I'd like to be able to add to it using passages just for ease of use. I'm still a massive novice at this stuff, so if it's impossible that's fine, just thought I'd ask. Thanks again everyone :)

Comments

  • edited July 2016
    I've got it working using
    $(document.body).append('<div id="rightSidebar"></div>');
    
    var passageText = tale.get('Test').text;
    document.getElementById("rightSidebar").innerHTML = passageText;
    

    where 'Test' is the name of the passage I'm using, seems to work fine. If there's a better way to do this let me know, otherwise I'll stick with this :) I'd love to get macros working too, dunno if that's possible though.
  • To render a passage into an existing element on the page you want the setPageElement() function:
    setPageElement(IdOrElement , PassageName [, OptionalDefaultText ])
    
    For example:
    setPageElement('rightSidebar', 'Test');
    
  • edited July 2016
    I use the GSAP TextPlugin to do this since it lets me replace the text with new words whenever I want. If you want to do macros just use TME's approach above. If you want to refresh the div to update variables you'd need to also connect the div to the postrender function.
  • Thanks TME that works perfectly. On the off chance someone reads this that knows something about Javascript, it's not a huge deal but I have a music player that runs off of Javascript that gives a script error the first time the page is run but doesn't do that after. It uses Jquery and Jquery-UI, and it gives me a
    volume.slider is undefined
    
    error, which I'm assuming means it's trying to run before Jquery UI is loaded? Any idea on how to fix that?
  • You could run it at a very low volume first time the story is run and disable javascript error popups.
  • Claretta wrote: »
    You could run it at a very low volume first time the story is run and disable javascript error popups.
    I think I fixed it, as far as I know Twine runs scripts in alphabetical order so I just moved the script loading for that one to another name. Seems to work now.

  • MBFtrace wrote: »
    ... as far as I know Twine runs scripts in alphabetical order...
    Twine does not run the script tagged passage, that is handled by the Story Format selected by the Author and it is up to the story format to decide what order it processes the script passages in.

    In the case of Sugarcane that order is alphabetical by passage title, other story formats may do it in the order the passages were created in the Twine application.
Sign In or Register to comment.