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

[Help][Sheet-workers] Update Modifier

1534385145

Edited 1534385214
Hey guys been working a custom sheet and finally got around to learning how to use JS and sheet-workers, but I am having trouble getting my head it. I made this simple modifier updater, very much like the one used in 5e OGL sheet (exact formula, -10/2) but I don't understand what I am doing wrong. Can you guys shed some light on it for me. <input type='number' class='sheet-strengthScore' name='attr_STR' value='10'> <input type='number' class='sheet-strengthMod'  name='attr_StrMod' value='0'> <script type='text/worker'> on("change:STR", function() {  getAttrs(["STR"], function(v) {      var strength = parseInt(v["STR"]);      var STRmodifier = Math.floor(strength-10) /2; setAttrs({StrMod: STRmodifier});  }); }); </script>
1534388150

Edited 1534388231
GiGs
Pro
Sheet Author
API Scripter
You dont say exactly what is happening. Does it report the wrong number, or just do nothing. The main thing I notice is the on change line. change:stat  statements must always be in lower case, no matter the name of the attribute.  The other thing I always do when you expect a number, is set a default a value using ||0 (which says "if no valid value, set to zero") Also you use v["STR"]. When the source name is a single word or doesnt use invalid letters (like dashes, eg str-mod would be bad), you can use the simpler v.stat syntax. Finally your strmodifier calculation was missing a set of brackets. Which if these is causing your error I don't know. Hopefully one of them fixes it. This would make your script: on(" change:str ", function() {  getAttrs(["STR"], function(v) {     var strength = parseInt( v.STR ) ||0 ;     var STRmodifier = Math.floor ( (strength-10) /2 ) ;     setAttrs({StrMod: STRmodifier});  }); });
1534392243
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
To add on to GG's excellent breakdown, all attribute names are lowercase when working in sheet workers, so v.STR won't return anything. v.str will.
My apologise guy. At this point, both the original code and the updated one with the changes to casing from G G don't do anything. If I change the str there is no update to the StrMod. 
1534393021
GiGs
Pro
Sheet Author
API Scripter
Scott C. said: To add on to GG's excellent breakdown, all attribute names are lowercase when working in sheet workers, so v.STR won't return anything. v.str will. Thanks for the compliment. However, v.STR definitely works, as does  getAttrs(["STR"].  It's only in the change: statements that you need to use lowercase. It may be that sheet workers just dont care about case sensitivity other than in the change line, I keep meaning to test this but never get around it.
1534393341
GiGs
Pro
Sheet Author
API Scripter
Vegemite Dangerous said: My apologise guy. At this point, both the original code and the updated one with the changes to casing from G G don't do anything. If I change the str there is no update to the StrMod.  Do you have more than one  <script type='text/worker'> block? That caused problems in the past. If so, move all of your scripts into just one. Also do you have any other scripts, it could be that one before this one has an error and code has stopped before reaching this one.
1534393559

Edited 1534393971
I am just testing it, so its literally just to small piece of code in the custom character preview. Just opened the game...... It works perfectly. Thanks heaps guys really cleared that up and I should have no problems moving forward.
1534397244
GiGs
Pro
Sheet Author
API Scripter
You're welcome! If you do make any changes to a character sheet's code, you have to fully refresh the campaign page to get the changes to load. Vegemite Dangerous said: I should have no problems moving forward. Famous last words, haha! I still have problems frequently.