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

[Sheet Worker] Radio Button to trigger a value to use set by a wheetworker

1513358911
MrBS
Pro
Sheet Author
API Scripter
Situation: The rules has a penalty to all action rolls equal to the number of wounds taken; so if someone has taken 3 wounds they are at -3.  This value can be ignored for a scene by spending a plot die or some power uses.  It is also possible that a buff may temporarily increase current wound value resulting in a positive; in this situation the penalty should be set to 0 otherwise it becomes a + to the action roll. I setup a sheetworker where a player can choose to apply the penalty (default) or ignore the penalty (by spending a plot die or using a power) for selecting the minimum value of either a 0 or a negative (wounds - current wounds).  The sheetworker also ensures the penalty is either a 0 or negative value and never a positive through Math.min. In the following the scenario of ignore passes through the 0 properly but the scenario of apply is generating a NaN (not a number) error.  What is wrong with the following. Structured through tables (<td> removed for brevity) I have: <input type="number" name="attr_Wounds" disabled="true" title="Value: 1 + Vigor Passive Modifier." value="((@{VigorModifier} + @{WoundsMod} + 1))"/> <input type="text" name="attr_WoundNotes"/> <!-- if checked set value equal to the penalty --> <input type="radio" value="((@{CurrentWounds}-@{Wounds}))" checked name="attr_WndPenToUse"/> <span>Apply</span> <!-- if checked set value equal to 0 to ignore the penalty  --> <input type="radio" value="0" name="attr_WndPenToUse"/><span>Ignore</span> <!-- manipulate hidden field by sheetworker to ensure the value is either negative or 0; never >0 --> <input type="hidden" name="attr_WoundDiff" value="((@{WndPenToUse}))" /> <!-- set equal to hidden field to prevent a disabled field from being activated (see Roll20 wiki) --> <input type="number" value="((@{WoundDiff}))" disabled="true" name="attr_WoundPenalty" > <script type="text/worker"> // When radio button (wndpentouse) changes, set Wound Penalty (wounddiff input value) to the value from wndpentouse; there are instances when wndpentouse can be positive... in that situation it needs to be 0 on("change:wndpentouse", function() { getAttrs(["wndpentouse", "wounddiff"], function(values) { setAttrs({wounddiff: Math.min(0,values.wndpentouse)}); }); }); Any and all help or suggestions are greatly appreciated! 
1513362587

Edited 1513362783
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
This is your problem right here (bolded): <!-- if checked set value equal to the penalty --> <input type="radio" value="((@{CurrentWounds}-@{Wounds}))" checked name="attr_WndPenToUse"/> <span>Apply</span> <!-- if checked set value equal to 0 to ignore the penalty  --> <input type="radio" value="0" name="attr_WndPenToUse"/><span>Ignore</span> Checking this radio button, sets the value of the attribute to "((@{CurrentWounds}-@{Wounds}))". Note that it sets it to this string, not the value of the formula you have put in, so when you do your Math.min(), call you are doing minimum of 0 (a number) and "((@{CurrentWounds}-@{Wounds}))" (a string), which based on the spec of Math.min , returns a NaN. In order to do this, I would change your sheet worker to this: on("change:wndpentouse", function() { getAttrs(["wndpentouse","currentwounds","wounds"], function(values) { /*Get the values of the wounds attributes that you are going to need to figure in the case of an apply. You don't need to get wounddiff because you aren't actually doing anything with that value other than setting it.*/ if(values.wndpentouse*1!==0){ values.wndpentouse = (values.currentwounds*1||0) - (values.wounds*1||0);//The *1 ensures that these are converted to numbers } setAttrs({wounddiff: Math.min(0,values.wndpentouse)}); }); });
1513402478

Edited 1513402559
MrBS
Pro
Sheet Author
API Scripter
Thanks for identifying the issue; the sheet worker you provided gave me a good starting point to solve it.  The double pipes optional was always setting it to zero because wounds is also an auto-calculated field.  I ended up duplicating the auto-calculation in the sheet worker and everything ended up being hunky dory.  Learned a couple other things from your response as well... so double thanks. Question though:  Without going to the raw number (in my solution I created a var called tempwounds that duplicated the auto-calculated field -- not very resilient to change) how do you pass the value of an auto-calculated field vs the formula? Cheers, Barry
1513403655
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Your sheetworker needs to calculate it each time. Personally I don't use autocalc fields in the sheet I'm working on as any appreciable number of them causes the sheet to freeze whenever it is first opened.
1513403849
MrBS
Pro
Sheet Author
API Scripter
Thanks again.  I am planning on converting the auto-calc fields to sheet workers; the one you helped me with above was my first.