Howdy, Stranger!

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

Rolling a variable number of dice, based on a stat

Hello,

First post, here :)

I'm setting up my first Twine game using sugarcube 2.x and I'm interested in some possibilities with the random macros.

I'm wanting to set up a situation where a character is able to roll a number of separate dice that depends on a previously generated statistic, for instance, rolling 2 dice if their statistic is 2; 3 dice if it is 3, etc.

I'd like the Twine to show the results of each dice separately, and then evaluate and count the number of 'successes' which have reached a target number.

I'm familiar with how to use, for instance:

<<set $l2t1 = random(1,6)>> <<print $l2t1>>

to generate a d6 roll and display it, and

<<if $l2t1 lte 4>> "You have failed"<<set $l2t1CHECK eq "FALSE">>
<<else>> "you have succeeded" <<set $l2t1CHECK eq "TRUE">>
<<endif>>

To evaluate success against a target number of 4 and log that success with another variable for later reference


...but is there a way to have the Twine iterate this process a number of times that is equal to a particular variable, and then log and print the results of each roll, as well as then the number of successes?

I'm imagining it printing something like:

"Your agility is 3"
"Your rolls are: 4, 1, 6"
"Those 2 successes are enough to pass the test"

I hope this is clear and I'm sorry if I have left anything out :)

Thanks heaps!

Comments

  • Try creating a recursive widget.

    Process one roll, decrement the number of rolls required and, if it's greater than 0, call yourself again. Should unwind ok once it hits zero.
  • Or you could use the <<for>> macro in the widget.
  • A for loop would have been my first suggestion as well. For example: (goes in a widget-tagged passage)
    /*
    	<<rollover D6_ROLL_POOL SUCCESSES_REQUIRED TARGET_NUMBER>>
    
    	e.g.
    		→ Roll 5 d6, requiring 3 successes against a roll-over target of 4.
    		<<rollover 5 3 4>>
    
    		→ Roll $dice d6, requiring $required successes against a roll-over target of $target.
    		<<rollover $dice $required $target>>
    */
    <<widget "rollover">>\
    <<silently>>
    <<set _required to $args[1]>>
    <<set _target to $args[2]>>
    <<set $rolls to []>>
    <<set $successes to 0>>
    <<for _i to $args[0]; _i > 0; --_i>>
    <<set _roll to random(1, 6)>>
    <<set $rolls.push(_roll)>>
    <<if _roll gt _target>><<set $successes++>><</if>>
    <</for>>
    <</silently>>\
    Your target is <<=_target>> and you require <<=_required>> successes.
    Your rolls are: $rolls.
    Yielding $successes success<<if $successes neq 1>>es<</if>>, \
    which <<if $successes neq 1>>are<<else>>is<</if>> \
    <<if $successes lt _required>> not<</if>> \
    enough to pass the test.
    <</widget>>
    
    Post-invocation, the variables $rolls (array) and $successes (integer) are set to an array of all roll values and the number of successes, respectively.
  • Thanks so much, people- these are great ideas :) I'll try them out tonight. Really appreciate the help!
  • Oh, yes. Forgot about that...
  • edited May 2016
    If I want the code TheMadExile posted to additionally emulate the old White Wolf "exploding" mechanic (re-roll any die that crits), would adding this be the correct way to achieve it?
    <<if _roll eq 10>><<set $args[0]++>><<endif>>
    
  • edited May 2016
    No. To do it simply, you'd want to do something like the following.

    FIND:
    <<if _roll gt _target>><<set $successes++>><</if>>
    <</for>>
    
    REPLACE WITH:
    <<if _roll gt _target>><<set $successes++>><</if>>
    <<if _roll eq 10>><<set _i++>><</if>>
    <</for>>
    
    Since the loop variable (_i) is decremented as part of the loop, incrementing it adds another roll.
Sign In or Register to comment.