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

Using Variables in Macros

May 05 (5 years ago)

Edited May 05 (5 years ago)

How do you use a variable in a macro?  I want to use the EXACT dice roll multiple times over multiple lines.


Using a contrived example:

/me drinks a Potion of Healing for [[2d4+2]]
!token-mod {{
  --set
     bar1_value|[[{@{selected|bar1}+2d4+2, @{selected|bar1|Max}d1}kl1]]
  --ids @{selected|token_id}
}}

The value will be different each roll.  Now, I'm not looking for suggestions on how to improve the macro, that's missing the point. I'm looking to use the SAME ROLL more than once for whatever reason.

var roll = [[2d4+2]]
/me drinks a Potion of Healing for $roll
!token-mod {{
  --set
     bar1_value|[[{@{selected|bar1}+$roll, @{selected|bar1|Max}d1}kl1]]
  --ids @{selected|token_id}
}}

In this non-working stub, I roll ONCE, and then apply that roll to both the screen and another script or macro.  Even if I just wanted to print it twice (for whatever reason), all I want to do is use the SAME ROLL more than once.  Today it's for the Potion of Healing, tomorrow it's for something else, the day after that it'll be for another thing.  I'm not looking to rewrite every single function of the Roll20 interface with my own person scripts, I just want to use a variable so I can use a roll more than once.


My current hack is to create attributes on EVERY character called "temp_roll" and use that in all my equations, but that's so customized to my use-case with such a high setup cost it's not worth sharing for others to use.


May 05 (5 years ago)
keithcurtis
Forum Champion
Marketplace Creator
API Scripter

Macros are not really a programming language. They are essentially a dice roller with some basic functions and formatting controls. They lack things like memory or logic. Dice rolls are fire and forget, and there are no if-then or looping structures.

As such, if you generate a roll, you cannot use it again later. You can re-use the results of user input from a drop-down query, but only within the same instruction. To do what you want to do without resorting to your hack would likely require an API script. Since you are a Pro subscriber you have the option, but it does require a basic working knowledge of JavaScript.