$numericalDifference$ Function for Dice?

If I create a dice button with two dice, I can use $numericalTotal$ to get the sum. What I want to do is roll two dice and get the difference between them (0-5). Any way to do that?

There are 2 ways to do this.

If you just want to report this using the in-built Report Format of the Dice Button, then the individual dice roll results are available as $result1$ and $result2$. However, to do the match to calculate the difference, you need to use BeanShell for the Report format. The following gives you what you want:

{"result = " + result + ", Diff = " + Math.abs(result1 -result2)}

If you want to report this outside of the Dice Button, then it is quite a bit trickier as the only thing you have to work with is the comma delimited list of results exported by the _result global variable. Assuming, the Dice Button is name ‘2d6’, then the following is the equivalent to the Report Format above:

{"result = " + d6_result + " diff = " + Math.abs(Integer.parseInt(d6_result.substring(0,1)) - Integer.parseInt(d6_result.substring(2,3)))}

The Integer.parseInt stuff is needed to convert from strings to numbers and is part of the in-built Java support in BeanShell. It’s not something you would be expected to know.

Your query has actually show up a couple of issues in the Audit Trail in the error log, and also the need for a fully documented toNumber() function to make this a bit simpler:

{"result = " + d6_result + " diff = " + Math.abs(d6_result.substring(0,1).toNumber() - d6_result.substring(2,3).toNumber())}

Unfortunately, the toNumber() function will need to wait for Vassal 3.8.


BeanShell to the rescue! Thanks, @Brent_Easton