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 Scripts: Fetching Highest Armor Value from Repeating Section

I'm been attempting to make the Armor section of the sheet I'm working on slightly more autonomous. The armor section has a repeating section where players can add each piece of item, with their respective values. I've been attempting to get the highest AV from it, and display it somewhere else as a "Total", but it always comes up blank for me. I'm a real beginner at this, so any help is appreciated! on("change:repeating_uheadarmour", function() { on("change:repeating_uheadarmour:uhead_armour_avc", function () { var armourValue; armourValue = Math.max(Number(values.repeating_uheadarmour_uhead_armour_avc)); setAttrs({ uhead_armour_avc_total: armourValue });     }); };
1545616240
GiGs
Pro
Sheet Author
API Scripter
repeating sections are tricky. You need to loop through or filter each of the items. Do you have a different repeating set for each location?
Pretty much, yeah, but as long as I can make the script work I shouldn't have much of a problem handling the rest
1545627005

Edited 1545627095
GiGs
Pro
Sheet Author
API Scripter
This function will do what you need:  on("change:repeating_uheadarmour:uhead_armour_avc remove:repeating_uheadarmour", function () {     getSectionIDs('repeating_uheadarmour', idArray => {         getAttrs(idArray.map(id => `repeating_uheadarmour_${id}_uhead_armour_avc`), v => {             const arr = Object.values(v);             const max = Math.max(...arr);             setAttrs({                 "uhead_armour_avc_total": max             });         });     }); }); It's not easy to explain how it works, I barely understand it myself!
1545627539

Edited 1545627597
GiGs
Pro
Sheet Author
API Scripter
Note that your version had nested Change statement - you can't do that. Explaining the above function briefly: getSectionIds builds an array of all the index values in your repeating section, and calls it idArray . The map  function on the  getAttrs line builds an array of all the attributes with the name  uhead_armour_avc  (remember, there's one on every row of the section), and creates an object (called v  in this case) containing key/value pairs - each attributes name is the key, and its value is the value. The const arr  line creates an array of all the values from the v object: thats an array of all the head armour values from your repeating set. Finally the const max line gets the max value from that array. It's not surprising you were struggling to find a solution here!  Repeating sections are really complex, and require a lot of different javascript techiques when you want to do something involving the whole section (like summing, or finding the max). 
1545628267
GiGs
Pro
Sheet Author
API Scripter
Now, if all of your armour repeating sections are named following the same pattern as above, you dont need to manually create an extra sheet worker for each. You can use a loop, and have one sheet worker for all of them, like so: const armours = ['head','chest','rightleg']; armours.forEach(location => {     on(`change:repeating_u${location}armour:u${location}_armour_avc`, function () {         getSectionIDs('repeating_u${location}armour', idArray => {             getAttrs(idArray.map(id => `repeating_u${location}armour_${id}_u${location}_armour_avc`), v => {                 const arr = Object.values(v);                 const max = Math.max(...arr);                 setAttrs({                     `u${location}_armour_avc_total`: max                 });             });         });     }); }); Just change the locations listed in the armours array at the start. This assumes your repeating sets and individual names are all named in the following pattern: repeating_uheadarmour:uhead_armour_avc repeating_uchestarmour:uchest_armour_avc repeating_urightlegarmour:urightleg_armour_avc etc. If they aren't named consistently, it's a bit trickier.
1545628438
GiGs
Pro
Sheet Author
API Scripter
Note that you could also have a single repeating section, and put all of your armour pieces in it, with different attributes for each location. it would still be possible to get the max for each specific location, if you wanted to do it that way.
Sorry for not replying, but I was doing some good ol' sleep! Thanks a lot for the help, this will take me a long way!