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 Help: Using the state of one attribute to change the value of other attributes.

I'm trying to use a select drop down to change the active state of an array of other attributes,  anyone happen to have an example something like this?
Hey, nevermind, figured it out for myself.  For anyone else looking to do similar here's what it looks like. on("change:logos1_gettinggoodatthis_select", function(eventInfo) { if(eventInfo.newValue == "none")   setAttrs({ logos1_convince_dynamite: 0, logos1_changethegame_dynamite: 0, logos1_facedanger_dynamite: 0, logos1_gotoetotoe_dynamite: 0, logos1_hitallyougot_dynamite: 0, logos1_investigate_dynamite: 0, logos1_sneakaround_dynamite: 0, logos1_taketherisk_dynamite: 0,   }); if(eventInfo.newValue == "convince")   setAttrs({ logos1_convince_dynamite: 1, logos1_changethegame_dynamite: 0, logos1_facedanger_dynamite: 0, logos1_gotoetotoe_dynamite: 0, logos1_hitallyougot_dynamite: 0, logos1_investigate_dynamite: 0, logos1_sneakaround_dynamite: 0, logos1_taketherisk_dynamite: 0,   }); if(eventInfo.newValue == "changethegame")   setAttrs({ logos1_convince_dynamite: 0, logos1_changethegame_dynamite: 1, logos1_facedanger_dynamite: 0, logos1_gotoetotoe_dynamite: 0, logos1_hitallyougot_dynamite: 0, logos1_investigate_dynamite: 0, logos1_sneakaround_dynamite: 0, logos1_taketherisk_dynamite: 0,   }); if(eventInfo.newValue == "facedanger")   setAttrs({ logos1_convince_dynamite: 0, logos1_changethegame_dynamite: 0, logos1_facedanger_dynamite: 1, logos1_gotoetotoe_dynamite: 0, logos1_hitallyougot_dynamite: 0, logos1_investigate_dynamite: 0, logos1_sneakaround_dynamite: 0, logos1_taketherisk_dynamite: 0,   }); if(eventInfo.newValue == "gotoetotoe")   setAttrs({ logos1_convince_dynamite: 0, logos1_changethegame_dynamite: 0, logos1_facedanger_dynamite: 0, logos1_gotoetotoe_dynamite: 1, logos1_hitallyougot_dynamite: 0, logos1_investigate_dynamite: 0, logos1_sneakaround_dynamite: 0, logos1_taketherisk_dynamite: 0,   }); if(eventInfo.newValue == "hitwallyougot")   setAttrs({ logos1_convince_dynamite: 0, logos1_changethegame_dynamite: 0, logos1_facedanger_dynamite: 0, logos1_gotoetotoe_dynamite: 0, logos1_hitallyougot_dynamite: 1, logos1_investigate_dynamite: 0, logos1_sneakaround_dynamite: 0, logos1_taketherisk_dynamite: 0,   }); if(eventInfo.newValue == "investigate")   setAttrs({ logos1_convince_dynamite: 0, logos1_changethegame_dynamite: 0, logos1_facedanger_dynamite: 0, logos1_gotoetotoe_dynamite: 0, logos1_hitallyougot_dynamite: 0, logos1_investigate_dynamite: 1, logos1_sneakaround_dynamite: 0, logos1_taketherisk_dynamite: 0,   }); if(eventInfo.newValue == "sneakaround")   setAttrs({ logos1_convince_dynamite: 0, logos1_changethegame_dynamite: 0, logos1_facedanger_dynamite: 0, logos1_gotoetotoe_dynamite: 0, logos1_hitallyougot_dynamite: 0, logos1_investigate_dynamite: 0, logos1_sneakaround_dynamite: 1, logos1_taketherisk_dynamite: 0,   }); if(eventInfo.newValue == "taketherisk")   setAttrs({ logos1_convince_dynamite: 0, logos1_changethegame_dynamite: 0, logos1_facedanger_dynamite: 0, logos1_gotoetotoe_dynamite: 0, logos1_hitallyougot_dynamite: 0, logos1_investigate_dynamite: 0, logos1_sneakaround_dynamite: 0, logos1_taketherisk_dynamite: 1,   }); });
1520731414
GiGs
Pro
Sheet Author
API Scripter
You can streamline that quite a bit in a couple of ways. This approach shortens it quite a bit.  on("change:logos1_gettinggoodatthis_select", function(eventInfo) { let convince = 0, changethegame = 0, facedanger = 0, gotoetotoe = 0, hitallyougot = 0, investigate = 0, sneakaround = 0, taketherisk = 0; switch(eventInfo.newValue) { case "convince": convince = 1; break; case "changethegame": changethegame = 1; break; case "facedanger": facedanger = 1; break; case "gotoetotoe": gotoetotoe = 1; break; case "hitwallyougot": hitwallyougot = 1; break; case "investigate": investigate = 1; break; case "sneakaround": sneakaround = 1; break; case "taketherisk": taketherisk = 1; break; }   setAttrs({         logos1_convince_dynamite: convince,         logos1_changethegame_dynamite: changethegame,         logos1_facedanger_dynamite: facedanger,         logos1_gotoetotoe_dynamite: gotoetotoe,         logos1_hitallyougot_dynamite: hitwallyougot,         logos1_investigate_dynamite: investigate,         logos1_sneakaround_dynamite: sneakaround,         logos1_taketherisk_dynamite: taketherisk, }); }); You dont need to use switch, you could use If as you did, but the key takeaway is since you are only changing one value in each if statement, you can establish variables to hold the values at the top, run through your ifs changing only the one value you need in each, and then use a single setAttrs statement at the end. another thing it's handy to know is how to use getAttrs. Sometimes when you have one of these functions, you need to get the values of stats that you cant get using eventInfo. So here's a version of that script using if statements and getAttrs. on("change:logos1_gettinggoodatthis_select", function() { getAttrs(['logos1_gettinggoodatthis_select'],function(values) { let convince = 0, changethegame = 0, facedanger = 0, gotoetotoe = 0, hitallyougot = 0, investigate = 0, sneakaround = 0, taketherisk = 0; let selected = values.logos1_gettinggoodatthis_select; if (selected === "convince") convince = 1; else if (selected === "changethegame") changethegame = 1; else if (selected === "facedanger") facedanger = 1; else if (selected === "gotoetotoe") gotoetotoe = 1; else if (selected === "hitwallyougot") hitwallyougot = 1; else if (selected === "investigate") investigate = 1; else if (selected === "sneakaround") sneakaround = 1; else if (selected === "taketherisk") taketherisk = 1; setAttrs({ logos1_convince_dynamite: convince, logos1_changethegame_dynamite: changethegame, logos1_facedanger_dynamite: facedanger, logos1_gotoetotoe_dynamite: gotoetotoe, logos1_hitallyougot_dynamite: hitwallyougot, logos1_investigate_dynamite: investigate, logos1_sneakaround_dynamite: sneakaround, logos1_taketherisk_dynamite: taketherisk, }); }); }); And finally, the way you've named your attributes makes it very easy to use an even more elegant approach.  on("change:logos1_gettinggoodatthis_select", function() { getAttrs(['logos1_gettinggoodatthis_select'],function(values) { let selected = values.logos1_gettinggoodatthis_select; let settings = { logos1_convince_dynamite: 0, logos1_changethegame_dynamite: 0, logos1_facedanger_dynamite: 0, logos1_gotoetotoe_dynamite: 0, logos1_hitallyougot_dynamite: 0, logos1_investigate_dynamite: 0, logos1_sneakaround_dynamite: 0, logos1_taketherisk_dynamite: 0, }; let thisSetting = 'logos1_' + selected + '_dynamite'; if (settings[thisSetting] !== undefined) settings[thisSetting] = 1; setAttrs(settings); }); });
1520769255

