Howdy, Stranger!

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

button click with countdown delay

im using Twine 2.0 with sugarcube 2 and here's the scenario

im using interactive button to open special menu option in my game and its need 5 and 10 success chance to open it.

everytime it's clicked, the button will generate number 1 , 2 , 3 ,4 , 5 by random. if its generate 2 or 3 it will be failed and the success rate will be back to 0 but if its generate any number except 2 or 3 its success.

and i want every click will have 10 seconds cooldown so anyone cant click the button with brute force LOL

everything is run as planned but how do i refresh the cooldown after the button has been click ? here's the code
Menu option one <span id="suc5">(5 success)</span>
Menu option two <span id="suc10">(10 success)</span>

Success: <<set $rate to 0>><span id="success-rate">$rate</span>
<<set $bReady to false>>

<<set $cDown to 10>><span id="countdown">$cDown</span> seconds remaining!
<span id="statusQ"></span>

<<button "the button">>
	<<if $bReady is true>>
		<<set $randPoint to random(5)>>
		<<if $randPoint eq 2 or $randPoint eq 3>>
			<<set $rate to 0>>
		<<else>>
			<<set $rate++>>
		<</if>>
		<<replace "#success-rate">>$rate<</replace>>
		<<if $rate eq 5>>
		  <<replace "#suc5">> <a data-passage="Hell" class="karma-link">Open</a><</replace>>
		<<elseif $rate eq 10>>
		  <<replace "#suc10">> <a data-passage="Heaven" class="karma-link">Open</a><</replace>>
		<</if>>
	<<else>>
		<<replace "#statusQ">>not yet!!<</replace>>
	<</if>>
<</button>>

<<set $command to "<<timed 1s>>">>
<<for $i to 0; $i lt $cDown; $i++>>
	<<set $command += "<<set $cDown -= 1>><<replace \"#countdown\">>$cDown<</replace>><<set $bReady to false>><<next>>">>
<</for>>
<<set $command += "<<replace \"#countdown\">>Ready<</replace>><<replace \"#statusQ\">><</replace>><<set $bReady to true>><</timed>>">>
<<print $command>>

any help will be appreciated, sorry for my bad english :D

Comments

  • You have two major issues:
    • You're using <<timed>> , when you should be using the <<repeat>> macro. You've also got it outside the <<button>> when it should be inside.
    • If you want a range of [1, 5], then the call to random() should be random(1, 5). Invoking it as random(5) gives you a range of [0, 5], as noted very clearly in the documentation. I'm leaving that for you to correct, or not, as you choose.

    Anyway, the following should be more or less what you wanted:
    Menu option one <span id="suc5">(5 success)</span>
    Menu option two <span id="suc10">(10 success)</span>
    
    Success: <<set $rate to 0>><span id="success-rate">$rate</span>
    
    <span id="roll"><<button "the button">>
    	/* Handle rolling. */
    	<<set $randPoint to random(5)>>
    	<<if $randPoint eq 2 or $randPoint eq 3>>
    		<<set $rate to 0>>
    	<<else>>
    		<<set $rate++>>
    	<</if>>
    	<<replace "#success-rate">>$rate<</replace>>
    	<<if $rate eq 5>>
    		<<replace "#suc5">><a data-passage="Hell" class="karma-link">Open</a><</replace>>
    	<<elseif $rate eq 10>>
    		<<replace "#suc10">><a data-passage="Heaven" class="karma-link">Open</a><</replace>>
    	<</if>>
    
    	/* Disable the button when clicked, reenabling it after 10 seconds. */
    	<<script>>$("#roll>button").prop("disabled", true)<</script>>
    	<<set $cDown to 10>>
    	<<replace "#countdown">>$cDown seconds remaining!<</replace>>
    	<<repeat 1s>>
    		<<set $cDown-->>
    		<<if $cDown gt 0>>
    			<<replace "#countdown">>$cDown seconds remaining!<</replace>>
    		<<else>>
    			<<script>>$("#roll>button").prop("disabled", false)<</script>>
    			<<replace "#countdown">><</replace>>
    			<<unset $cDown>>
    			<<stop>>
    		<</if>>
    	<</repeat>>
    <</button>></span> <span id="countdown"></span>
    
    You'll note that I made it disable the button, so that it cannot be clicked on, and moved the timer display nearer to the button, to make it look a bit nicer.
  • awesomee, this is better than i expected and looks very neat. thanks TME u rock
Sign In or Register to comment.