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

Action Button & Repeating Fields

Hey, Im trying to have 2 actions buttons in a repeating field section. But im having some problems to get it to get the attributes right. So the 2 buttons will increase or decrease attr_mastery_current by 1. But right now it set it to either 1 or -1, so im thinking that my problem is in getting the current attribute. HTML code: <h3>Mastery Points</h3> <fieldset class="repeating_mastery">     <div class="mastery_points">         <span>Type</span>         <input type="text" name="attr_mastery_type">         <span>Max</span>         <input type="number" name="attr_mastery_max">         <span>Current</span>         <button type="action" name="act_mastery-down" class="downaction" style="font-family: 'Pictos'">}</button>         <input type="number" name="attr_mastery_current" value="0">         <button type="action" name="act_mastery-up" class="upaction" style="font-family: 'Pictos'">{</button>     </div> </fieldset> Sheet-worker: on('clicked:repeating_mastery:mastery-up', function(eventInfo) { const rowid = eventInfo.sourceAttribute.split('_')[2]; getAttrs(['repeating_mastery_${rowid}_mastery_current'], function(values) { // get the level stat, and coerce into a numerical value. const current = +values.mastery_current || 0; // increase by 1 const newcurrent = current +1; // save the updated attribute to the sheet setAttrs({ [`repeating_mastery_${rowid}_mastery_current`]: newcurrent }); }); }); on('clicked:repeating_mastery:mastery-down', function(eventInfo) { const rowid = eventInfo.sourceAttribute.split('_')[2]; getAttrs(['repeating_mastery_${rowid}_mastery_current'], function(values) { // get the level stat, and coerce into a numerical value. const current = +values.mastery_current || 0; // decrease by 1 const newcurrent = current -1; // save the updated attribute to the sheet setAttrs({ [`repeating_mastery_${rowid}_mastery_current`]: newcurrent }); }); }); Hope you guys can help me! Thanks
1611945790
GiGs
Pro
Sheet Author
API Scripter
firstly, I wouldnt reccomment ending any attribute name with _current. This might conflict with roll20's own internal management of attributes, where attributes have a current and max property. You dont need to use two different sheet workers for this. Since are using eventInfo, this can tell you which button was clicked. In my experience, the eventInfo.triggerName property is more reliable than eventInfo.sourceAttribute when using click events/action buttons, so I'd recommend switching to that. All that said, I'm pretty sure the thing stopping your sheet workers working is here: getAttrs(['repeating_mastery_${rowid}_mastery_current'], function(values) { When using template literals (the ${rowid} part), you need to use backticks in place of quotes: these `, not these: ' You can find them on UK keyboards to on the top row, to the left of number 1. So that would look like getAttrs([`repeating_mastery_${rowid}_mastery_current`], function(values) {
1611946230

Edited 1611954635
GiGs
Pro
Sheet Author
API Scripter
Assuming you change this line <input type="number" name="attr_mastery_current" value="0"> to this <input type="number" name="attr_mastery" value="0"> here's a sheet worker that handles both up and down buttons: on ( 'clicked:repeating_mastery:mastery-up clicked:repeating_mastery:mastery-down' ,  function  ( eventInfo ) {      const   rowid  =  eventInfo . triggerName . split ( '_' )[ 2 ];           // get last two letters and see if it is 'up', if not it must be a 'down' button.      const   direction  =  eventInfo . triggerName . substr (- 2 ,  2 ) ===  'up'  ?  1  : - 1 ;      getAttrs ([ `repeating_mastery_ ${ rowid } _mastery` ],  function  ( values ) {          // get the level stat, and coerce into a numerical value.          // You need to use the full repeating section attribute name.          const   current  = + values [ `repeating_mastery_ ${ rowid } _mastery` ] ||  0 ;          // modify by 1 depending on direction          const   newcurrent  =  current  +  direction ;          // save the updated attribute to the sheet          setAttrs ({              [ `repeating_mastery_ ${ rowid } _mastery` ]:   newcurrent         });     }); });
1611948778
Finderski
Pro
Sheet Author
Compendium Curator
You can simply this further by not even worrying about the rowID...that's assuming the fields being changed are in the same "row" as the action buttons (which this implies).  According to the Sheet Worker documentation (hey, Andreas, I looked referenced it before replying...LOL) Values in repeating sections require a little special handling. If the event that you are inside of is already inside of a repeating section, you can simply request the variable using its name prefaced by the repeating group name and you will receive the value in the same repeating section the event was triggered in. For example, if we have a  repeating_spells section that has both  SpellName ,  SpellLevel , and  SpellDamage , then: So, it could be this: getAttrs ([" repeating_mastery_ mastery" ],  function  ( values ) {          // get the level stat, and coerce into a numerical value.          const   current  = + values . mastery  ||  0 ;          // modify by 1 depending on direction          const   newcurrent  =  current  +  direction ;          // save the updated attribute to the sheet          setAttrs ({              repeating_mastery_ mastery :   newcurrent         });     }); I've probably forgotten something basic, but the getAttrs should be that simple, since you don't need the rowId.
1611949569

Edited 1611954615
GiGs
Pro
Sheet Author
API Scripter
Finderski said: You can simply this further by not even worrying about the rowID...that's assuming the fields being changed are in the same "row" as the action buttons (which this implies).  According to the Sheet Worker documentation (hey, Andreas, I looked referenced it before replying...LOL) I considered suggesting this, but IME it's not necessarily true with click events and action buttons. But I'm very fatigued and not 100% sure, so I played it safe and stuck with the id in place.
1611954599

Edited 1611955756
GiGs
Pro
Sheet Author
API Scripter
I just tested things, and Finderski was correct, you can skip the rowid here. But we both forgot to use the full repeating section attribute name when retrieving the value with this line:         const current = +values.mastery || 0; So here's the corrected sheet worker that handles both buttons (I also corrected my earlier code). Remember this assumes you change the input name from attr_mastery_current to attr_mastery. on ( 'clicked:repeating_mastery:mastery-up clicked:repeating_mastery:mastery-down' ,  function  ( eventInfo ) {      // get last two letters and see if it is 'up', if not it must be a 'down' button.      const   direction  =  eventInfo . triggerName . substr (- 2 ,  2 ) ===  'up'  ?  1  : - 1 ;      getAttrs ([' repeating_mastery_mastery' ],  function  ( values ) {          // get the level stat, and coerce into a numerical value.          const   current  = + values . repeating_mastery_mastery  ||  0 ;          // modify by 1 depending on direction          const   newcurrent  =  current  +  direction ;          // save the updated attribute to the sheet          setAttrs ({              repeating_mastery_mastery:   newcurrent         });     }); });
1611955890
GiGs
Pro
Sheet Author
API Scripter
Also, if you want to make sure value cant go below zero, change this line const   newcurrent  =  current  +  direction ; to const   newcurrent  = Math.max(0, current  +  direction );
WOW! Thanks a lot guys! Thanks for explaining so well, and taking me though it step by step :) It worked perfectly and i understand why it didnt before
1612027669
GiGs
Pro
Sheet Author
API Scripter
I'm glad to hear it :)