Howdy, Stranger!

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

Sugarcube Javascript Link to Passage

I have this in a link:
state.display("foo", this);
I think this doesn't work because I am using SugarCube. I am having difficulty finding any help in the SugarCube documentation for what the changes to this syntax are that will make this work to go to "foo."

Comments

  • There are no changes to the syntax.  The formal parameters to the state.display() method have not changed in SugarCube.

    I assume you're trying to use that code snippet in the middle of a rendering passage?  If so, that won't work (in any header).

    What, specifically, are you attempting to do?
  • I'm trying to figure out if there is a way to transition to another passage via a javascript function (not a macro). I want to pass some custom functionality on a particular link, and I don't want to write a macro for every time there is an ad hoc thing, so I was looking to just run some custom functions on the passage, and then link to another passage on the same click. Something like this (replace state.display("foo",this); with what actually works):
    <<click "text">><<script>>customFunction();state.display("foo",this);<</script>><</click>>
  • The <<click>> macro has a forwarding argument for this exact situation.

    <<click "link_text" "passage_name">><<script>>customFunction();<</script>><</click>>
  • But I want to conditionally send you to a different passage depending on other variables.

    There must be a javascript function to send you to another passage, because the whole thing operates on javascript. I just can't figure out what the exact syntax is, and no errors come up in the console due to the way Twine abstracts passage-level js...
  • I'm not sure why you would need to delve into javascript for the link forwarding part of this. Why not just decide on the passage and then send the reader there using the syntax that TheMadExile gave you?
    <<if $drunk eq true>><<set $next to "Drunken Brawl">><<else>><<set $next to "Peaceful Night">><</if>>
    <<click "Continue" $next>><<script>>customFunction();<</script>><</click>>
    If you do need to use javascript, state.display("passage name") will work, as long as you do it within a click or some other event that will occur after the passage is rendered (again, as TheMadExile said).
  • The reason I'm not using the <<if>> syntax is because it only works in and of itself on things which are done at passage render. I am using a form of cycling link to change user selection in the middle of an active passage. So, the user sets variables within the same passage the link to the next variable passage is in.

    There's a lot of stuff going on, and I've already embedded a bunch of my story in the file, so I'd rather not share the entire source code.

    In the following example, vars['foo'] is just my shorthand for state.active.display('foo'). There are links that the user can click in the passage that, instead of taking them to another passage, run like a "cyclingLink" (it's a custom variant of that macro, but very similar) and set variables in doing so. The cycling links set variables called pageVar1 and pageVar2... again, this happens during an existing, active passage, so wiki syntax won't capture any changes made here.

    Depending on what values the user ended the cycling links on, the link to the next passage should go to a different place.

    I could do this by having my cycling links also hide and reveal different links with CSS (because I am using a custom macro to do cycling links that can also initiate js scripts on click), but this is tedious and messy. So, I'm doing this instead:

    <<click "...">><<script>>
    if(vars['pageVar1'] == 1 &amp;&amp; vars['pageVar2'] == 1) {
    state.display("foo");
    }
    if(vars['pageVar1'] == 1 &amp;&amp; vars['pageVar2'] == 2) {
    state.display("foo2");
    }
    etc...
    };
    <</script>><</click>>
    In any case, the problem I had before was a syntax error on my end... I'd be embarrassed, but how can I see what errors there are? Some syntax errors print out that warning at passage render, but others don't... and when they don't, they also don't trigger a warning when invoked either, just nothing happens at all. This makes finding bugs like that very difficult. Anyway, it works now.

  • Ah, alright.  Yes, using state.display() inside of <<click>> works since it occurs outside of passage render.

    As to how to see errors in code that's outside passage rendering, normally you'd simply check the console log (virtually all major browsers include developer tools now).  If it's some kind of runtime error, rather than simply buggy but legal code, you should see an error in the log.

    That won't work here though, since <<click>> discards errors.  I should be able to make it propagate errors to the console log.  I'll put that on my TODO list.
  • Glad it's working. As for error-checking a bit of code, you might also get some mileage out of pasting your code into javascript lint, e.g. http://www.javascriptlint.com/online_lint.php if you can't see anything in the console.
  • Okay.  A new SugarCube release has been published which addresses this.  The <<button>> & <<click>> macros will now propagate errors within their contents.
  • TheMadExile wrote:

    Okay.  A new SugarCube release has been published which addresses this.  The <<button>> & <<click>> macros will now propagate errors within their contents.


    Wow! Thanks! I had no idea this would prompt a change to the build. I thought it not propagating those errors was set in stone. Now I CAN see the error in the console easily.

    One thing though, does there need to be an alert message for the error? Sometimes there can be errors which don't actually break the game where it would be better to just let the game continue even though there was an error. I know, promoting dirty code isn't the most popular thing to do, but as long as the error is in the console log as it is now, the alert pop up could just get in the way of smooth play in some cases where the error doesn't matter, and won't help a serious developer find the error in the log anyway.
Sign In or Register to comment.