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

Checkbox: Changing Dice Rolled

1588710525

Edited 1588714549
Eli N.
Pro
Compendium Curator
Hey there, so how would one create a checkbox that modifies the dice rolled? Typically the dice rolled is a 1d20cs>19cf1cf19, but with this box checked I need to roll a d10!pcs>19cf1cf19. This is what I attempted to do. I had the checkbox like this and  <div class="sheet-col-1-16"><input type="checkbox" name="attr_def_dice"  /></div> And the Roll Button like this  <div class="sheet-col-1-16"><button type='roll' name='roll_weapon_defense' value='&{template:hm_std} {{name=@{character_name} defends!}} {{weapon=@{weapon}}} {{def_roll=[[@{def_dice_mod} +@{weapon_defense_mod_total}]]}} {{dr=[[@{dr}]]}} {{sdr=[[@{shield_dr}]]}}'>DEF</button></div> I defined def_dice_mod as <input type="hidden"  name="attr_def_dice_mod" /> My script worker is the following on("change: def_dice sheet:opened", function() {         getAttrs(["def_dice"], function(values) {       if (value.def_dice= 0) {       setAttrs({def_dice_mod: "1d20cs>19cf1cf19"});       }       else if (value.def_dice= 1) {           setAttrs({def_dice_mod: "1d10!pcs>19cf1cf19"});       }     }); }); The error is it isn't registering that that is a roll. It just gives me a zero. 
1588714310
GiGs
Pro
Sheet Author
API Scripter
I dont see a def_dice_mod in the html you posted either, nor do i see anything in the roll button that calls it. Also your checkbox has a value of -10, but in the sheet worker you are looking for a value of 1. Also you sheet worker is looking for an attribute named form_switch, but your getAttrs doesnt mention it, nor is it listed in the html you posted. Also there are two more issues with the if statement, which I'll explain after the code below. Here's a way to do it. First the inputs: set yup a checkbox as you have to handle the switch. Note the name change. And add a hidden attribute that will hild the switched dice. <input type="checkbox" name="attr_def_dice_switch" value="1" /> <input type="hidden" name="attr_def_dice" value=" 1d20cs>19cf1cf19 " /> Change your button to refer to this <button type='roll' name='roll_weapon_defense' value='&{template:hm_std} {{name=@{character_name} defends!}} {{weapon=@{weapon}}} {{def_roll=[[@{def_dice} +@{weapon_defense_mod_total}]]}} {{dr=[[@{dr}]]}} {{sdr=[[@{shield_dr}]]}}'>DEF</button> Now correct the sheet worker on("change: def_dice_switch", function() {         getAttrs(["def_dice_switch"], function(values) {                     var switch = parseInt(values.def_dice_switch) || 0;                     var dice = switch ? "1d10!pcs>19cf1cf19" : "1d20cs>19cf1cf19";                     setAttrs({                              def_dice: dice                     });     }); }); Attributes in character sheets are stored as strings, not numbers, so using "if (attribute = something)" will usually fail because 1 does not equal "1". You have to convert them to a number first. Thats what the praseInt line above does. Secondly in javascript, = always assigns a value. To compare values, you have to use == or ===. This line:                     var dice = switch ? "1d10!pcs>19cf1cf19" : "1d20cs>19cf1cf19"; Is the same as doing                     var dice = '';                     if(switch === 1) {                         dice = "1d10!pcs>19cf1cf19";                     } else {                         dice = "1d20cs>19cf1cf19";                     } Notice how i use the triple equality here. That is the proper way to compare values in JS, but the values have to be of the same tpe (equal, and both numbers). The one line version is a lot more compact though...
1588714696

Edited 1588714712
Eli N.
Pro
Compendium Curator
Since I posted this I found like 12 errors and had been trying to update my post every time I found one. I already got rid of the value= -10 and I already found the form_switch error.  I will read through your response, and try those thing as well. Thanks!
1588715079

