Rolling Dice Macro in Harlowe

Twine Version: 2.3.15
Story Format: Harlowe 3.2.3

Hello, recently I was trying to make a rolling dice macro in harlowe so I could call to it easily. So far I think I have a simple method of doing the dice rolls, but I’m struggling to make it work in the macro.

You rolled the dice...

(set: $d1 to (random:1,6)) 
(set: $d2 to (random:1,6))

(print: $d1) (print:"&") (print: $d2)

(print: $d1 + $d2 )(print:"!")

This is what I tried to use, and it seems to work well on its own.

(set: $rolldice to (macro: 
(set: $d1 to (random:1,6)) 
(set: $d2 to (random:1,6))

(output-data: $d1) (output:"&") (output-data: $d2)

(output-data: $d1 + $d2 )(output:"!")



This is how I tried to put it into a custom macro. But I only get the $d1 value outputted. And when I call the macro twice in a row, I get two values that are different from the $d1 and $d2 values they should be. I think I’m missing something really obvious, and I’m wondering how I would fix this.

If you look at the custom macro definition examples included in documentation of the (macro:), (output:), and (output-data:) macros you will see that each of them has only a single (output:) or (output-data:) macro call in it, that none of them call both of the “output” related macros, and that the calling of the “output” macro occurs at the end of the custom macro definition.

So your custom macro definition needs to be rewritten so that there is only a single usage of an “output” macro at the end of the definition.

warning: Due to how the Passage Transition process is implemented the content directly associated with the “output” macros is executed twice. For this reason I suggest only a pre-determined “value” should be passed as an argument or referenced in the Hook associated with these macros.

As it appears you want the custom macro to output a formatted multiline message then I suggest changing your code to something like the following, which uses the (str:) macro and HTML <br> element to build such a message.

(set: $rolldice to (macro: [
	(set: $d1 to (random: 1,6)) 
	(set: $d2 to (random: 1,6))
	(set: _output to (str: $d1) + " & " + (str: $d2) + "<br><br>" + (str: $d1 + $d2) + "!")
1 Like

Thank you!! That helps a lot. Okay great, so only one output, and I can construct it as a string and then set it to a temporary variable inside the macro.