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

Can't apply new math to an earlier value using $[[x]]

1692246664

Edited 1692246865
Zack
Marketplace Creator
I'll try to break down the problem I'm having. Let's say you've run some math in a macro already, and want to print the result elsewhere. It could look something like {{attack=[[1d20]]+5}} {{roll=$[[0]]}} Say the macro rolled a 12 on the 1d20. The Attack section would list 17, while the Roll section would list the result of the earlier roll, 12. But then, let's say you want to print the earlier roll divided by 2. You might instead try to put down something like {{roll=$[[0]]/2}}, expecting it to take the earlier result, divide it, then print the new result. What actually happens instead is that it prints "12/2". {{roll=($[[0]]/2)}} becomes "(12/2)".  {{roll=[[$[[0]]/2]]}} becomes "[[12/2]]" etc. I've tried a bunch of different ways of structuring it, but it always registers the extra code as plain text if it works at all. Is there any way to get it to actually run an additional operation on that earlier value?
1692278719
Kraynic
Pro
Sheet Author
Not to my knowledge, no.  The way you do something like that is to apply all the math you want in the original calculation, including that division.  {{attack=[[[[1d20+5]]/2]]}} Then you use the $[[X]] trick to display the various values you will need.  So you could pull out the non-divided roll with $[[0]] and then the divided one with $[[1]].
1692281358
Zack
Marketplace Creator
I've used that on simpler stuff, like {{attack=(([[1d20/2]])*2)+5}} Unfortunately it falls apart on more complicated stuff. Right now I was hoping to retrieve the decimals so I could independently display the gold piece and silver piece values of a result, instead of just having fractional gold values. But I haven't been able to figure out a way to do it without this.
1692283012
Gauss
Forum Champion
Hi Zack, could you give the original math of what you are trying to do on the gold and silver values? 
1692284593

Edited 1692288638
GiGs
Pro
Sheet Author
API Scripter
You cannot apply any math at all to properties that look like this $[[0]]. It's best to think of them as "things", not numbers, you cannot put them in calculations. Kraynic's suggestipon is the way to go. Note that if you try to use the same roll two or more different ways, you probably cannt do it (and would need to use Custom Roll Parsing instead). But here's a way to do what you've described. Instead of this: {{attack=[[1d20]]+5}} {{roll=$[[0]]}} you'd have something like [[ [[ [[1d20]]/2]] *2 +5]] {{attack=$[[2]] }} {{roll=$[[0]]}} [[halfed die=$[[1]]}} But if you want that halving rounded off to a whole number, you can't do it with this approach. Zack said: I've used that on simpler stuff, like {{attack=(([[1d20/2]])*2)+5}} Unfortunately it falls apart on more complicated stuff. Right now I was hoping to retrieve the decimals so I could independently display the gold piece and silver piece values of a result, instead of just having fractional gold values. But I haven't been able to figure out a way to do it without this. There are some forms of math you cannot do with normal dice expressions - instead you need Reusing Rolls. But there are also excpressions you can't do using Reusing Rolls. I think you can do almost everything with Custom Roll parsing, but that requires a much more complex procedure (writing a sheet worker - and a complex one at that, knowing how dice values are constructed and extracted, and modifying your rolltemplate), soyou have to decide how important it is.
1692288733

Edited 1692288946
Zack
Marketplace Creator
Gauss said: Hi Zack, could you give the original math of what you are trying to do on the gold and silver values?  The full macro is stupidly complex, but what I'm basically trying to do would be something like this; &{template:default} {{name=Calculate Cost}} {{GP=[[floor([[1d100/5]])]]}} {{SP=($[[0]]-$[[1]])*10}} So I'm basically just trying to split off the decimal value and print it separately as a whole number.
1692288763
GiGs
Pro
Sheet Author
API Scripter
As far as I know, there's only one situation where objects like $[[0]] can be treated as numbers: when using them in a rolltemplate's logic helper. They are treated as "things", objects of some sort, the rest of the time.
1692288878

Edited 1692288932
GiGs
Pro
Sheet Author
API Scripter
Zack said: Gauss said: Hi Zack, could you give the original math of what you are trying to do on the gold and silver values?  The full macro is stupidly complex, but what I'm basically trying to do would be something like this; &{template:default} {{name=Calculate Cost}} {{GP=[[floor([[1d100/5]])]]}} {{SP=($[[0]]-$[[1]])*10}} It's a good idea to state exactly what you macro is in normal english, not using roll20. But you can't do this $[[0]]-$[[1]] - those objects are not numbers. If you want the remaineder after a calculation, there is a an operator for that (maybe %?)
1692289464

