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] Question about attrLookup()

1543333516
Missingquery
Pro
Sheet Author
API Scripter
I reached out to Aaron about this awhile back, but I haven't heard back yet, so I figured I might put it out in the open so other people can offer help as well! "I'm having an issue with the attrLookup() function you wrote a while back, in which in certain cases it returns the secondmost item in a repeating section instead of the first one. For reference, here's the function: var attrLookup = function(character,name,caseSensitive){ let match=name.match(/^(repeating_.*)_\$(\d+)_.*$/); let returnval; if(match){ let index=match[2], attrMatcher=new RegExp(`^${name.replace(/_\$\d+_/,'_([-\\da-zA-Z]+)_')}$`,(caseSensitive?'i':'')), createOrderKeys=[], attrs=_.chain(findObjs({type:'attribute', characterid:character.id})) .map((a)=>{ return {attr:a,match:a.get('name').match(attrMatcher)}; }) .filter((o)=>o.match) .each((o)=>createOrderKeys.push(o.match[1])) .reduce((m,o)=>{ m[o.match[1]]=o.attr; return m;},{}) .value(), sortOrderKeys = _.chain( ((findObjs({ type:'attribute', characterid:character.id, name: `_reporder_${match[1]}` })[0]||{get:_.noop}).get('current') || '' ).split(/\s*,\s*/)) .intersection(createOrderKeys) .union(createOrderKeys) .value(); if(index<sortOrderKeys.length && _.has(attrs,sortOrderKeys[index])){ returnval = attrs[sortOrderKeys[index]]; if (returnval != undefined){ return returnval; } else { return "" } } } return name; }; It seems to only do this with certain attributes and not others, but not always; I've had the most trouble with the repeating_weapons_wtype attribute on my Fire Emblem Tabletop sheet, but it's occurred sometimes with other attributes. If there's only one repeating section, it seems to work as intended. If you could help me with identifying this issue, I would really appreciate it." Why it does this with some attributes but not others, I have no idea.
1543939000

Edited 1543939029
Missingquery
Pro
Sheet Author
API Scripter
Bumping this so hopefully it can get some more visibility again.
1544011095
The Aaron
Pro
API Scripter
Sorry to have let this one slip through the cracks.  If you manually reorder the repeating section (swap top two rows and then swap them back), do you get the right value? The way getting the order of repeating rows works is extremely janky. If a repeating group has been manually reordered, then the order for the rows is stored in a meta-attribute, otherwise they are in creation order. However, it’s further complicated by the fact that rows added after a manual reorder event are not added to the meta-attribute until the next reorder manual event. When this was originally discussed, Riley said the creation order of row attributes is the order returned by findObjs().  I speculate that this is not as strongly guaranteed as he originally made it out to be. If you manually reorder and it fixes the issue, that’s probably what was causing it and I’ll need to adjust the above to use a different methods of determining the creation order. 
1544024923

Edited 1544024997
Missingquery
Pro
Sheet Author
API Scripter
No, swapping doesn't seem to fix the problem. I tested for before swapping, during, and after, as well as with an additional third row with a differing value from the first two- all of those returned the same attribute in the second row, regardless of positioning.
1544560124
Missingquery
Pro
Sheet Author
API Scripter
Bumping yet again!
1544711757

Edited 1544711994
Ammo
Pro
If you have a known-bad case where is it returning the wrong thing, you could log the _reporder_repeating_weapons attribute and createOrderKeys, plus the key of the first repeating weapon you were expecting to be returned.  Then Aaron and other interested parties could know for sure if findObjs does what is expected. If it does not, it would be nice to know how Roll20's implementation of this dereferencing works.  I assume it does the correct thing in your broken case, if you just call a trivial macro like @{selected|repeating_weapons_$1_wtype}? I was bored and so I disassembled and reassembled this code and found no bugs.
1544744771
The Aaron
Pro
API Scripter
Thanks for looking Ammo!  Sorry I've not ben able to take a look at this.  New job is great, but it really puts a press on my free time... If you want to PM me an invite and GM me, I can try and pop in at some bizarre time and try to recreate the issue.