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

Sheetworker @{button} syntax

I'm trying to write a single sheetworker to work for a large number of buttons, matching each button to a corresponding attr. const lights = [     'lights5a',     'lights5b' ]; lights.forEach(button => {     on(`clicked:${button}`, () => {     getAttrs([`${button}`], v=> { setAttrs({ [`${button}`]: ((+v.[`${button}`] || 0) + 1) % 2                 });             });         });     }); This is currently set up for two buttons, lights5a and lights5b. There is a corresponding attr for each with the same names. When a button is clicked the worker should set the corresponding attr to 1 if it's currently 0 and 0 if it's currently 1. I've copied work I've done (with help!) on previous sheets but haven't used ${button} in getAttrs or after the colon in setAttrs before, so I suspect the error lies in one of those. Can anyone please advise?
1675289726
GiGs
Pro
Sheet Author
API Scripter
The syntax here is incorrect: + v .[ ` ${ button } ` ] It should be + v [ ` ${ button } ` ] And for easier code, could also be + v [ button ] Personally I'd write it a little differently: const lights = [     'lights5a' ,     'lights5b' ]; lights . forEach ( button => {     on ( `clicked: ${ button } ` , () => {         getAttrs ([ button ], v => {             const toggle = + v [ button ] || 0             setAttrs ({                 [button]: 1 - toggle             });         });     }); }); Though for this to work, you must have an action button named act_[button] and an input named attr_[button] (replace [button] as appropriate).
Great, thanks GiGs.