Howdy, Stranger!

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

random variable with button interaction in sugarcube 2

edited November 2015 in Help! with 2.0
i have struggle with this problem for 2 days , this is the scenario

i saw a girl surrounded by 5 man in the corner of the street, what should i do ?

(here's the options, 2 default option which available from beginning and 2 other options will unlocked when reach success rate needed)
help her
keep walking and ignore her
stay and look what happen later (need 5 success rate)
pick your machine gun and shoot them all (need 10 success rate)
<<set $randPoint to random(5)>>
Success: <<set $rate to 0>><span id="success-rate">$rate</span>
<<button "the button">>
	<<if $randPoint eq 2 or 3>>
	<<set $rate to 0>>
	<<else>>
	<<set $rate++>>
	<</if>>
<</button>>

with this code , when pressed the button and if the random point reach 2 or 3 ... the success rate come back to zero but increasing with another number

hell even the success rate is doesnt even change a bit ... must be a bad coding i have.

anyone can help me about this ? thanks before. sorry for my bad english, not my mother language

Comments

  • edited November 2015
    There are a number of issues with your code example:

    1. To compare $randPoint to two numbers you need to do:
    <<if $randPoint eq 2 or $randPoint eq 3>>
    
    2. Changing the value of a previously displayed variable will not cause the displayed value to automatically update, you have to code the update yourself:
    <<replace "#success-rate">>$rate<</replace>>
    
    3. The random value assigned to $randPoint does not change, so the button will always either add one to $rate or set $rate to zero. If you want $randPoint to have new random value each time the button is clicked then you have to move the <<set>> inside the button's code.

    Try the following:
    Success: <<set $rate to 0>><span id="success-rate">$rate</span>
    <<button "the button">>
    	<<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>>
    <</button>>
    
  • edited November 2015
    greyelf wrote: »
    Success: <<set $rate to 0>><span id="success-rate">$rate</span>
    <<button "the button">>
    	<<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>>
    <</button>>
    

    thx for the quick replay and its works great :)

    and for the last, how do i disable link when i using wiki link (correct me if im mistake about the name), just like this
    [[pick your machine gun and shoot them all|Shoot]]
    
    or like this
    <a data-passage="Shoot">pick your machine gun and shoot them all</a>
    

    then how to enable it ? sorry for too much request for this .. thanks before
  • ah nevermind, already solved the problem by replacing the text options using replace macro inside the button.

    but it seems i have another problem, how to add delay on the button so people cant forcing it by clicking it repeatedly , because its look so easy when people clicking the button with insane speeed LOL and i dont want that happen.

    please give me advice, thanks before.

Sign In or Register to comment.