How to set an attribute using a Sheet Worker

1507805681
Axel
Pro
Sheet Author
Hello! I'm trying to get a sheet to set an attribute based on the value of another attribute, but since there's no easy math function, I think I need a sheet worker script, but I don't quite get how to make it work. Any help would appreciated. What I need is a series of "To Hit"-attributes to conform to the table below, updating based on a "BS"-attribute. As you can see, although there is a certain symmetry, there is no clear math function I could use as far as I know, so I'm at a bit of a loss for how to proceed.
1507809575

Edited 1507809912
Loki
Pro
Sheet Author
Hi. Working with Sheet Workers I recommend you to think about what event your code should listen to. For example, if your Sheet Worker script should be activated if a certain attribute changes, you should specify this as the event: on("change:attackers-bs", function(eventInfo) { }); In this function you can now write down the code that translates the attacker's BS into a certain value. Assuming that the Range in your table is another value that you want to read off of your sheet, you should use the getAttrs-function to get it (and save it in a variable): on("change:attackers-bs", function(eventInfo) { getAttrs(["range", "attacker-bs"], function(values) { var bs = values.attacker-bs; var range = values.range; }); }); Now you can approach the problem in several ways, from a three-dimensional array to a chain of select case-statements to some mix of  math and exceptions (because some of the values are in fact calculatable). Personally I think I'd do something like this: select (true) { case (range >37) { if (bs <= 4) { result = "Miss"; } else if (bs > 4 && bs < 12) { let temp = 11; result = temp - (bs - 5); } else if (bs == 12) { result = 6; } break; } } This is, of course, only a very rough sketched approach, untested and all. But my tip is: Calculate as much as possible. And of course make sure to have a look at the official documentation (available here ). Cheers, Loki
1507814425
Axel
Pro
Sheet Author
Thanks. My plan was to have the sheet display the 5 possible target numbers for a given attribute value. So if BS=4, then display the following on the sheet: BS Rng 2-4 Rng 5-12 Rng 13-24 Rng 25-36 Rng 37+ 4 6 8 9 11 Miss This way, the player will easily see what they need to roll. To do this, I'll need derive five different variables from the parent variable. I'll then use a macro that calls on the "to hit" value based on range that is selected in the macro, as in approx: /r 1d12>?{Range|2-4,@{range1}|5-12,@{range2}|13-24,@{range3}|25-36,@{range4}|37+,@{range5}}
1507815560

Edited 1507815962
Loki
Pro
Sheet Author
Ok. When should this happen? Because if this should be a result of a roll, I'd try to write this directly into a roll template in the HTML part of the sheet (instead of using a Sheet Worker script). Because, I don't know whether you can access the Roll20 chat via a Sheet Worker-Script (and it seems that you want to do this). You could pass the bs-value to the roll template and then check which value it has and post the part of the table into the chat that is applicable. The posted macro would still work if the "range1", "range2" etc. are set before. e.g.: &{template:default} {{name=Test Attack}} {{bs=[[@{bs}+0]]}} <rolltemplate class="sheet-rolltemplate-default"> <table> <tr><th>{{rollname}}</th></tr> <tr><td><span class="tcat">Ranges:</td></tr> {{#rollBetween() bs 4 4}} <!-- content --> {{/rollBetween() bs 4 4}} </table> </rolltemplate> Cheers.
1507822833
Axel
Pro
Sheet Author
Thanks again. This is certainly a solution I could go with, but it would be cool if I could display the numbers on the character sheet as well. Any script I'd use would start with on("change:bs", function() { });
1507823403
Axel
Pro
Sheet Author
A straightforward if/then function would work wonders for what I want I think.
1507828472

Edited 1507828498
Loki
Pro
Sheet Author
Ok, what would be the next step? As I mentioned earlier I'm under the impression that you cannot write something into the game chat. Writing something into the character sheet is no problem. In your if/then construction you can just use the setAttrs-function (see the link to the wiki I've posted earlier) and write the result. e.g. (given output is the attribute of something to put data in): on("change:bs", function(eventInfo) { getAttrs(["bs"], function(values) { var bs = values.bs; if (bs == 4) { setAttrs({ output: "6 8 9 11 Miss" )}; } else if (bs == 5) { setAttrs({ output: "5 7 8 10 11" )}; } }); }); But I'm honest: I don't know if its possible to format that text in any way. Probably via CSS, but I don't know much about that. Cheers.
1507835764
Axel
Pro
Sheet Author
I appreciate the help really, and I don't need to write anything in the game chat. I just need to derive multiple attributes (X, Y, and Z) from one attribute (A). So if A = 4, then X = 6, Y = 8, Z = 9, and so on.