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 restores deleted item.

This sheet worker... // Update a WEAPON'S attack throw, whenever the weapons base attribute is changed. on('change:repeating_weapons remove:repeating_weapons', function (eventInfo) { getAttrs(['repeating_weapons_weapon_attr', 'attack_throw', 'strength_mod', 'dexterity_mod'], function(values) { const theAttributeModifier = Number(values[values.repeating_weapons_weapon_attr]); const theArmorThrow = Number(values.attack_throw) + theAttributeModifier; setAttrs({ repeating_weapons_weapon_throw : theArmorThrow }); // <-- This line keeps "adding" a new, blank item when a remove event happens. }); }); ...works fine, except when a repeating_weapons item is removed. When that happens, the item appears to be deleted, but a new, empty item is added at the same time, making it impossible to remove all the items. If I comment the setAttrs statement, the issue goes away. I assume that setting the attribute on an item ID that's been removed recreates the item? 
1587758912
GiGs
Pro
Sheet Author
API Scripter
omonubi said: If I comment the setAttrs statement, the issue goes away. I assume that setting the attribute on an item ID that's been removed recreates the item?  That makes sense.  It also doesnt make sense to have remove in the change item there. If youre removing an item there's no need to recalculate a throw score, so just remove that section: on('change:repeating_weapons', function (eventInfo) {
1587759090
GiGs
Pro
Sheet Author
API Scripter
Generally speaking, the remove:repeating_  section doesnt belong in workers that use this syntax: repeating_weapons_weapon_attr If you're using getSectionIDs to get each rowid, the remove: can fit, but I cant think of any situations off-hand where you'd use remove: when using the shorthand syntax because by definition, that is changing only the row where changes were triggered. It doesnt make sense to edit a row that has been removed.
I removed the remove event condition (ha - a pun!), but the issue persists. The updated event handler: // SHEET-WORKER: Update a WEAPON:ATTACK THROW, whenever the weapons base attribute is changed. on('change:repeating_weapons', function (eventInfo) { getAttrs(['repeating_weapons_weapon_attr', 'attack_throw', 'strength_mod', 'dexterity_mod'], function(values) { const theAttributeModifier = Number(values[values.repeating_weapons_weapon_attr]); const theArmorThrow = Number(values.attack_throw) + theAttributeModifier; setAttrs({ repeating_weapons_weapon_throw : theArmorThrow }); // <-- Still causing the issue }); }); Is there a different syntax I can try?
1587768189

Edited 1587768222
GiGs
Pro
Sheet Author
API Scripter
Try to make the change event specific to the attribute you are changing on('change:repeating_weapons:weapon_attr', function (eventInfo) {
That fixes the issue, but then the handler is only invoked if "attr_weapon_attr" changes, which isn't optimal - for example, when a new weapon item is added to the repeating section. It might help to see the HTML: <div> <h3>Weapons</h3> <fieldset class="repeating_weapons"> <input type="text" name="attr_weapon_name"/> <span>Stone:</span> <input type="number" value="0" name="attr_weapon_enc"/> <span>Dmg:</span> <input class="shortText60" type="text" name="attr_weapon_dmg"/> <span>Attribute:</span> <select class="weapon_attr" name="attr_weapon_attr"> <option value="strength_mod">STR</option> <option value="dexterity_mod">DEX</option> </select> <span>Throw:</span> <span name="attr_weapon_throw"></span> <button type="roll" value="/roll @{weapon_throw}" name="roll_weapon_throw"></button> </fieldset> <input type="hidden" value="0" name="attr_small_weapon_count"/> <input type="hidden" value="0" name="attr_large_weapon_enc"/> </div> Each repeating_weapons item contains an "attr_weapon_attr" <select> that specifies the character attribute that weapon relies on for its attack bonus. When a new repeating_weapons item is added, this value defaults to the first item in the list ("strength_mod", derived from the character's strength). Since I can't detect the addition of the repeating item, I can't calculate the derivative value of "attr_weapon_throw" (which is partly determined by "attr_weapon_attr") until after something in the item changes. If I make the event handler change you suggest, the only way to trigger the attack throw calculation is by toggling/changing the selected <option> in the <select> statement. It occurs to me I could use some other HTML design element(s) to work-around this, but the way I'd prefer  this to work is for the calculation to trigger on any change to the new item (like entering a name). Thus why I had the more general  on('change:repeating_weapons', function (eventInfo) { So I'm back to wondering if there's something I need to change about the setAttrs() statement, which appears to be interfering with repeating item deletion as described in my initial post. Or should I consider a different approach to the design of the <select>? Thanks, again!
1587823897
GiGs
Pro
Sheet Author
API Scripter
The way I handle problems like this is to add an empty option. <span>Attribute:</span> <select class="weapon_attr" name="attr_weapon_attr">             <option value="-" selected>?</option> <option value="strength_mod">STR</option> <option value="dexterity_mod">DEX</option> </select> and make sure it is selected by default. Then the user has to manually select an option.  But in your case, just adding selected to one of those strength or dex options might work too. Selected means when you create a new item, that option will be selected, and that should trigger the event.
Yep, this is better than continuing to fiddle with event handler. I appreciate it.