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 on change best practices?

1566409512
Chad
Sheet Author
So, My sheet isn't big enough that I think this really matters, but I like to be efficient as a matter of practice. My question is, what is most efficient in the worker scripts, or does it really matter: Background: I have a number of skills. Each time one is changed, I change two other attributes (one is hidden, the other is shown in a span) Option 1: Have one big "on change" listener listing all of the skills and calling a single function which will then go through and calculate all the hidden fields for all of the skills. Option 2: Having an "on change" listener for each individual skill which will then call a function to only changes that skill. My assumption is that option 2 is best, unless adding multiple "on change" functions drastically reduces sheet efficiency simply because there are too many listeners? Thoughts?
1566413368
GiGs
Pro
Sheet Author
API Scripter
You're right that option 2 is best. Having a lot of listeners isnt a problem, having one listener that has to read all  the attributes (via getAttrs) that might change and run through the function, every time any one of them changes, is definitely a lot less efficient. If you are going to be creating a lot of sheet workers that basically do the exact same thing, just with different attribute names, there are several shortcuts. This is my current favourite:  A Sheet Worker that generates Sheet Workers .
Hello Chad, I have moved your post to the Character Sheets & Compendium forum. Good Luck!
1566417590
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
One thing to keep in mind is that you want to avoid getAttrs -> setAttrs -> getAttrs cascades. These are all asynchronous functions, and so if you rely on the event generation from a sheetworker set attribute to trigger further calculations, that will be extremely laggy. In cases like that, it's much better to simply get all the attributes you might possibly need, calculate all of your stuff at once, and then do a single setAttrs.