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

GetSectionIDs also asynchronous?

A have a repeater called "repeating_attacks" and inside my big giant sheetworker I need to pull a value from each line inside it, perform a calculation based on some stat values external to the repeater, and then apply the results to a different field back inside.  (Basically determine which stat is used to determine the "to hit" value for the given attack, then find the "to hit" based on that stat, apply other potential bonuses, and update "top hit" for each attack.) To get started I'm trying to just build lists of the source and target column names to be able to feed into GetAttrs and SetAttrs.  I'm using what seems to be a pretty common method to do this:         const toHitValArray = [];         const toHitStatArray = [];         getSectionIDs('attacks', function (ids) {             console.log('==== ids');             console.log(ids);             if (ids.length > 0) {                 ids.forEach(id => toHitValArray.push(                     `repeating_attacks_${id}_tohit`                 ));                 ids.forEach(id => toHitStatArray.push(                     `repeating_attacks_${id}_tohit_bc`                 ));             }         });         console.log('==== toHitValArray');         console.log(toHitValArray);         console.log('==== toHitStatArray');         console.log(toHitStatArray);         With one item in the repeaer, these results show up in the console: ==== toHitValArray eval" class="frame-link-source" draggable="false"> sheetsandboxworker.js :183:11 Array [ ] eval" class="frame-link-source" draggable="false"> sheetsandboxworker.js :184:11 ==== toHitStatArray eval" class="frame-link-source" draggable="false"> sheetsandboxworker.js :185:11 Array [ ] eval" class="frame-link-source" draggable="false"> sheetsandboxworker.js :186:11 ==== ids eval" class="frame-link-source" draggable="false"> sheetsandboxworker.js :171:12 Array [ "-m8liuhc2y7xryhmnzuz" ] So the console.log statements to display the contents of the source/target arrays are firing before the ID fetch and its corresponding console logging.  Do I need to find a way to somehow force GetSectionIDs to finish before proceeding?  (Pretty sure I've seen examples very much like this while scouring the forums - I'm provide an example but the search function seems to be broken for me at the moment.)
1594956107
GiGs
Pro
Sheet Author
API Scripter
You need to put any code dependent on the getSectionIDs inside &nbsp;the getSectionIDs. As you rightly point out, it is an asynchronous function, and any code not inside the function cannot use its values.&nbsp; When you getSectionIDs in a sheet worker, you need to put the getAttrs and setAttrs inside it. If you have multiple getSectionIDs (for different sections) you need to nest them inside each other. The second half of this older post of mine explains how to work with getSectionIDs:&nbsp; <a href="https://app.roll20.net/forum/permalink/8158315/" rel="nofollow">https://app.roll20.net/forum/permalink/8158315/</a> If you post the code for your complete sheet worker, I can point you how to rewrite it.
Okay, putting the code inside the getSectionIDs solves that problem.&nbsp; Now I need to switch to getSectionIDsOrdered, and I haven't seen any examples of its usage on the forums, just the initial definition of the function.&nbsp; Does anyone know of any sheets that use it so I could take a look at them?
1595039701
GiGs
Pro
Sheet Author
API Scripter
Do you need to use it? I've never done anything in a sheet worker where I needed to know the ordering. What's your use case?
1595042023
GiGs
Pro
Sheet Author
API Scripter
I should show to use it just in case you do need it. You would copy the function from the wiki into your script block, and then use it's name instead of getSectionIDs, like so: var getSectionIDsOrdered = function (sectionName, callback) { 'use strict' ; getAttrs ([`_reporder_${sectionName}`], function (v) { getSectionIDs (sectionName, function (idArray) { let reporderArray = v[`_reporder_${sectionName}`]&nbsp;? v[`_reporder_${sectionName}`]. toLowerCase (). split ( ',' )&nbsp;: [], ids = [... new Set(reporderArray.filter(x =&gt; idArray.includes(x)).concat (idArray))]; callback (ids); }); }); }; on('change:repeating_sectionname', function() { &nbsp;&nbsp;&nbsp;&nbsp;getSectionIDsOrdered('sectionname', function(idArray) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// do stuff with idArray &nbsp;&nbsp;&nbsp;&nbsp;}); }); Basically you use it exactly &nbsp;the same way you use getSectionIDs, you just need to put the function from the wiki in your script block.
In the "powers" repeating section earlier in the sheet players can spend points to get to-hit bonuses of various levels of specificity (e.g. "all attacks" for a lot of points, groupings of attacks like "all unarmed combat" for fewer points, or on a specific single attack at the lowest cost.&nbsp; The "attacks" repeater is intended to be a convenient list of common combat actions with To Hit values pre-calculated based on the character's other stats and the bonuses from Powers.&nbsp; Ideally there'd be a nested repeater inside Powers for however many attacks the character has defined, but since repeaters don't nest I simply added fields under Powers for bonuses on All Attacks and attacks 1 through 6 in hopes that would be enough most of the time. So from powers I get totals of the bonuses for All and the 6 numbered attacks, which I then need to use in the calculation of items inside the Attacks repeater in the order shown on screen so it corresponds to the numeric bonuses from Powers. First attack To Hit = (base hit from stats) + "All Attacks" bonus + "Attack 1" bonus Second attack To Hit = (base hit from stats) + "All Attacks" bonus + "Attack 2" bonus etc.