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

Help tracking a sheetworker script

I've been working on my character sheet, and have run into a snag.  I'm trying to pass a checkbox value (in a repeating item section) to a roll template as part of a roll and change the output in the chat window.  I'm not sure if this is even a sane way to do what I'm trying to accomplish, but it was built from an example sheet that I had access to.  I suspect I'm not referencing repeating item variables correctly in the sheetworker script, but I'm not sure.  Here is the relevant code:  The relevant Character Sheet HTML: < div class = "injury" > < span class = "label-100px heading" > Injury </ span > < span class = "label-100px heading" > Severity </ span > < span class = "label-50px heading" > Level </ span > < span class = "label-50px heading" > Points </ span > < span class = "label-25px heading" style = " font-family: pictos " > S </ span > < span class = "label-100px heading" > Bleeding Rate </ span > < span class = "label-100px heading" > Location(s) </ span > < span class = "label-100px heading" > Type </ span > < span class = "label-25px heading" > Inf </ span >      < span class = "label-100px heading" > Recovery </ span > < fieldset class = "repeating_injury" > < input type = "text" name = "attr_injury-name" class = "label-100px" > < select name = "attr_injury-severity" class = "label-100px" > < option value = "Superficial (s)" > Superficial </ option > < option value = "Minor (M)" > Minor </ option > < option value = "Serious (S)" > Serious </ option > < option value = "Critical (C)" > Critical </ option > < option value = "Greivous (G)" > Greivous </ option > < option value = "Mortal (K)" > Mortal </ option > </ select >   < input type = "number" name = "attr_injury-level" min = "0" max = "9" value = "0" >   < input type = "number" name = "attr_injury-points" min = "0" max = "99" value = "0" >     < input type = "checkbox" name = "attr_injury-enchanted" class = "enchanted" >< span ></ span > < select name = "attr_injury-bleeder" class = "label-100px" > < option value = "0" > ○○○○○○ </ option > < option value = "1" > ○○○○○● </ option > < option value = "2" > ○○●○○● </ option > < option value = "3" > ○●○●○● </ option > < option value = "4" > ○●●○●● </ option > < option value = "5" > ○●●●●● </ option > < option value = "6" > ●●●●●● </ option > </ select > < input type = "text" name = "attr_injury-location" class = "label-100px" > < input type = "text" name = "attr_injury-type" class = "label-100px" > < span class = "label-12px" >   </ span > < input type = "checkbox" name = "attr_injury-infected" class = "infected" >< span ></ span > < input type = "hidden" name = "attr_injury-infected-feedback" value = "0" readonly > < span class = "label-10px" >   </ span > < input type = "number" name = "attr_injury-hr" min = "0" max = "7" value = "0" > < button type = "roll" class = "button-roll" value = "&{template:rolltest} {{character_name=@{character_name}}} {{infected=[[1]]}} {{rolldesc=Recovers @{injury-name}}} {{rolltarget=[[{[[{[[[[@{injury-hr}*@{endurance} ]]- [[(@{fatigue})*5]] + [[@{modifier}]]]],5}kh1]],95}kl1 (HR)]]}} {{penalty=[[(@{fatigue})*5 (Fatigue Penalty)]]}} {{rollresult=[[1d100cs5cs10cs15cs20cs25cs30cs35cs40cs45cs50cs55cs60cs65cs70cs75cs80cs85cs90cs95cs100]]}} {{targetmod=[[@{modifier} (@{modifier|max})]]}} {{ml=[[@{injury-hr}*@{endurance} (@{injury-hr} x END)]]}} {{note=@{injury-severity} @{injury-type} to the @{injury-location}: @{injury-level} / @{injury-points} }}{{detail=@{injury-details} @{injury-infected} }}" name = "roll_injury-recovery-test" > </ button > <!-- <button type="roll" value="&{template:rollcheck} {{character_name=@{NAME}}} {{rolldesc=rolls a Healing Check}} {{infected=[[@{INJURY_INFECTED_FEEDBACK}]]}} {{rolltarget=[[{[[{[[(@{INJURY_HEALINGROLL} * [[@{COMBAT_ENDURANCE}]]) + ?{Target Modifier?|0}]],5}kh1]],95}kl1]]}} {{penalty=[[0]]}} {{rollresult=[[1d100cs5cs10cs15cs20cs25cs30cs35cs40cs45cs50cs55cs60cs65cs70cs75cs80cs85cs90cs95cs100]]}} {{targetmod=[[?{Target Modifier?|0}]]}} {{ml=[[@{INJURY_HEALINGROLL} * [[@{COMBAT_ENDURANCE}]]]]}} {{note=@{INJURY_LOCATION} @{INJURY_SEVERITY}@{INJURY_LEVEL} H@{INJURY_HEALINGROLL}}}" name="roll_HealingCheck" {{infected=[[@{injury-infected-feedback}]]}} ></button></div> --> < input type = "checkbox" class = "sheet-skill-detail-show gear" />< span ></ span > < div class = "sheet-skill-detail" > < textarea name = "attr_injury-details" class = "textarea-contacts" placeholder = "Injury Details..." ></ textarea > </ div > </ fieldset > </ div > The Javascript: < script type = "text/worker" > on("change:repeating_injury:injury-infected", function() { getAttrs([ "repeating_injury_injury-infected", "repeating_injury_injury-infected-feedback" ], function(values) { var isInfected = (values.repeating_injury_injury-infected == 0 ? 0 : 1); setAttrs({ repeating_injury_injury-infected-feedback: isInfected }); }); }); </ script > And the roll template < rolltemplate class = "sheet-rolltemplate-rolltest" > < table > < tr > < th colspan = "2" >< span class = "tcat" > {{character_name}} {{rolldesc}} </ span ></ th > </ tr > {{#infected}} {{#rollTotal() infected 1}} < tr > < td colspan = "2" class = "sheet-infected" > Infected </ td > </ tr > {{/rollTotal() infected 1}} {{/infected}} <!-- <tr><td><span class="sheet-tcat"> M%  -  GP  +  Mod  =</span></td><td><span class="sheet-tcat"> EML</span></td></tr> --> < tr >< td > {{ml}} - {{penalty}} + {{targetmod}} = </ td >< td > < span class = "sheet-right" > {{rolltarget}} </ span ></ td ></ tr > < tr >< td >< span class = "sheet-tcat" > Roll(1d100): </ span ></ td >< td > {{rollresult}} </ td ></ tr > {{#^rollGreater() rollresult rolltarget}} {{#rollWasCrit() rollresult }} < tr > < td colspan = "2" class = "sheet-crit-success" > Critical Success!! </ td > </ tr > {{/rollWasCrit() rollresult }} {{#^rollWasCrit() rollresult }} < tr > < td colspan = "2" class = "sheet-success" > Marginal Success. </ td > </ tr > {{/^rollWasCrit() rollresult }} {{/^rollGreater() rollresult rolltarget}} {{#rollGreater() rollresult rolltarget}} {{#rollWasCrit() rollresult }} < tr > < td colspan = "2" class = "sheet-crit-failure" > Critical Failure!! </ td > </ tr > {{/rollWasCrit() rollresult }} {{#^rollWasCrit() rollresult }} < tr > < td colspan = "2" class = "sheet-failure" > Marginal Failure. </ td > </ tr > {{/^rollWasCrit() rollresult }} {{/rollGreater() rollresult rolltarget}} {{#note}} < tr > < td colspan = "2" class = "sheet-note" > {{note}} </ td > </ tr > {{/note}} {{#detail}} < tr > < td colspan = "2" class = "sheet-detail" > {{detail}} </ td > </ tr > {{/detail}} </ table > </ rolltemplate > The roll template seems to be working.  I'm able to hard-code the call to the sheetworker with {{infected=[[1]]}} or {{infected=[[0]]}}; it's set to 1 in the code above... With 0 the row is not present and with 1 the infected line is included.  I also toyed with the details field in the very long roll-button value text (Can this not be broken across multiple lines? It's really difficult to edit but breaking lines breaks the rolls...) When I include @{injury-infected} I get either "0" or "on" depending upon whether the rows checkbox is checked or not.  When I include @{injury-infected-feedback} I get 0 either way.  This leads me to believe the error is in the sheetworker script.  Which is supposed to convert the repeating_injury variable injury-infected to injury-infected-feedback as 1 or 0; this is the part that's not working.     Is there a better way to do this?  Thanks.  
1593470528

Edited 1593470584
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
I may be misunderstanding what you're attempting, but why are you setting infected feedback? It's always going to equal the value of infected. A much easier way for this is to just declare what the checked value for @{injury-infected} is. You do that by just adding a value property to its html: < input type = "checkbox" name = "attr_injury-infected" class = "infected" value="1" >< span ></ span > Then, you don't need the infected feedback.
1593470875
GiGs
Pro
Sheet Author
API Scripter
There is a reason the sheet worker isn't working. It's because the attribute name has a hyphen in it, and you are using this syntax: var isInfected = (values.repeating_injury_injury-infected == 0 ? 0 : 1); javascript doesnt like hyphens in variable or property names, so in the above it thinks the attribute is  repeating_injury_injury You can fix this by either removing hyphens from attribute names, or use the bracket syntax: var isInfected = (values['repeating_injury_injury-infected'] == 0 ? 0 : 1); Notice the name is surrounded by quotes (to turn it into a string), and enclosed in square brackets. That said, I'm not understanding the purpose of the -feedback  attribute. If you give the checkbox a value=1, I think you dont actually need it, like <input type="checkbox" name="attr_injury-infected" class="infected" value="1"> Then it will equal 1 when checked, and 0 when unchecked. Then you can use {{infected=[[@{injury-infected}]]}} directly in the roll.
That works much better.  Thank you.  
Yup the sheetworker isn't needed.  I'm adapting a sheet developed by someone else and not everything made sense to me.   What is the best practice for variable naming in repeating items?  I have tried to take into account best practices but have been confused.  I would normally use "attr_injury_infected" but my understanding is that in repeating item sets underscores are problematic.  
1593471947
GiGs
Pro
Sheet Author
API Scripter
Underscores aren't a problem in attribute names inside a fieldset, just the fieldset name itself.  repeating_injury_list for example, is bad. repeating_injury-list or repeating_injurylist is fine. Upper case letters should be avoided in the fieldset name too, but can be used in the inside attributes. (Though generally its best to avoid upper case letters in attribute names altogether.)
So as long as my field set name is repeating_injury each I can use injury_name, injury_severity, injury_level, injury_infected as variables in the set?  I miss-understood. That will make things much easier... 
1593472217
GiGs
Pro
Sheet Author
API Scripter
JPM said: So as long as my field set name is repeating_injury each I can use injury_name, injury_severity, injury_level, injury_infected as variables in the set?  I miss-understood. That will make things much easier...  Yes. I do that myself a lot.
1593486980
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
I typically remove the duplicated part of the attribute from the attribute name, so I'd just do repeating_injury_-jio890zz_name instead of repeating_injury_-jio890zz_injury_name
1593489439
GiGs
Pro
Sheet Author
API Scripter
I include a prefix to avoid multiple event triggers (like something being triggered on name  as well as repeating_injury_-jio890zz_name ). It's not often going to cause issues, but you never know.
1593490395
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Just one of the many reasons you shouldn't have repeating attributes with the same name as a non repeating attribute, but it's a good reason to do the prefix. I just find the repetition in the repeating attribute call annoying.
1593491968
GiGs
Pro
Sheet Author
API Scripter
I admit I find it annoying too :)