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

Generic Sheet Workers, Mk II

I  once got a huge amount of help from the amazing  Jakob (who also co-wrote the Blades in the Dark sheet, which is fantastic) on crafting sheet workers that apply to several attributes.  And now I am workign on a new sheet that could really use something like that. I would like the sheet worker to look at a select field (named whatever _attribute) to see what is selected, and put whatever is selected into a textbox (named whatever _attribute_name). I can do this with one skill per sheet worker, using the example below.  Thank you to all would read this.     //Attribute Showing on("change:aim_attribute sheet:opened", function() {     getAttrs(['aim_attribute'], function(values) {             if (values.aim_attribute == '@{Intellect}' ) {                 setAttrs({                     'aim_attribute_name': 'Intellect'                 })             }             else if (values.aim_attribute == '@{Cunning}' ) {                 setAttrs({                     'aim_attribute_name': 'Cunning'                 })             }             else if (values.aim_attribute == '@{Resolve}' ) {                 setAttrs({                     'aim_attribute_name': 'Resolve'                 })             }             else if (values.aim_attribute == '@{Might}' ) {                 setAttrs({                     'aim_attribute_name': 'Might'                 })             }             else if (values.aim_attribute == '@{Dexterity}' ) {                 setAttrs({                     'aim_attribute_name': 'Dexterity'                 })             }             else if (values.aim_attribute == '@{Stamina}' ) {                 setAttrs({                     'aim_attribute_name': 'Stamina'                 })             }             else if (values.aim_attribute == '@{Presence}' ) {                 setAttrs({                     'aim_attribute_name': 'Presence'                 })             }             else if (values.aim_attribute == '@{Manipulation}' ) {                 setAttrs({                     'aim_attribute_name': 'Manipulation'                 })             }             else if (values.aim_attribute == '@{Composure}' ) {                 setAttrs({                     'aim_attribute_name': 'Composure'                 })              }             else if (values.aim_attribute == 0 ) {                 setAttrs({                     'aim_attribute_name': 'None'                 })              }         });     });
1527191598
Jakob
Sheet Author
API Scripter
Something like this? const skills = ["aim"]; skills.forEach(skill => {   on(`change:${skill}_attribute sheet:opened`, function() {     getAttrs([`${skill}_attribute`], function(values) {       if (values[`${skill}_attribute`] === "0") {         setAttrs({           [`${skill}_attribute_name`]: "None"         });       }       else {         setAttrs({           [`${skill}_attribute_name`]: values[`${skill}_attribute`].slice(2, -1)         });       }     });   }); }); Explanation: the slice method with arguments (2,-1) just takes all the characters of a string except the first two and the last one, so it turns "@{Cunning}" into "Cunning".
...yes.  Thank you very much.  This makes my day. Ya know, I was hoping to look at how you did it this time, versus how you did it last time.  I might have figured out how to you put this stuff together.  But you went and did something completely different. Thwarted again!
1527192108

Edited 1527192332
Jakob
Sheet Author
API Scripter
Coal Powered Puppet said: ...yes.  Thank you very much.  This makes my day. Ya know, I was hoping to look at how you did it this time, versus how you did it last time.  I might have figured out how to you put this stuff together.  But you went and did something completely different. Thwarted again! I  may have learned a thing or two as well in the meantime ... but go ahead and ask if you have questions. Some things that may seem strange: Template literals (e.g. `change:${skill}_attribute sheet:opened`) . These are essentially just strings, except that you can insert code into them by using ${code}. The above is equivalent to "change:"+skill+"_attribute sheet:opened". Computed keys in an object literal (e.g.  [`${skill}_attribute_name`]:).  This is just a way of letting the key of an object in an object literal depend on a variable instead of being fixed. The above is essentially the same as: const setting = {}; setting[skill + "_attribute_name"] = values[`${skill}_attribute`].slice(2, -1); setAttrs(setting);