Edited 1520773772
Will K.
Sheet Author
Damn, that's a much better looking block of text.  Thanks GG!
Got another one for you.  Similar premise, but this time with 2 select drop-down elements working in tandem on the same array of attributes.  Tried a modified version of your second example, but it doesn't seem to take.  Can you see where I went wrong? on("change:logos1_whostheboss_selectA change:logos1_whostheboss_selectB", function() {     getAttrs(['logos1_whostheboss_selectA', 'logos1_whostheboss_selectB'],function(values) {         let convince = 0,             changethegame = 0,             facedanger = 0,             gotoetotoe = 0,             hitallyougot = 0,             investigate = 0,             sneakaround = 0,             taketherisk = 0;         let selectedA = values.logos1_whostheboss_selectA;          let selectedB = values.logos1_whostheboss_selectB;          if (selectedA === "convince" || selectedB === "convince")             convince = 1;         else if (selectedA ===  "changethegame" || selectedB ===  "changethegame")             changethegame = 1;         else if (selectedA ===  "facedanger" || selectedB ===  "facedanger")             facedanger = 1;         else if (selectedA ===  "gotoetotoe" || selectedB ===  "gotoetotoe")             gotoetotoe = 1;         else if (selectedA ===  "hitwallyougot" || selectedB ===  "hitwallyougot")             hitwallyougot = 1;         else if (selectedA ===  "investigate" || selectedB ===  "investigate")             investigate = 1;         else if (selectedA ===  "sneakaround" || selectedB ===  "sneakaround")             sneakaround = 1;         else if (selectedA ===  "taketherisk" || selectedB ===  "taketherisk")             taketherisk = 1;                  setAttrs({             "logos1_whostheboss_convince_dynamite": convince,             "logos1_whostheboss_changethegame_dynamite": changethegame,             "logos1_whostheboss_facedanger_dynamite": facedanger,             "logos1_whostheboss_gotoetotoe_dynamite": gotoetotoe,             "logos1_whostheboss_hitallyougot_dynamite": hitwallyougot,             "logos1_whostheboss_investigate_dynamite": investigate,             "logos1_whostheboss_sneakaround_dynamite": sneakaround,             "logos1_whostheboss_taketherisk_dynamite": taketherisk,         });     }); });
1520776391
GiGs
Pro
Sheet Author
API Scripter
It looks like a logic problem.  With if else constructs, the function stops checking when it finds the first true item. That's what you want to happen in the first macro, but in this one you need to check every statement because two of them could be true. Assuming this is the issue, you just need to remove "else" from each line: if (selectedA === "convince" || selectedB === "convince")             convince = 1;         if (selectedA ===  "changethegame" || selectedB ===  "changethegame")             changethegame = 1;         if (selectedA ===  "facedanger" || selectedB ===  "facedanger")             facedanger = 1;         if (selectedA ===  "gotoetotoe" || selectedB ===  "gotoetotoe")             gotoetotoe = 1;         if (selectedA ===  "hitwallyougot" || selectedB ===  "hitwallyougot")             hitwallyougot = 1;         if (selectedA ===  "investigate" || selectedB ===  "investigate")             investigate = 1;         if (selectedA ===  "sneakaround" || selectedB ===  "sneakaround")             sneakaround = 1;         if (selectedA ===  "taketherisk" || selectedB ===  "taketherisk")             taketherisk = 1;
There we go, that works.