Edited 1692290096
Zack
Marketplace Creator
GiGs said: Zack said: Gauss said: Hi Zack, could you give the original math of what you are trying to do on the gold and silver values?  The full macro is stupidly complex, but what I'm basically trying to do would be something like this; &{template:default} {{name=Calculate Cost}} {{GP=[[floor([[1d100/5]])]]}} {{SP=($[[0]]-$[[1]])*10}} It's a good idea to state exactly what you macro is in normal english, not using roll20. But you can't do this $[[0]]-$[[1]] - those objects are not numbers. If you want the remaineder after a calculation, there is a an operator for that (maybe %?) The modulo (%) wouldn't give me the decimal. just the remainder. Say I took 10/3=3.33... What I want is to get 3 and 33 as separate results. But 10%3 would give 1. The difficulty is in applying two separate formulas to the same base roll and displaying them independently. The macro I listed would be equivalent to "GP floor(10/3)=3" and "SP (3.33-3)*10=33"
1692289857
Gauss
Forum Champion
Zack said: Gauss said: Hi Zack, could you give the original math of what you are trying to do on the gold and silver values?  The full macro is stupidly complex, but what I'm basically trying to do would be something like this; &{template:default} {{name=Calculate Cost}} {{GP=[[floor([[1d100/5]])]]}} {{SP=($[[0]]-$[[1]])*10}} So I'm basically just trying to split off the decimal value and print it separately as a whole number. Math, not macro please. Math as if Roll20 didn't exist. 
1692290069

Edited 1692290254
GiGs
Pro
Sheet Author
API Scripter
Zack said: The difficulty is in applying two separate formulas to the same base roll and displaying them independently. The macro I listed would be equivalent to "GP 3" and "SP (3.33-3)*10=33" You can multiply the modulo by a factor. But as gauss says, we need the complete explanation without Roll20 involved. Then someone might figure out a way to do it. There's a good chance you simply can't do what you're trying to do in Roll20 (at least with this approach), but we don't know for sure yet.
1692290406

Edited 1692290646
Zack
Marketplace Creator
GiGs said: Zack said: The difficulty is in applying two separate formulas to the same base roll and displaying them independently. The macro I listed would be equivalent to "GP 3" and "SP (3.33-3)*10=33" You can multiply the modulo by a factor. But as gauss says, we need the complete explanation without Roll20 involved. Then someone might figure out a way to do it. I thought I had. If I just did 10/3, it would come out with 3.33 gold pieces. What I want instead is to display 3 gold pieces and 33 silver pieces. I could try breaking that down into copper as well, but I'm already stuck on silver. Normally I could do that by taking taking the initial 10/3=3.33... For the GP I floor it to remove the decimal, getting 3. For the SP, I remove the whole number by taking the floor result (3) and subtracting it from the original result (3.33...), which gives 0.33... I then multiply that by 10 to get 33.33... and floor that to get 33. Displaying either 3 or 33 is easy enough, but not both .
1692292214
GiGs
Pro
Sheet Author
API Scripter
It sounds like yiu are given a value, and want to convertthat value into its equivalent GP, Sp, CP, etc. Is that correct? How do you get that starting value?
1692293315

Edited 1692293706
Zack
Marketplace Creator
GiGs said: It sounds like yiu are given a value, and want to convertthat value into its equivalent GP, Sp, CP, etc. Is that correct? How do you get that starting value? That's where things get very complicated. Any starting value should work, but mine is part of a crafting macro for custom crafting rules on an Artificer. Instead of just doing x GP per day, they roll tool proficiency. The item has a DC based on the cost to make it. Normally there's a large DC that requires multiple rolls across multiple days, but there's a fringe case here in which consumables with an already low cost like potions, with a rarity low enough to fall under Magic Item Adept for reduced cost and DC, allow the Artificer to make multiple in a single roll. With that in mind I'm streamlining the macro by having a dropdown list of consumables to determine cost, calculating the DC internally based on cost, displaying the upper limit of how many can be made based on the roll by dividing the roll by the DC, displaying the cost per item and the total cost of making the upper limit number of items by multiplying the cost by the items made. It could be done by hand, but it really speeds things up to just have the macro do it automatically. Right now it's just displaying the GP cost with fractional values. So let's say you are making a Potion of Healing with DC 3 and roll 23 with the Alchemist's Tools. That means you can make up to 7 potions over a single day as this level 10+ Artificer. The potions cost 12.5gp each (50, divided by half for crafting, divided by half again from Magic Item Adept), so if you make 7 it costs 87.5gp. Ideally I'd display that 12.5gp and 87.5gp as 12gp 5sp and 87gp 5sp instead.
1692304534
timmaugh
Forum Champion
API Scripter
Just in case scripts are available (or someone else finds this conversation who *does* have scripts available)... this is relatively straightforward with metascripts. Here are a couple ways to do it, with the required scripts noted for each: !&{template:default} {{name=Calculate Cost}} {{GP=[[floor([[1d100/5]])]]}} {{SP=[\][\]round(($[[0]]-$[[1]])*10)\]\]}} {&simple}   ...REQUIRES: ZeroFrame !&{template:default} {{name=Calculate Cost}} {{GP=[[floor([[1d100/5]])]]}} {{SP={&math round(($[[0]].value-$[[1]].value)*10)} }}{&simple} ...REQUIRES: ZeroFrame, MathOps