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

[API][Bug] FindObjs() and getAttrByName() do not update correctly when an attribute add event triggers an script.

1558228287

Edited 1558232541
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
If an API script is listening for attribute add event, the attribute appears differently depending on how you look for it as shown in the list below: The passed Obj:  The obj that is passed as part of the listener is correct and has the correct current/max values getObj():  The newly created attribute is found correctly, and has the correct current/max values findObjs():  The newly created attribute is not found regardless of what search options you use getAttrByName():  The default value of the attribute is found, but not the value that the attribute actually has The Script I've used to check this and compare it to the attribute change behavior is below: on('ready',()=>{ on('add:attribute',(obj)=>{ log(`Added obj current value: "${obj.get('current')}"`); log(`Added getObj current value: "${getObj('attribute',obj.id).get('current')}"`); log(`Added findObjs obj: "${JSON.stringify(findObjs({type:'attribute',characterid:obj.get('characterid'),name:obj.get('name')}))}"`); log(`Added getAttrByName: "${getAttrByName(obj.get('characterid'),obj.get('name'))}"`); }); on('change:attribute',(obj)=>{ log(`changed obj current value: "${obj.get('current')}"`); log(`changed current value: "${getObj('attribute',obj.id).get('current')}"`); log(`changed findObjs obj: "${JSON.stringify(findObjs({type:'attribute',characterid:obj.get('characterid'),name:obj.get('name')}))}"`); log(`changed getAttrByName: "${getAttrByName(obj.get('characterid'),obj.get('name'))}"`); }); }); And then I tested this using the 5e by Roll20 sheet and adding an attack that I then named. This is the output from when I change the atkrange attribute of the repeating row. The "added..." lines are from when I first set the attribute. The "changed..." lines are from when I changed it to a new value after it was already created. "Added obj current value: \"5\"" "Added getObj current value: \"5\"" "Added findObjs obj: \"[]\"" "Added getAttrByName: \"\"" "changed obj current value: \"6\"" "changed current value: \"6\"" "changed findObjs obj: \"[{\"name\":\"repeating_attack_-LfCcU3iFt9KP4JqsWes_atkrange\",\"current\":\"6\",\"max\":\"\",\"_id\":\"-LfCcZR_DpOLjUGzQkLF\",\"_type\":\"attribute\",\"_characterid\":\"-LfCba3cxzSHYzQQ51nK\"}]\"" "changed getAttrByName: \"6\"" EDIT: I'm 90% sure this is because of the indexing that findObjs goes through, and I would guess something similar for getAttrByName. Simply adding a 100ms delay in fixes it with the test sheet.
Hey Scott, Thanks for all of this info! A ticket has been submitted to the devs for further investigation