Howdy, Stranger!

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

Repeating a line of code until a condition is met?

With Sugarcube 2, I'm using a variable to tweak a countdown timer, but the whole nature of a variable is being thrown about due to my limited knowledge of how the system I'm configuring might work.

Basically, I want something like this.

<<set $var to 10>>

Countdown: <span id="countdown">$var</span> seconds remaining!
<<silently>>
<<timed 1s>><<replace "#countdown">><<set $var -= 1>>$var<</replace>>

if $var is NOT zero, repeat the above line
if $var is zero, <<goto "Passage1">>

<</timed>>
<</silently>>

Instead, for ten seconds, I'm putting in something like...

<<timed 1s>><<replace "#countdown">><<set $var -= 1>>$var<</replace>>
<<next>><<replace "#countdown">><<set $var -= 1>>$var<</replace>>
<<next>><<replace "#countdown">><<set $var -= 1>>$var<</replace>>
<<next>><<replace "#countdown">><<set $var -= 1>>$var<</replace>>
<<next>><<replace "#countdown">><<set $var -= 1>>$var<</replace>>
<<next>><<replace "#countdown">><<set $var -= 1>>$var<</replace>>
<<next>><<replace "#countdown">><<set $var -= 1>>$var<</replace>>
<<next>><<replace "#countdown">><<set $var -= 1>>$var<</replace>>
<<next>><<replace "#countdown">><<set $var -= 1>>$var<</replace>>
<<next>><<replace "#countdown">>Too Late!<</replace>>
<</timed>>

See what I mean?

Any help would be greatly appreciated.

Comments

  • Well you could build a string using a <<for>> loop and print that instead, but Im not sure you are gaining anything by doing so.

    note: I moved the <<set>> from inside the <<replace>> to just before it because all you really want to be inserted into the target span is the result.
    <<set $var to 10>>
    
    Countdown: <span id="countdown">$var</span> seconds remaining!
    
    <<set $command to "<<timed 1s>>">>
    <<for $i to 0; $i lt $var; $i++>>
    	<<set $command += "<<set $var -= 1>><<replace \"#countdown\">>$var<</replace>><<next>>">>
    <</for>>
    <<set $command += "<<replace \"#countdown\">>Too Late!<</replace>><</timed>>">>
    <<print $command>>
    
  • That looks like an awfully complicated bit of code, but it works! Thank you very much.
Sign In or Register to comment.