3
\$\begingroup\$

I am trying to set up a multiattack function in AnyDice. I already have a function that calculates the damage dealt by a single attack, but now I want to calculate the total damage dealt by multiple successive attacks, where the number of attacks is given by a variable.

How can I set up a function in AnyDice to repeat another (already written) function a number of times equal to some pre-defined variable and output the sum of the results?

\$\endgroup\$
2
  • \$\begingroup\$ Is the question really that unclear? It seems obvious what is being asked, certainly to anyone who is familiar with Anydice. \$\endgroup\$
    – Carcer
    Mar 30 '17 at 9:59
  • \$\begingroup\$ Hi, Daniel. I've tried to edit your question to hopefully make it clearer (and possibly get it reopened), but if I've made any mistakes or changed it into something other than what you meant to ask, please feel free to revert my edits and/or to make further edits yourself. Thanks! \$\endgroup\$ Apr 20 '17 at 23:38
6
\$\begingroup\$

Use a loop with an accumulator variable. Example code:

function: attack ATTACK:d {
  result: ATTACK
}

function: multiattack X:n Y:d {
  TOTAL: 0
  loop N over {1..X} {
    DAMAGE: [attack Y]
    TOTAL: TOTAL + DAMAGE
  }
  result: TOTAL
}

MULTIATTACK: 3
ATTACK: 2d10

output [multiattack MULTIATTACK ATTACK] named "[MULTIATTACK] attacks"

The "attack" function handles the resolution of an individual attack. In this case it's very simple, and just returns the die it is given.

The "multiattack" function is a helper function which handles calling the attack function a specified number of times. Note that you can take parameters here which the multiattack function itself does not care about and pass them on to the attack function, which does. It uses the accumulator variable, TOTAL, to track the total result of the attacks, and then returns it when all attacks are calculated.

The variable "N" is also available inside the scope of the loop. My example doesn't directly use it, but if you were modelling 3e D&D's iterative attacks, for instance, you could write logic using the value of N to apply the appropriate attack penalty depending on if it were the 2nd, 3rd, or 4th attack, for instance.

Then there's variables and the output statement which I hope are self-explanatory.

\$\endgroup\$
3
  • \$\begingroup\$ I don't think the attack function is necessary. This simplified version returns the same output. \$\endgroup\$ Mar 29 '17 at 22:03
  • 3
    \$\begingroup\$ In the example I have written it was not strictly necessary to split the attack into another function because it is literally just returning the die it's given, but I structured it this way because the reality will be that the attack function is doing something rather more complicated, and the point is that this is an example of one function calling another function multiple times to achieve a sum result, which is what the question asked. \$\endgroup\$
    – Carcer
    Mar 29 '17 at 22:11
  • \$\begingroup\$ thank you. I am indeed using a much more complicated attack function including variables for advantage and disadvantage and crits etc. \$\endgroup\$
    – Daniel
    Apr 21 '17 at 16:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .