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

Consolidating sheet workers

1558730070
Kraynic
Pro
Sheet Author
I am pretty ignorant of scripts in general.  Everything that follows has come from direct suggestions from the forums, or copying something I have seen from the wiki or forum and tinkering until it works.  <script type="text/worker"> on("change:ps sheet:opened", function () { getAttrs(['ps'], function (values) { const ps = parseInt(values['ps'])||0; const modifier = ps*20; setAttrs({ lift: modifier }); }); }); on("change:ps sheet:opened", function () { getAttrs(['ps'], function (values) { const ps = parseInt(values['ps'])||0; const modifier = ps*10; setAttrs({ carry: modifier }); }); }); on("change:pe sheet:opened", function () { getAttrs(['pe'], function (values) { const pe = parseInt(values['pe'])||0; const modifier = pe; setAttrs({ carry_max: modifier }); }); }); on("change:pe sheet:opened", function () { getAttrs(['pe'], function (values) { const pe = parseInt(values['pe'])||0; const modifier = pe; setAttrs({ run_max: modifier }); }); }); on("change:spd sheet:opened", function () { getAttrs(['spd'], function (values) { const spd = parseInt(values['spd'])||0; const modifier = spd*15; setAttrs({ run_melee: modifier }); }); }); on("change:spd sheet:opened", function () { getAttrs(['spd'], function (values) { const spd = parseInt(values['spd'])||0; const modifier = Math.round(spd*.68); setAttrs({ run_mph: modifier }); }); }); on("change:spd change:hth_numberattacks sheet:opened", function () { getAttrs(['spd','hth_numberattacks'], function (values) { const spd = parseInt(values['spd'])||0; const hth_numberattacks = parseInt(values['hth_numberattacks'])||0; const modifier = Math.round((spd/hth_numberattacks)*15); setAttrs({ run_attack: modifier }); }); }); function maMod(score) { let bonus = 0; if (score >= 30) bonus = 97; else if (score >= 29) bonus = 96; else if (score >= 28) bonus = 94; else if (score >= 27) bonus = 92; else if (score >= 26) bonus = 88; else if (score >= 25) bonus = 84; else if (score >= 16) bonus = Math.floor(score-8)*5; return bonus; }; on("change:ma sheet:opened", function () { getAttrs(['ma'], function (values) { let ma = parseInt(values['ma'])||0; let invoke_trust = maMod(ma); setAttrs({ invoke_trust: invoke_trust }); }); }); function psMod(score) { let bonus = 0; if (score >= 16) bonus = Math.round(score-15)*1; return bonus; }; on("change:ps sheet:opened", function () { getAttrs(['ps'], function (values) { let ps = parseInt(values['ps'])||0; let ps_bonus = psMod(ps); setAttrs({ ps_bonus: ps_bonus }); }); }); function ppMod(score) { let bonus = 0; if (score >= 16) bonus = Math.round((score-15)*.5); return bonus; }; on("change:pp sheet:opened", function () { getAttrs(['pp'], function (values) { let pp = parseInt(values['pp'])||0; let pp_bonus = ppMod(pp); setAttrs({ pp_bonus: pp_bonus }); }); }); function pbMod(score) { let bonus = 0; if (score >= 30) bonus = 92; else if (score >= 29) bonus = 90; else if (score >= 28) bonus = 87; else if (score >= 27) bonus = 84; else if (score >= 16) bonus = Math.floor(score-10)*5; return bonus; }; on("change:pb sheet:opened", function () { getAttrs(['pb'], function (values) { let pb = parseInt(values['pb'])||0; let charm_impress = pbMod(pb); setAttrs({ charm_impress: charm_impress }); }); }); </script> This all works, but when I read suggestions in the wiki that the sheet workers should only acquire values from the sheet once and all calculations should work from that one pull, I get thinking this probably isn't the best setup.  Anyone have some suggestions for making this more streamlined?  Or should I just leave it as it is since it works?
1558736538
GiGs
Pro
Sheet Author
API Scripter
You're right that the setup you have isnt the most efficient. There are no serious problems here, but you could streamline them a bit. The first and easiest thing you can improve is to combine different setAttrs functions into one. Your first two workers for example: on("change:ps sheet:opened", function () { getAttrs(['ps'], function (values) { const ps = parseInt(values['ps'])||0; const modifier = ps*20; setAttrs({ lift: modifier }); }); }); on("change:ps sheet:opened", function () { getAttrs(['ps'], function (values) { const ps = parseInt(values['ps'])||0; const modifier = ps*10; setAttrs({ carry: modifier }); }); }); Here the change and getAttrs lines are identical, so you should combine these functions into one. on("change:ps sheet:opened", function () { getAttrs(['ps'], function (values) { const ps = parseInt(values['ps'])||0; const psx20 = ps*20; const psx10 = ps*10; setAttrs({ lift: psx20, carry, psx10 }); }); }); The sheet workers that use function calls are a bit weird to me. Functions are great when you have code that is being repeated in different places in your code. You can set up a function for code that is only used once, if you think it might be needed to be duplicated later, or you want to make your code more readable and understandable. But for sheet workers, it doesnt really make sense. Each of your functions is unique, and short, so it may just as well stay be in the on(change) code. This makes your code more readable. So this function pbMod(score) { let bonus = 0; if (score >= 30) bonus = 92; else if (score >= 29) bonus = 90; else if (score >= 28) bonus = 87; else if (score >= 27) bonus = 84; else if (score >= 16) bonus = Math.floor(score-10)*5; return bonus; }; on("change:pb sheet:opened", function () { getAttrs(['pb'], function (values) { let pb = parseInt(values['pb'])||0; let charm_impress = pbMod(pb); setAttrs({ charm_impress: charm_impress }); }); }); Could be written as on("change:pb sheet:opened", function () { getAttrs(['pb'], function (values) { let pb = parseInt(values['pb']) || 0; let bonus = 0; if (score >= 30) bonus = 92; else if (score >= 29) bonus = 90; else if (score >= 28) bonus = 87; else if (score >= 27) bonus = 84; else if (score >= 16) bonus = Math.floor(score - 10) * 5; setAttrs({ charm_impress: bonus }); }); }); You could also improve it further by examinining your if statement, and seeing if there any patterns being used. I notice 27/28/29 increases by 3 each step, so you could change that to if (score >= 30) bonus = 92; else if (score >= 27) bonus = (score *3) +3; else if (score >= 16) bonus = Math.floor(score - 10) * 5; I notice you have another function below for ps . You can move that code into the ps worker I put at the top here. In the end you'll have a lot fewer sheet workers, and more coherent code. In short: combine all sheet workers you can into the same function, and break out code into separate functions only when it makes your code shorter.  Regarding spd : you have several spd workers, they should all be combined into one. But the one that uses spd and hth_numberofattacks should remain its own separate thing. 
1558752077

Edited 1558753033
Kraynic
Pro
Sheet Author
Thanks.  I will tinker with that this weekend and see if I mess anything up so badly I need to bring the remains back here! Edit:  As far as things looking weird, that is just due to my ignorance.  I found something that looked like it might do what I needed.  If something worked, I would leave it alone and find something (or copy/paste) for the next function so I wouldn't mess up the one that was already working. 
1558758419
Kraynic
Pro
Sheet Author
Well, ended up tinkering with this tonight when I should be in bed.  I have the first ps pair combined as you showed, and used that example to combine pe and 2 of the spd calculations.  I'm not sure how to go about doing the same with the last ps calculation though: function psMod(score) { let bonus = 0; if (score >= 16) bonus = Math.round(score-15)*1; return bonus; }; on("change:ps sheet:opened", function () { getAttrs(['ps'], function (values) { let ps = parseInt(values['ps'])||0; let ps_bonus = psMod(ps); setAttrs({ ps_bonus: ps_bonus }); }); }); Is it possible to integrate this with your first example in such a way that it won't give negative numbers when the ps attribute drops below 16? I assumed I needed all this so that it would only do something if the attribute was above a certain number.
1558759962
GiGs
Pro
Sheet Author
API Scripter
There was  mistake in my earlier code. This setAttrs({ lift: psx20, carry, psx10 }); should have be setAttrs({ lift: psx20, carry: psx10 }); Yes, you can include that and it's easier than you think. on("change:ps sheet:opened", function () { getAttrs(['ps'], function (values) { const ps = parseInt(values['ps'])||0; const psx20 = ps*20; const psx10 = ps*10; let ps_bonus = 0; if(ps > 15) ps_bonus = Math.round(ps -15); setAttrs({ lift: psx20, carry: psx10, ps_bonus: ps_bonus }); }); }); Notice it's the same thing as you're doing in the function, just variable names have changed.
1558802402
Kraynic
Pro
Sheet Author
Ah, ok.  And I did spot the typo last night, so the only thing left (I think) was that last ps calculation.