Page 1 of 1

Is there an expression to have a dice explode only once?

Posted: Fri Dec 15, 2017 12:24 pm
by Zandel
Thanks

Re: Is there an expression to have a dice explode only once?

Posted: Fri Dec 15, 2017 1:50 pm
by Phergus
I assume you checked the wiki?

Dice_Expressions

Don't see that option but I just glanced at it.

Re: Is there an expression to have a dice explode only once?

Posted: Fri Dec 15, 2017 8:48 pm
by Zandel
I did, no luck

Re: Is there an expression to have a dice explode only once?

Posted: Sat Dec 16, 2017 8:39 am
by Phergus
Should be pretty straight-forward to do a macro that does what you need. Make it a UDF and then it would be easy to call from any other macro.

Re: Is there an expression to have a dice explode only once?

Posted: Sat Dec 16, 2017 12:08 pm
by Zandel
UDF? Not sure what that is.

Re: Is there an expression to have a dice explode only once?

Posted: Sat Dec 16, 2017 1:51 pm
by aliasmask
I rewrote an old function to explode each die once. Original here: http://forums.rptools.net/viewtopic.php ... 00#p241200

Code: Select all

[H: defineFunction("rollDiceE","rollDiceE@"+getMacroLocation(),1,0)]

Code: Select all

<!-- rollDiceE(numDice,sides): result (hidden: dice.tooltip) -->
[h: dice.num = arg(0)]
[h: dice.sides = arg(1)]

[h: dice.rolls = ""]
[h, count(dice.num), code: {
	[H: roll = roll(1,dice.sides)]
	<!-- explode once -->
	[H, if(roll == dice.sides): roll = roll + roll(1,dice.sides)]
	[H: dice.rolls = listAppend(dice.rolls,roll)]
}]
[h: dice.result = eval(strformat("sum(%{dice.rolls})"))]
[h: dice.tooltip = strformat('<span title="<html><b>%{dice.num}d%{dice.sides} :</b> %{dice.rolls}</html>">%{dice.result}</span>')]
[h: macro.return = dice.result]
keyword: amsave dice

Re: Is there an expression to have a dice explode only once?

Posted: Sat Dec 16, 2017 6:52 pm
by Phergus
UDF is User Defined Function (see Wiki: defineFunction()). That first bit of macro code that AM put in his post. This is normally put on a Library_Token in a onCampaignLoad macro.

Re: Is there an expression to have a dice explode only once?

Posted: Sun Dec 17, 2017 6:15 am
by Zandel
Got it, can one of you post what the syntax to call this UDF as written above would look like if say I wanted to do 1d8 exploding once?


Thanks

Re: Is there an expression to have a dice explode only once?

Posted: Sun Dec 17, 2017 7:17 am
by aliasmask
Create your lib Token, "lib:Test".
Create a macro called "onCampaignLoad"
Edit macro and put:

Code: Select all

[H: defineFunction("rollDiceE","rollDiceE@"+getMacroLocation(),1,0)]
Save macro and Run by clicking macro button so it is globally defined.

Edit and create new macro "rollDiceE".
Turn off player can edit in settings.
Add text to macro:

Code: Select all

<!-- rollDiceE(numDice,sides): result (hidden: dice.tooltip) -->
[h: dice.num = arg(0)]
[h: dice.sides = arg(1)]

[h: dice.rolls = ""]
[h, count(dice.num), code: {
   [H: roll = roll(1,dice.sides)]
   <!-- explode once -->
   [H, if(roll == dice.sides): roll = roll + roll(1,dice.sides)]
   [H: dice.rolls = listAppend(dice.rolls,roll)]
}]
[h: dice.result = eval(strformat("sum(%{dice.rolls})"))]
[h: dice.tooltip = strformat('<span title="<html><b>%{dice.num}d%{dice.sides} :</b> %{dice.rolls}</html>">%{dice.result}</span>')]
[h: macro.return = dice.result]
Save macro.

Edit and create new macro in Campaign Window Frame called "Roll 1d8"
Add text to macro:

Code: Select all

[H: result = rollDiceE(1,8)]
[H: tooltip = dice.tooltip]
[R: strformat("Roll 1d8: %{tooltip}")]
Save and run macro.

---

Explanation of what's going on.

First you create a globally accessible library of macros. Included in this library is a special macro called onCampaignLoad that auto runs when you open the campaign. Since you just created it in this example you need to manually run it for the first time. Wiki: defineFunction() is just creating the reference to the macro you want to run using an alias, which happens to be the same name as the macro, but doesn't need to be.

Next you create the contents of that macro to be run. You need to turn off the Players can edit in the options of the macro which is defaulted to on. Personally, I think the default should be off because the token owner and gm can always edit the token anyway. At this point, it's just an annoying step.

Finally, you create a macro that everyone can use referencing the new function in the Campaign Window.

Code: Select all

[H: result = rollDiceE(1,8)]
This gives you the numeric value of the result to be used in formula or whatnot. There's a hidden result created called "dice.tooltip" which is a text based result that gives a tooltip when moused over showing the details of the roll. (see Wiki: defineFunction() for scope details)

Re: Is there an expression to have a dice explode only once?

Posted: Sun Dec 17, 2017 7:30 am
by Zandel
Thank you, works great!