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: Using an API to switch second value

1605552071
Eli N.
Pro
Compendium Curator
So I have a drop down box that asks if you are using a shield with the current weapon, this is under a repeating section repeating_weapon             <input type="hidden" name="attr_shield_dr_inuse" value="0" />             <div class="sheet-col-1-16" title="Choose if this weapon uses a shield">         <select name="attr_shield_def_switch">     <option value="0">N</option>     <option value="@{shield_defense_bonus}">Y</option> </select>     </div> This variable is outside the repeating section  <div class="sheet-col-1-8"><input type="number" name="attr_shield_dr" value="0"/></div> However I want to change shield_dr_inuse as well. I was just trying to do a simple script worker just to make sure it could read shield_def_switch and change the values, and I couldn't get this to work.  on("change:repeating_weapon:shield_def_switch", function() {         getAttrs(["repeating_weapon_shield_def_switch"], function(values) {             const sds = int(values.repeating_shield_def_switch");             console.log(sds);             if (sds == 0 ) dr_value= "-99";             else dr_value="1";         setAttrs({             repeating_weapon_shield_dr_inuse: dr_value         });     }); }); I know it's probably something simple that I am making a mistake on here.
1605552606
GiGs
Pro
Sheet Author
API Scripter
This line const sds = int(values.repeating_shield_def_switch"); should probably be const sds = int(values.repeating_weapon_shield_def_switch");
1605555394
Eli N.
Pro
Compendium Curator
I also noticed the " in that as well. Made those 2 changes.  It now properly reads the "-99" line but doesn't switch to the 1 value when the other option is chosen on("change:repeating_weapon:shield_def_switch", function() {         getAttrs(["repeating_weapon_shield_def_switch"], function(values) {             const sds = int(values.repeating_weapon_shield_def_switch);             console.log(sds);             if (sds === 0 ) dr_value= "-99";             else dr_value="1";         setAttrs({             repeating_weapon_shield_dr_inuse: dr_value         });     }); });
1605555516
Eli N.
Pro
Compendium Curator
Here is a question, does the script worker calculate the right value if one of the values of shield_def_switch is   "@{shield_defense_bonus}" ?
1605555945
GiGs
Pro
Sheet Author
API Scripter
Eli N. said: Here is a question, does the script worker calculate the right value if one of the values of shield_def_switch is   "@{shield_defense_bonus}" ? No, sheet workers will read that as the text "@{shield_defense_bonus}", and not as the value it represents.
1605556077
Eli N.
Pro
Compendium Curator
That's probably where the error is right? Because it only reads values of zero. So how do I fix that? 
1605556548
GiGs
Pro
Sheet Author
API Scripter
The best way is to get the attribute value directly, by adding an entry for it in the on{'change line, and also in the getAttrs. Then in the code, get the value when you need it. I cant post a full code fix because I'm not sure how it is used, or what your sheet worker is doing honestly (why is a value being set to -99?). But if you describe it in more detail, I can do that.
1605557062
Eli N.
Pro
Compendium Curator
The numbers were just so I could figure out if the script worker was working, building blocks to success. That way I don't build it and not know my error was way at the beginning.  So ideally the code looks something like this  on("change:repeating_weapon:shield_def_switch", function() {         getAttrs(["repeating_weapon_shield_def_switch"], function(values) {             const sds = int(values.repeating_weapon_shield_def_switch);             console.log(sds);             if (sds === 0 ) dr_value= "0"; #meaning no shield is in use so the DR from a shield is 0             else dr_value="@{shield_dr}"; #meaning a shield is in use so use the DR value of the shield         setAttrs({             repeating_weapon_shield_dr_inuse: dr_value         });     }); }); So it checks to see if a shield is being used by referencing this drop down             <div class="sheet-col-1-16" title="Choose if this weapon uses a shield">         <select name="attr_shield_def_switch">     <option value="0">N</option>     <option value="@{shield_defense_bonus}">Y</option> </select>     </div> But the problem is that the script worker reads @{shield_defense_bonus} as exactly that and not the value. So how can I change the script worker so that if "0" is chosen it spits out 0 and if "@{shield_defense_bonus}" is chosen then it spits out the "@{shield_dr}"
1605558489
GiGs
Pro
Sheet Author
API Scripter
I would change this value <option value="@{shield_defense_bonus}">Y</option> to <option value="1">Y</option> And change your sheet worker to something like (notice how it grabs the shield value in the getAttrs line) on("change:repeating_weapon:shield_def_switch", function() {         getAttrs(["repeating_weapon_shield_def_switch", "shield_defense_bonus"], function(values) {             const toggle = int(values.repeating_weapon_shield_def_switch);             const shield = int(values.shield_defense_bonus);             const dr_value = toggle ? shield : 0;         setAttrs({             repeating_weapon_shield_dr_inuse: dr_value         });     }); }); There's one line to call out:             const dr_value = toggle ? shield : 0; This says if(toggle > 0) set dr_value to shield value, else set it to 0. This is a ternary operator, and they are a very compact replacement for if statements, especially ones where you a setting a value that has exactly two possibilities. The problem with this worker is it doesnt handle changes in the actual shield value outside the repeating section. If the shield value changes, then every row in the repeating section which has a shield set, needs to have its value updated. So you need a more complex sheet worker, like this one, which uses the getSectionIDs function to address every row in the repeating section: on ( "change:repeating_weapon:shield_def_switch change:shield_defense_bonus" ,  function  () {      getSectionIDs ( 'repeating_weapon' ,  ids   =>  {          const   fieldnames  = [];          ids . forEach ( id   =>   fieldnames . push ( `repeating_weapon_ ${ id } _shield_def_switch` ));          getAttrs ([... fieldnames ,  "shield_defense_bonus" ],  function  ( values ) {              const   shield  =  int ( values . shield_defense_bonus );              const   output  = {};              ids . forEach ( id   =>  {                  const   toggle  =  int ( values [ `repeating_weapon_ ${ id } _shield_def_switch` ]);                  const   dr_value  =  toggle  ?  shield  :  0 ;                  output [ `repeating_weapon_ ${ id } _shield_dr_inuse` ] =  dr_value ;             });              setAttrs ( output );         });     }); });
1605560065
Eli N.
Pro
Compendium Curator
So by switching this <option value="@{shield_defense_bonus}">Y</option> to <option value="1">Y</option> I would then have to have the script worker handle shield_defense_inuse.  In my previous code, this was just an autocalc where essentially shield_defense_inuse= @{shield_def_switch}  So I can see how to change most of the first half, on ( "change:repeating_weapon:shield_def_switch change:shield_defense_bonus change:shield_dr" ,  function  () {      getSectionIDs ( 'repeating_weapon' ,  ids   =>  {          const   fieldnames  = [];          ids . forEach ( id   =>   fieldnames . push ( `repeating_weapon_ ${ id } _shield_def_switch` ));          getAttrs ([... fieldnames ,  "shield_defense_bonus","shield_dr" ],  function  ( values ) {              const   shield1  =  int ( values . shield_dr );              const   shield2  =  int ( values . shield_defense_bonus );              const   output  = {};              ids . forEach ( id   =>  {                  const   toggle  =  int ( values [ `repeating_weapon_ ${ id } _shield_def_switch` ]);                  const   dr_value  =  toggle  ?  shield  :  0 ;                  output [ `repeating_weapon_ ${ id } _shield_dr_inuse` ] =  dr_value ;             });              setAttrs ( output );         });     }); }); but the back half I don't quite know how to tweak. The toggle should stay the same right?  Would it be something like this?     const dr_value= toggle? shield1 ;0;              const def_value= toggle? shield2 ; 0 ; output [ `repeating_weapon_ ${ id } _shield_dr_inuse` ] =  dr_value ;     output [ `repeating_weapon_ ${ id } _shield_def_inuse` ] =  def_value ;
1605560970
GiGs
Pro
Sheet Author
API Scripter
You're very close. Note that a ternary operator uses colons (:) not semi-colons (;) just before the final value. So that should be     const dr_value= toggle? shield1 : 0;              const def_value= toggle? shield2 : 0 ; output[`repeating_weapon_${id}_shield_dr_inuse`] = dr_value;     output[`repeating_weapon_${id}_shield_def_inuse`] = def_value; Since you're getting multiple attributes, I'd condense and rewrite a bit more descriptively like this: on ( "change:repeating_weapon:shield_def_switch change:shield_defense_bonus change:shield_dr" ,  function  () {      getSectionIDs ( 'repeating_weapon' ,  ids   =>  {          const   fieldnames  = [];          ids . forEach ( id   =>   fieldnames . push ( `repeating_weapon_ ${ id } _shield_def_switch` ));          getAttrs ([... fieldnames ,  "shield_defense_bonus" , "shield_dr" ],  function  ( values ) {              const   shield_dr  =  int ( values . shield_dr );              const   shield_def  =  int ( values . shield_defense_bonus );              const   output  = {};              ids . forEach ( id   =>  {                  const   toggle  =  int ( values [ `repeating_weapon_ ${ id } _shield_def_switch` ]);                  output [ `repeating_weapon_ ${ id } _shield_dr_inuse` ] =  toggle  ?  shield_dr  :  0 ;                  output [ `repeating_weapon_ ${ id } _shield_def_inuse` ] =  toggle  ?  shield_def  :  0 ;             });              setAttrs ( output );         });     }); });
1605564687
Eli N.
Pro
Compendium Curator
You are an angel
1605565392
GiGs
Pro
Sheet Author
API Scripter
hehe, thank you!