Edited 1588717416
Eli N.
Pro
Compendium Curator
It is still not working for me My values:             <input type="hidden" name="attr_def_dice" value="1d20cs>19f1cf19"/>                          <div class="sheet-col-1-16"><input type="checkbox" name="attr_def_dice_switch" value="1"/></div> The roll: <div class="sheet-col-1-16"><button type='roll' name='roll_weapon_defense' value='&{template:hm_std} {{name=@{character_name} defends!}} {{weapon=@{weapon}}} {{def_roll=[[@{def_dice} +@{weapon_defense_mod_total}]]}} {{dr=[[@{dr}]]}} {{sdr=[[@{shield_dr}]]}}'>DEF</button></div> The worker <script type="text/worker">     on("change: def_dice_switch sheet:opened", function() {             getAttrs(["def_dice_switch"], function(values) {                         var switch = parseInt(values.def_dice_switch) || 0;                         var dice = switch ? "1d10!pcs>19cf1cf19" : "1d20cs>19cf1cf19";                         setAttrs({                                 def_dice: dice                         });             });     }); </script>
1588716340
GiGs
Pro
Sheet Author
API Scripter
Probably because of the space on this line  on("change: def_dice_switch sheet:opened", function() { It should be  on("change:def_dice_switch sheet:opened", function() { We all make these mistakes, lol.
1588716813
Eli N.
Pro
Compendium Curator
It is giving me zero every time I roll it. Checked or unchecked (even with the space removed)
1588717211
GiGs
Pro
Sheet Author
API Scripter
Where is it giving you zero?
1588717331
GiGs
Pro
Sheet Author
API Scripter
Oh I misread. Change your button value to this for testing value='/roll @{def_dice}' just to confirm the issue is in the dice calculation, and not somewhere else in the roll.
1588717604

Edited 1588717932
Eli N.
Pro
Compendium Curator
Zero with the box checked and unchecked I even tried just "1d20" for defense dice and removed the worker script and it still rolls a 0
1588717934
GiGs
Pro
Sheet Author
API Scripter
Oops, my sheet worker used a reserved keyword. You cant use switch  as a variable name. It should be on("change:def_dice_switch", function() {         getAttrs(["def_dice_switch"], function(values) {                     var check = parseInt(values.def_dice_switch) || 0;                     var dice = check ? "1d10!pcs>19cf1cf19" : "1d20cs>19cf1cf19";                     setAttrs({                              def_dice: dice                     });     })
1588718091
Eli N.
Pro
Compendium Curator
Still zero
1588718848

Edited 1588718894
GiGs
Pro
Sheet Author
API Scripter
I just copied this into an existing sheet: <input type="checkbox" name="attr_def_dice_switch" value="1" /> <input type="hidden" name="attr_def_dice" value="1d20cs>19cf1cf19" /> <div class="sheet-col-1-16"><button type='roll' name='roll_weapon_defense' value='/roll @{def_dice}'>DEF</button></div> And this in the script block on("change:def_dice_switch", function() {         getAttrs(["def_dice_switch"], function(values) {                     var check = parseInt(values.def_dice_switch) || 0;                     var dice = check ? "1d10!pcs>19cf1cf19" : "1d20cs>19cf1cf19";                     setAttrs({                              def_dice: dice                     });     }) And its working fine. You need to check you have copied everything properly, and that no other code is interfering. Even if it wasnt working, it wouldnt be showing 0 in the chat window. So there is likely something wrong with your button value.
1588719272
Eli N.
Pro
Compendium Curator
Just to make sure I am doing this write, what do you mean by script block?
1588720008
GiGs
Pro
Sheet Author
API Scripter
The section in your html that looks like this <script type="text/worker"> </script> You should have one section like that, and all your sheet workers go inside it. People usually put it at the start or end of the html, to make it easy to find (I put mine at the end). But it can be anywhere.
1588720047
Kraynic
Pro
Sheet Author
Neat.  I will probably be needing something like this soon. 
1588720401

Edited 1588721208
Eli N.
Pro
Compendium Curator
So the problem seem to be that it's not finding @{def_dice}. I switched the variable names and it's printing the right number, but the check box is not switching values. 
1588721858

Edited 1588730031
GiGs
Pro
Sheet Author
API Scripter
Do you have another attribute with that name in the sheet? Try changing the def-dice name like so <input type="checkbox" name="attr_defence_dice_switch" value="1" /> <input type="hidden" name="attr_defence_dice" value="1d20cs>19cf1cf19" /> <div class="sheet-col-1-16"><button type='roll' name='roll_weapon_defense' value='/roll @{defence_dice}'>DEF</button></div> And this in the script block on("change:defence_dice_switch", function() {         getAttrs(["defence_dice_switch"], function(values) {                     var check = parseInt(values.defence_dice_switch) || 0;                     var dice = check ? "1d10!pcs>19cf1cf19" : "1d20cs>19cf1cf19";                     setAttrs({                              defence_dice: dice                     });     }); }); Dont manually change the names, but copy and paste directly from here. It's easy to miss some instances of the names if you do it manually.
1588722122

Edited 1588722175
Eli N.
Pro
Compendium Curator
It's working correctly on rolling   1d20cs>19cf1cf19   but it is not switching to the " 1d10!pcs>19cf1cf19" So I assume thats an error with the worker script? I copied it exactly 
1588724383
GiGs
Pro
Sheet Author
API Scripter
I dont see how it would be, because its working at my end. It could be a problem with the checkbox. Is that using the new name?
1588724581
Eli N.
Pro
Compendium Curator
Yeah I copied it exactly 
1588724987
Eli N.
Pro
Compendium Curator
Is it something stupid like all scripts should be indented?
1588725129
GiGs
Pro
Sheet Author
API Scripter
No, white space like indents doesnt matter in javascript. Can you post your entire sheet's HTML and CSS to pastebin, and link it here?
1588726669

Edited 1588726729
Eli N.
Pro
Compendium Curator
Here is the HTML&nbsp; <a href="https://pastebin.com/GiesBFBz" rel="nofollow">https://pastebin.com/GiesBFBz</a> CSS&nbsp; <a href="https://pastebin.com/YdumKy37" rel="nofollow">https://pastebin.com/YdumKy37</a>
1588729109
Kraynic
Pro
Sheet Author
Just thought I should say that the snippets posted don't work for me either.&nbsp; Maybe I copied something wrong? &lt;input type="checkbox" name="attr_defence_dice_switch" value="1" /&gt; &lt;input type="hidden" name="attr_defence_dice" value="1d20cs&gt;19cf1cf19" /&gt; &lt;div class="sheet-col-1-16"&gt;&lt;button type='roll' name='roll_weapon_defense' value='/roll @{defence_dice}'&gt;DEF&lt;/button&gt;&lt;/div&gt; &lt;script type="text/worker"&gt; on("change:defence_dice_switch", function() { getAttrs(["defence_dice_switch"], function(values) { var check = parseInt(values.defence_dice_switch) || 0; var dice = check ? "1d10!pcs&gt;19cf1cf19" : "1d20cs&gt;19cf1cf19"; setAttrs({ defence_dice: dice }); }) &lt;/script&gt; These rolls are unchecked, checked, unchecked.
1588730072
GiGs
Pro
Sheet Author
API Scripter
I somehow missed the last line of brackets in the last version when copying it: I've edited the post and fixed the code.
1588737072
Eli N.
Pro
Compendium Curator
It is still not working
1588739706
GiGs
Pro
Sheet Author
API Scripter
Looking at your sheet code I notice some important information: it's in a repeating section. That changes the code the sheet worker needs to work, and explains why it was working for me and not you. Try this: on("change:repeating_weapon:defence_dice_switch",&nbsp;function()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getAttrs(["repeating_weapon_defence_dice_switch"],&nbsp;function(values)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;check&nbsp;=&nbsp;parseInt(values.repeating_weapon_defence_dice_switch)&nbsp;||&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;dice&nbsp;=&nbsp;check&nbsp;?&nbsp;"1d10!pcs&gt;19cf1cf19"&nbsp;:&nbsp;"1d20cs&gt;19cf1cf19"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setAttrs({ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;repeating_weapon_defence_dice:&nbsp;dice &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;}); });
1588740987
Eli N.
Pro
Compendium Curator
We are in business baby! I can't thank you enough!&nbsp;
1588741550
Eli N.
Pro
Compendium Curator
Now does this mean I should change the attr_defence_dice to attr_repeating_weapon_defense_dice?&nbsp;
1588744307

Edited 1588744465
GiGs
Pro
Sheet Author
API Scripter
No, leave that as it is. Repeating section names are weird. A true repeating section name looks something like repeating_weapon_-56GH6TYU89_defence_dice It is made of three parts: The repeating section name forms the first part An id for the row is the middle part. If a repeating_weapons section contains 3 weapons, how does roll20 know which weapon to use? That's where the row id comes in. and finally, the name you give a specific attribute (name="attr_defence_dice") tells which attribute in the repeating section is being used. There are some situations where you need to puts those bits together&nbsp;manually. But when you have a checkbox that only affects another attribute on the same row, as in this case, roll20 can manage it automatically. In the sheet worker, you give it the repeating section name, and the particular attribute, and it secretly supplies the row id behind the scenes to build this full attribute name - but you dont see that happening.&nbsp; If you were to change the attr_defence_dice to attr_repeating_weapon_defense_dice roll20 would create an attribute name like repeating_weapon_-56GH6TYU89_repeating_weapon_defence_dice and the sheet worker wouldnt work. I hope this explains things. repeating sections are complicated!