Howdy, Stranger!

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

Does Sugarcube 2 have a "check" function?

I have a variable that counts down from 1000 to 0 in ten seconds. I also have a <<if $var is 0>> condition at the bottom of the page, however that only registers when the page loads in, and $var is at 1000.

Is there a check function or something I can do to make the program say "Hey, $var is now 0, you wanna run that bit of conditional code now?"

Any help would be greatly appreciated.

Edit: I'm aware reloading the page does that, but that would just reload the counter up to 1000, show the thing that should be available only when it's at 0... and basically break everything.

Comments

  • edited October 2015
    I believe doing something like "<<if: $var is 0>>$var" would show it and you can put anything after the arrows like "hey your countdown is now at 0" or "Hey, $var is now 0, you wanna run that bit of conditional code now?" like you said.
  • In simple terms, once the current passage has been displayed it becomes inactive until some external input happens like: a timer event; a keyboard/mouse event; etc.

    Why not add extra code to your background count-down code so that it updates the current page when it reaches zero?
  • edited October 2015
    I have a variable that counts down from 1000 to 0 in ten seconds.
    How are you accomplishing that? I'd wager that adding a check to your countdown is the way to go.
  • I have a variable that counts down from 1000 to 0 in ten seconds.
    How are you accomplishing that? I'd wager that adding a check to your countdown is the way to go.
    greyelf wrote: »
    In simple terms, once the current passage has been displayed it becomes inactive until some external input happens like: a timer event; a keyboard/mouse event; etc.

    Why not add extra code to your background count-down code so that it updates the current page when it reaches zero?

    I'm using a tweaked version of some code I was shown on here, to help with an earlier question. The code I have works exactly as desired, except for the purposes of this question, in which I'm having trouble implementing a "refresh" or a "check" on an if statement.

    <<set $seconds to 10, $cnd to $seconds * 100>>

    Countdown: <span id="countdown">$cnd</span> seconds remaining!

    <<set $command to "<<timed 0.01s>>">>
    <<for $i to 0; $i lt $cnd; $i++>>
    <<set $command += "<<set $cnd -= 1>><<replace \"#countdown\">>$cnd<</replace>><<next>>">>
    <</for>>
    <<set $command += "<<replace \"#countdown\">>Too Late! <<set $check to 1>><</replace>><</timed>>">>
    <<print $command>>

    <if $check is 1>>
    This bit of code will run
    <</if>>

    the variable $check is set to 0 before the code runs. When $cnd reaches 0, $check becomes 1, but the browser still has the <<if $check is 1>> bit of code to not run because it initially read it as a 0.
  • You could just put the code after "Too Late!", as you're doing with the $check set now. Since you're building the <<timed>> call as a string and invoking it via <<print>>, however, that might get hairy if whatever you want to do is involved. An easy workaround would be to use <<display>>.

    For example:
    <<set $seconds to 10, $cnd to $seconds * 100>>
    
    Countdown: <span id="countdown">$cnd</span> seconds remaining!\
    <<silently>>
    <<set $command to '<<timed 0.01s>>'>>
    <<for $i to 0; $i lt $cnd; $i++>>
    <<set $command += '<<set $cnd -= 1>><<replace "#countdown">>$cnd<</replace>><<next>>'>>
    <</for>>
    <<set $command += '<<replace "#countdown">><<display "Too Late">><</replace>><</timed>>'>>
    <<print $command>>
    <</silently>>
    

    And then create a new "Too Late" passage and put everything in there. For example:
    Too Late!
    
    /* This bit of code will run */
    

    PS: Building a 1000 clause <<timed>> is horrific. I suppose that I need to create a repeatable macro in the same vein as <<timed>>.
  • Just a note. The next release of SugarCube 2.x will include the new <<repeat>> and <<stop>> macros.

    You'll be able to use them to simplify the above construct to something like the following:
    <<set $seconds to 10>>\
    Countdown: <span id="countdown"><<= $seconds.toFixed(2)>></span> seconds remaining!\
    <<silently>>
    <<repeat 10ms>>
    	<<set $seconds -= 0.01>>
    	<<if $seconds gte 0>>
    		<<replace "#countdown">><<= $seconds.toFixed(2)>><</replace>>
    	<<else>>
    		<<replace "#countdown">>Too Late!<</replace>>
    		/* This bit of code will run */
    		<<stop>>
    	<</if>>
    <</repeat>>
    <</silently>>
    
Sign In or Register to comment.