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

[Help] [Callback Functions] Calculating Class Levels as a repeating section

My code structure currently looks like this:  Sheet.Calculate.ClassLevelsCallback = function(attributes,classLevelIdsArray){ for(var i = 0; i < classLevelIdsArray.length; i++){ -- Do code for class levels in order; } }; Sheet.Calculate.ClassLevels = function(){ getAttributeArrayForRepeatingSection("classlevels",["clName","clHPRoll","clPSPRoll","clCalculatedHPRoll","clCalculatedHPRoll"],Sheet.Calculate.ClassLevelsCallback); }; on("change:repeating_classlevels",function(eventInfo){ Sheet.Calculate.ClassLevels(); }); function getAttributeArrayForRepeatingSection(sectionName,propertyArray,callback){ getSectionIDsOrdered(sectionName,function(idArray){ var attributeArray = []; for(var i = 0; i < idarray.length; i++){ var id = idarray[i]; for(var j = 0; j < propertyArray.length;j++){ attributeArray.push(`repeating_${sectionName}_${id}_${propertyArray[j]}`); } } getAttrs(attributeArray,callback); }); }; function getSectionIDsOrdered(sectionName, callback, argsArray) {   'use strict';   getAttrs([`_reporder_${sectionName}`], function (v) {     getSectionIDs(sectionName, function (idArray) {       let reporderArray = v[`_reporder_${sectionName}`] ? v[`_reporder_${sectionName}`].toLowerCase().split(',') : [],         ids = [...new Set(reporderArray.filter(x => idArray.includes(x)).concat(idArray))];       callback(ids);     });   }); }; In getAttributeArrayForRepeatingSection(...) I have an idarray that comes in as a callback arg from getSectionIDsOrdered (found  here ) and I need to pass that into the callback for the getAttrs(...,callback) function. There is certain logic that is only implemented on the first class level (such as getting max HP at level 1), and other house-rule logic that is only implemented based on the order in which class levels are taken. Anyone know how to handle this?
1523902990

Edited 1523903080
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Is this an API script, or a sheetworker in a character sheet?  Nvm, obviously a character sheetworker based on the functions. Also, what info are you getting from the idArray that isn't already present in attributeArray? The attributes array from getAttrs will be ordered the same way as the array it got attributes based off of. EDIT, I would also look at replacing your for loops with _.each() iteration as it is somewhat easier to read (could of course just be personal opinion).
Every Id in the IdArray in this implementation correlates to 5 values in the attributes object in the callback.  If I've already gotten the idArray, I'd rather pass that around than go through Object.keys(attributes) to figure out what my id arrays are. Also, where does it say that the order of Object.keys(attributes) will come back the same as the array submitted in getAttrs?  if that's something the function guarantees then great, but I haven't seen that anywhere. But I think I figured it out... function getAttributeArrayForRepeatingSection(sectionName,propertyArray,callback){ getSectionIDsOrdered(sectionName,function(idArray){ var attributeArray = []; for(var i = 0; i < idarray.length; i++){ var id = idarray[i]; for(var j = 0; j < propertyArray.length;j++){ attributeArray.push(`repeating_${sectionName}_${id}_${propertyArray[j]}`); } } getAttrs(attributeArray,function(attributes){ callback(attributes,idArray); }); }); };
1523903776

Edited 1523904179
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
I don't know that it is documented anywhere, but It certainly is the behavior I see. However, yes, that would be the way to do it.