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 .
×
May your rolls be merry + bright! 🎄
Create a free account

Sheet Worker Script "Table map"

I was trying to do a fairly straightforward "table map" conversion to map one set of values to another rather than trying to write a messy math function to do it. I've gotten as far as this: <script type="text/worker">     on("change:fate_1", function() {         getAttrs(["fate_1"], function(v) {             switch (v.fate_1) {                 case 0:                     setAttrs({"fate_1_level":2});                     break;                 case 10:                     setAttrs({"fate_1_level":3});                     break;                 case 30:                     setAttrs({"fate_1_level":4});                     break;                 case 70:                     setAttrs({"fate_1_level":5});                     break;             };         });     }); </script> But it ALWAYS returns 5;  Originally I had a "default" clause at the end to return 2 as a "default" and under those circumstances, it ALWAYS returned 2.  It seems like somehow it is always returning the last value in the list. I've tried a lot of different tests, and I don't really understand where I'm going wrong.  Can anyone assist?
1458587491
The Aaron
Pro
API Scripter
try: switch( parseInt(v.fate_1,10) ) {
Jackpot, thanks.   I guess I was trying to case a String to an Integer. Oops.
This seems like a good way to implement the data lookup issue I was discussing in a different thread.  Thanks for the idea!
Now I'm working to try to generalize this - I have fate_1 through fate_8, and I figured, "Hey, I've got eventInfo.sourceAtttribute, that can tell me which one changed, and I can only update that one." except I can't figure out how to retrieve the value for it.  Is there a way to get the value of the eventInfo.sourceAttribute attribute? I feel like this stuff is not documented as clearly as I would like.
1458602612
The Aaron
Pro
API Scripter
I believe this would do it: getAttrs([eventInfo.sourceAttribute],function(values){ console.log(values[eventInfo.sourceAttribute]); }); Note that variables and properties are case sensitive so be sure it is actually sourceAttribute and not sourceattribute. 
Okay! This seems to mostly be working: <script type="text/worker">     on("change:fate_1 change:fate_2 change:fate_3 change:fate_4 change:fate_5 change:fate_6 change:fate_7 change:fate_8", function(eventInfo) {         getAttrs([eventInfo.sourceAttribute],function(values){             var sourceFate = eventInfo.sourceAttribute;             var sourceFateValue = values[eventInfo.sourceAttribute];             var sourceFateLevel = sourceFate.concat("_level");             switch( parseInt(sourceFateValue,10) ) {                 case 0:                     setAttrs({[sourceFateLevel]:2});                     break;                 case 10:                     setAttrs({[sourceFateLevel]:3});                     break;                 case 30:                     setAttrs({[sourceFateLevel]:4});                     break;                 case 70:                     setAttrs({[sourceFateLevel]:5});                     break;             };         });     }); </script> It seems to behave a LITTLE bit weirdly if the attributes it is changing don't exist yet. Is there a check I could do for that?
1458624818
The Aaron
Pro
API Scripter
Not really. You could try getting it before setting it maybe?  What do you mean by "weirdly"?
Yeah, defining them ahead of time is probably the way to go, I'll add them as hidden attributes in the html, I guess. As for "weirdly" I mean that they don't seem to update promptly.  If I have a fate_1_level attribute already existent, and change the value of fate_1, the fate_1_level updates basically immediately, whereas if I don't have, say, fate_8_level already present, and update the value of fate_8, the changes seem to take like, minutes to occur. That or something else I am doing causes them to happen.
1458660978
The Aaron
Pro
API Scripter
Ah.  That is weird.
I figured it out;  It's probably a minor "bug". Basically, the value is updated correctly and immediately, but the new attribute doesn't appear in the "Attributes & Abilities" tab of the character sheet until you close the sheet and open it again.
1458672759
The Aaron
Pro
API Scripter
Ah, that makes sense.  The view is generated when the sheet is opened, so it's not suprising it doesn't get adjusted.  Might be a nice enhancment though.