Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

How Do I Extract the Rolled Dice When Dice Can Explode?

Hello all,

I'm expecting to write a Roll20 API script here (as our GM doesn't really know anything about Javascript) to implement the mechanic in the Legend of the Five Rings RPG that allows you to re-roll un-exploded dice (d10's) that have a value of 1. I know how to logically structure the script (see pseudo-code below), I just don't know how to access the dice via the Roll20 API. Basically, the following procedure should happen:

1. Player types command. I'm going to guess that for now, I'm going to have to have this script examine every single invocation of /r or /gr.
2. Let the dice-roller evaluate all dice. Yes, this means that all dice that do explode should explode (my group usually uses !! for explosions, but ! is equally applicable).
3. Parse the JSON object so that I can actually get to the dice rolls using JSON.parse() on the chat message.

And this is the part where I get fuzzy on what I have to do. I am wondering if there is an easy way to extract all the dice rolls from the parsed JSON object into a Javascript variable that's essentially an array and then go through that array to figure out how many dice == 1. I'd want to look at their final values, as I know from testing that the default "r" reroll option will discard any roll containing a 1 in it's explosion chain (e.g. 10+10+10+1 will still get discareded, even though it clearly isn't a "1"). I want to examine the compounded results (so I guess I'll need !! in (2) above), and then send a new chat message via sendChat() to force the dice-roller to re-roll until all dice evaluate the statement value == 1 to false, and then print the results. I just don't know what the easiest way to (a) access the compounded dice rolls after parsing the JSON object and (b) forcefully re-rolling dice would be. I simply want an easy function that will extract the compounded dice values into an array for me, a function that'll allow me to re-roll dice and then allow me to only have the final chat message of the final roll and dice results be shown to either all (if /r is used) or only the sender and the GM (if /gr is used) without sending a message for every time it tries to reroll dice.

Pseudocode:
on every chat message with a roll in it {

roll all dice and place them in var dicearray
while(!(return from function to check for dice == 1)){
     reroll dice to attempt to get rid of all 1's
     update dicearray
}

send chat message with final roll
}


August 10 (12 years ago)

Edited August 10 (12 years ago)
Riley D.
Roll20 Team

On this page: https://wiki.roll20.net/API:Chat under there are two headings in italics, "Roll Result Example 1" and "...2" (2 probably relates closest to what you'll be seeing for this type of roll). If you click the expand buttons next to those, it will show you what information you get about the rolls attached to any chat events. Let us know if you have any questions about the roll object and what it has in it.

After you figure out how many to re-roll, you can use the sendChat() command to execute a roll as the person who made the original roll.