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

Macro "switch" case 1 o 3

Hi everyone, I recently found that it was possible to predefine answers in a macro then erase those infos with rolls. I pretty sure that this is not clear, so here is my example :  -> I wanted to have a function that toss a coin, then if HEAD then effect 1, otherwise TAIL then effect 2 : ``` &{template:default} {{name=**TOSS A COIN**}} {{[1](#)=**HEAD !** Effect 1}} {{[2](#)=**TAIL !** Effect 2}} {{[[[1d2]]](#)=}} ``` So this is working well, but i thought at first that i was rolling a 2faces dice and that the result of it will select the predifined answer, and i was wrong.  So now i was working on a macro in which toss a coin would be roll a 6 faces dice. And before working on it, i was wondering how to make it with 3 predifned answers. So here was my first attempt :  ``` &{template:default} {{name=**Blabla 3 events**}} {{[1](#)= Effect 1}} {{[2](#)= Effect 2}} {{[3](#)= Effect 3}} {{[[[1d3]]](#)=}} ``` That's where i found out that the answer would look something like that : If dice == 2 then output number 2 would be erased. And outputs 1 and 3 would still be up. So i continue working on it and tried to add another erase roll  ``` &{template:default} {{name=**Blabla 3 events**}} {{[1](#)= Effect 1}} {{[2](#)= Effect 2}} {{[3](#)= Effect 3}} {{[[[1d3]]](#)=}} {{[[[1d3]]](#)=}} ``` But of course, sometime it will work, i'll have 2 different roll result, but sometime, it would be same result, so will have 2 effects instead of 1.  So i found out that rolls have references like, indexs, and could be reused with $[[n]], where n is the index.  And i work on that :  ``` &{template:default} {{name=**Blabla 3 events**}} {{[1](#)= Effect 1}} {{[2](#)= Effect 2}} {{[3](#)= Effect 3}} {{[[[1d3]]](#)=}} {{[ $[[0]]+1 % 3 + 1 ](#)=}} ``` With " $[[0]]+1 % 3 + 1  " is the only way that i found to describe the way of rolling a dice which is not equal to the previous $[[0]] (previous roll) +1 (to have another result) % 3 (modulo 3, to avoid a result = 3 + 1 = 4) + 1 (cause "% 3" will define a domain on [0,2] and not [1,3], so i add 1 to the domain) So now i have 2 different results for rolling 3 faces dices, BUT my macro, OF COURSE, isn't running...  Any help, please ? :D 
1699617417
timmaugh
Pro
API Scripter
You can't reuse the roll in another roll like this (although you only have single brackets around the modulo equation, not double). At least, you can't short of a Mod script. In order to do what you want with standard Roll20 syntax, you have to construct the more complex roll first, then refer to the interior roll later... This lets you use the 1d3 without having to do new math on it: [[ [[1d3]]+1 %3]] ...and... $[[0]] That way the 0-index refers to the 1d3
Heyn thk for ur quick feedback !&nbsp; So yeah i didnt put brackets around modulos and other operation, cause it didn't work aswell, the first brackets that you can see is for that kind of formula : {{ [n] (#)=}} where n is the previous referenced key to a value effect. That's why i only put one. You are talking about " Mod script", but i dont what it is, can you tell me more about that ?&nbsp; You said " You can't reuse the roll in another roll like this", but i found that solution here : <a href="https://wiki.roll20.net/Reusing_Rolls" rel="nofollow">https://wiki.roll20.net/Reusing_Rolls</a> and so i just did what they did before : {{ roll a dice }} then new bracket {{ use the first roll result with $[[0]] }} "" In order to do what you want with standard Roll20 syntax, you have to construct the more complex roll first"" so i think that i finnaly understand what you meant here : if i want to use $[[n]] in next brackets, i need to have as first brackets my rolls. But then, how to hide it ? cause i dont want to have just a roll in my macro output :O
1699624020
timmaugh
Pro
API Scripter
Reusing Rolls is definitely a trick you can do, and it can be helpful in a lot of different situations. What I was saying is that you can't re-use that same roll in the roll equation of another roll... that will break Roll20 parsers: This WON'T work: [[1d20]] [[ $[[0]] + 1]] To do that, you have to construct the more complex roll first using an embedded inline roll for the d20 so that you can later refer to it: [[ [[1d20]] + 1]] $[[0]] In your case, you wouldn't have a roll just hanging out in your output, because you'd use it the same way you did for the initial example where you flattened out of the template parts: {{[[1d2]](#)=}} Constructing it like that, you'd have: &amp;{template:default}&nbsp;{{name=**Blabla 3 events**}}&nbsp; {{1(#)= Effect 1}} {{2(#)= Effect 2}} {{3(#)= Effect 3}} {{[[ ([[1d3]] +1) % 3 + 1 ]](#)=}} {{$[[0](#)=}} (I forgot the parentheses in my previous post, but I believe they're necessary so I included them, here.) I removed the extraneous brackets to simplify things to the eye. You can add them back, if you need them. Also, if you had to do work in a template that you didn't want to have show (if you were concerned about having a roll hang out in your command line, you can put anything between template parts, and it won't show: &amp;{template:default} {{name=Proof of Concept}} This won't show {{Hoocha=Volcano Man}} So if you had to do preliminary roll work to arrive at a solution from which you could later reuse the rolls, you could do it like that and have it hidden. As for explaining scripts, they are a much larger subject and they are a perk of Pro membership (the game-owner has to be Pro, then everyone in the game can use them). Specifically as it relates to reusing rolls for math again, though, that's something that ZeroFrame can do (part of the MetaScriptToolbox): !&amp;{template:default} {{name=Proof of Concept}} [[1d20]] [\][\] ($[[0]] + 1) % 3 +1 \]\] {{Roll 0=$[[0]]}} {{Roll 1=$[[1]]}}{&amp;simple}
Wow, thanks a lot ! That's way clearer ! :O But i will still be blocked in my case. If i use this formula " [[ ([[1d3]] +1) % 3 + 1 ]]", it makes no sens. The purpose of this is to have [[1d3]] getting replaced by a variable. If i cant use variable, neither reuse the previous roll to be part of a new formula, then i can't do what i want, which is roll 2 "3 faces" dices while having 2 different results. Hence i had a first [[1d3]] then reusing his result to get another value between 1 and 3 while being different of previous one. So what should i do to make it works ?&nbsp;
1699627014
timmaugh
Pro
API Scripter
Granted, I haven't had a chance to test it, but in what way would you be blocked? The initial formula: [[ ([[1d3]] +1) % 3 + 1 ]] ...would produce the second result (the +1 result, looping back to 1 after your limit of 3). Then you reference the $[[0]] roll (which is just the 1d3) to get the first result (the original roll). That gets you the two results, just in reverse order, which doesn't matter for your application. Roll indexing is a bit tricky, but the $[[0]] index refers to the first roll to resolve, which is the inner 1d3 roll... then $[[1]] would refer to the next roll to resolve (the more complex/original equation). We don't need $[[1]], but I wanted to indicate how the rolls were being referred to so you understood which one was being used. BTW, I should have been clearer that my image was showing a 1d10 as the base roll, rather than a 1d3. I should have done 1d3. Sorry.
Ok, ive done some test, now i understand :O&nbsp; Thank you, sincerely. So $[[]] is referring only to a dice event, not to a formula But if i wanted to have a formula, and referred to it ? How could i do that ?&nbsp;
1699628826

Edited 1699628850
Unfortunatly,&nbsp; ``` &amp;{template:default} {{name=**TEST**}} {{[1](#)=Effect 2}} {{[2](#)=Effect 2}} {{[3](#)=Effect 3}} {{[[[([[1d3]]+1)%3+1]]](#)=}} {{$[[0]](#)=}} ``` Or with&nbsp; {{[$[[0]]](#)=}}, or&nbsp; {{$[[[0]]](#)=}}, does not work. Output here :&nbsp;
May i ask another out of context question ?&nbsp; Is there a way of interacting directly with attribute with a macro or formula ? Like i create an attribute "hp_bar" which will represent hp of someone. Is this possible to say : token_name.hp_bar = token_name.hp_bar - 1d8 in a macro ? Or to have an attribute "STR" and to add 5 to it ?&nbsp;
Hi all, i just found that doing this : [[1d3r[[1d3]]]] then $[[0]] would so the same as doing our previous formula which is :&nbsp; [[([[1d3]]+1)%3+1]] then&nbsp; $[[0]] I tried to debug my macro all day long, and i think it is not possible... without "(#)", the output key would be "[1]" instead of a purple "1", while $[[0]] referred to [[1d3]], the roll of 3 faces dice, instead of his result.&nbsp; Maybe there is a possible to do that with Decks, i might look up for that solution near.
1699803362
timmaugh
Pro
API Scripter
I finally got a chance to play with this, and I think you're right that you can't do it natively in a Roll20 template. That said, I got it to work with the MetaScriptToolbox... so I'll show that in a minute. First, I think there's an error in your equation/formula: 1d3 gives a range of 1-3 1d3 % 3 gives a range of 0-2 You only have to add +1 to that one time... otherwise you're effectively moving 2 positions. Which, I guess, could simulate using the position you rolled and the *previous* position (since you're only working with a set size of 3)... but unless that's what you're going for you can do with a single +1. That said, by using ZeroFrame (from the MetaScriptToolbox), the goal can be achieved by: !&amp;{template:default} {{name=**Blabla 3 events**}} {{[1](#)= Effect 1}} {{[2](#)= Effect 2}} {{[3](#)= Effect 3}} {{[[[ ([[1d3]]+1) % 3]]](#)=}} {{[$[[0]].value](#)=}}{&amp;simple} I know you don't have the Pro tag net to your name, so I'm only including the script-provided solution because you asked about scripts. If the game owner gets a Pro sub, everyone in the game can use the scripts the GM installs. Nick has a good video on getting scripts installed, if you/your gm wants to go that route.
Rainbow Encoder (the great mathemagician, the reverse-enginarcanist, the over-complicated easy solutionneer) has found a trick to reuse rolls indefinitely without the need of scripts but it is not a path I would suggest to the common mortals (which I am). Below is a combination of several useful tricks found by the community: - HTML Styles in chat and Overwriting templates fields in macros (by Oosh), - Tooltips for links roll buttons etc... (by RainbowEncoder) Flip a coin and display only one result (I believe that what you wanted in the first place): &amp;{template:default}{{name=TOSS A COIN}}{{[0](#)=}}{{[1](#)=HEAD! Effect 1}}{{[[[1d2=2]]](#)=TAIL! Effect 2}}{{[0](#)=The result is:}} Same as above but with some CSS styling to hide some unwanted info (keys) and making the result of a different color: &amp;{template:default}{{name=TOSS A COIN}}{{[0](#" hidden)=}}{{[1](#" hidden)=[HEAD! Effect 1](#" style="color:green;text-decoration:none;cursor:text)}}{{[[[1d2=2]]](#" hidden)=[TAIL! Effect 2](#" style="color:red;text-decoration:none;cursor:text)}}{{[0](#" hidden)=The result is:}} Roll a 4-sided dice and display only one result (with CSS styling and tooltips info): &amp;{template:default}{{name=TOSS A COIN for 5 GP}}{{[0](#" hidden)=}}{{[[[1d4&lt;4]]](#" hidden)=[Poor](#" style="color:gray;text-decoration:underline;cursor:text" class="showtip" title="It is worthless.)}}{{[[[1d4&lt;3]]](#" hidden)=[Uncommon](#" style="color:green;text-decoration:underline;cursor:text" class="showtip" title="It worth 10 GP.)}}{{[[[1d4&lt;2]]](#" hidden)=[Rare](#" style="color:blue;text-decoration:underline;cursor:text" class="showtip" title="It worth 100 GP!)}}{{[[[1d4&lt;1]]](#" hidden)=[Legendary!](#" style="color:orange;text-decoration:underline;cursor:text" class="showtip" title="It worth at least 10,000 GP!)}}{{[0](#" hidden)=The quality of the item you won is:}} Note that in this last example, no roll is reused. The trick lies in the fact that you make believe you roll a single die whereas in fact, you roll several dices and sort the result in a descending order of probability. (e.g. roll 4 times a 4-sided dice, roll 8 times a 8-sided dice, roll 12 times a 12-sided dice, etc... and each time you set the higher chance to get a given roll at the 1st position: first [[1d4&lt;4]], then [[1d4&lt;3]], then [[1d4&lt;2]], and so on...) Hoping those examples help you in your searches. Post Scriptum: for the first macro you wrote, think the result of the coin acts like a blank paper that covers the corresponding roll. If you wanted it to cover the "wrong" result, you'd just have to invert the rows (i.e. 1: Tail, 2: Head).
Heyllo !&nbsp; Thanks both of you ! ERIK you are a genius ! Your trick is really good, it ensures to have different results, with only one final line. But there is minor problem, your math is not correct. Let me explain :&nbsp; At the end, we should have 1/4 chance to get each result, which was my goal. But with your formulas, those probabilities are not the same. The probability of having a result is one minus the formula probability, the all multiplied by the previous probabilty. So for "legendary" item, it is 1/4 , all good. But for "Rare" item, it is :&nbsp; (1-1/4)x(1/2) = 3/8 , which is not 1/4. Same for Uncommun item, it is :&nbsp; (1-3/8)x(3/4) = (5/8)x(3/4) = 15/32 . But all good, cause i found how to correct that :&nbsp; ``` &amp;{template:default}{{name=TOSS A COIN for 5 GP}}{{[0](#" hidden)=}}{{[1](#" hidden)=[Poor](#" style="color:gray;text-decoration:underline;cursor:text" class="showtip" title="It is worthless.)}}{{[[[1d2&lt;1]]](#" hidden)=[Uncommon](#" style="color:green;text-decoration:underline;cursor:text" class="showtip" title="It worth 10 GP.)}}{{[[[1d3&lt;1]]](#" hidden)=[Rare](#" style="color:blue;text-decoration:underline;cursor:text" class="showtip" title="It worth 100 GP!)}}{{[[[1d4&lt;1]]](#" hidden)=[Legendary!](#" style="color:orange;text-decoration:underline;cursor:text" class="showtip" title="It worth at least 10,000 GP!)}}{{[0](#" hidden)=The quality of the item you won is:}} ``` Proof here :&nbsp;
And it is extending pretty well ! For 6 faces :&nbsp; 6 = [[1d6]]&lt;1 5 = [[1d5]]&lt;1 4 = [[1d4]]&lt;1 3 = [[1d3]]&lt;1 2 = [[1d2]]&lt;1 1 = 1 For N faces : N = [[1dN]]&lt;1 N-1 = [[1d(N-1)]]&lt;1 ...&nbsp; N-(N-2) = [[1d(N-(N-2))]]&lt;1 1 = 1
- "Err.. huh. What kind of croo.. businessman I would be, if I let the shee.. honest customers to have the very same chances of winning my worthless trink.. my premium quality item and my most prized top-notch item? I'd go bankrupt." *change headgear from black mask to top hat with monocle* - "Yup... sure. I see I've met a professional, a true connaisseur un somme. I better watch my mouth from then." *and since he never missed a good opportunity* - "Want to test your luck again and toss a coin? (that's 5 GP.)" xD True I lose the purpose of the multi-roll macro while writing it. Glad you saw it and modified it to your convenience. As for your off-topic question, it is possible to write a macro that will display and/or modify an attribute or an ability... in the chat window only. Only scripts can modify character sheets or tokens. Scripts are available through API Mods (a Pro perk). Only one player need to go Pro, usually the one who create the game / is the GM. Every players who join his/her game will benefit from the Mods he/she installed. For the example macro below, you need a token linked to a character sheet. If the character sheet doesn't have any attribute named "Strength", replace the string with any attribute that is present in the sheet. &amp;{template:default}{{name=Character Stat}}{{**@{selected|token_name}** strength changed from [[@{selected|Strength}]] to [[@{selected|Strength}+5]]}}