Generic Functions

1497659733

Edited 1497659741
Coal Powered Puppet
Pro
Sheet Author
I found a  thread talking about generic functions .  If I can figure this out, it would help me a lot.  Is there anyone who can explain this with colored markers and big poster boards?  I am making a sheet for the Cortex system, and it uses a lot of "steps", starting at d0, d2, d4 and going all the way to d12+d2, d12+d4, etc.  I was hoping to get one set of sheet workers to use when something is increased a few steps at a time. 
1497661642
Thought I'd mention that if you'd prefer to use a macro (benefit is being able to use Roll Queries to modify step number), I've written a few macros for Earthdawn's step system. Assuming it works the same way, I'd just need a picture of the step table to write a macro for it
1497664287
Coal Powered Puppet
Pro
Sheet Author
I actually want to set this up using sheet workers, and do it that way.  Right now, I can make a series of a hundred sheet workers to do what I want, and I am hoping for a across the board fix. Macros...macros have a tendency to drive me (more) bonkers.
1497667669
Brian
Pro
Sheet Author
API Scripter
The problem that the code is trying to solve is having several blocks of code that are similar, and creating a function that will do those repeated actions with the small differences replaced by parameters. Exactly what that entails varies widely by the logic you're trying to achieve.
1497725700
Coal Powered Puppet
Pro
Sheet Author
Renaming things to make the code works is doable.  Right, so this is what I have, and works; I would love to make an easy set of code everything can use, instead of 47 sheet workers with only name changes:      //Step Calc part 1 on("change:Agility_select change:Agility_mod sheet:opened", function() {         getAttrs(["Agility_select", "Agility_mod"], function(values) {             setAttrs({                 Agility_subtotal: +values.Agility_select + +values.Agility_mod             });         });     });     //Step Calc part 2 on("change:Agility_subtotal sheet:opened", function() {     getAttrs(["Agility_subtotal"], function(values) {         if (values.Agility_subtotal == 0) {             setAttrs({                  Agility: 'd0'             })         } else if (values.Agility_subtotal == 1) {             setAttrs({  Agility: 'd2'             })         } else if (values.Agility_subtotal == 2) {             setAttrs({  Agility: 'd4'             })         } else if (values.Agility_subtotal == 3) {             setAttrs({  Agility: 'd6'             })         } else if (values.Agility_subtotal == 4) {             setAttrs({  Agility: 'd8'             })         } else if (values.Agility_subtotal == 5) {             setAttrs({  Agility: 'd10'             })         } else if (values.Agility_subtotal == 6) {             setAttrs({  Agility: 'd12'             })         } else if (values.Agility_subtotal == 7) {             setAttrs({  Agility: 'd12+d2'             })         } else if (values.Agility_subtotal == 8) {             setAttrs({  Agility: 'd12+d4'             })         } else if (values.Agility_subtotal == 9) {             setAttrs({  Agility: 'd12+d6'             })         } else if (values.Agility_subtotal == 10) {             setAttrs({  Agility: 'd12+d8'             })         } else if (values.Agility_subtotal == 11) {             setAttrs({  Agility: 'd12+d10'             })         } else if (values.Agility_subtotal == 12) {             setAttrs({  Agility: 'd12+d12'             })         }     }); });
1497730831

Edited 1497731270
Jakob
Pro
Sheet Author
API Scripter
Like this? (Remark: I think you need to lowercase your attribute names in all events). var stats = ['Agility', 'Wits', 'Strength']; stats.forEach(stat => {   'use strict';   on(`change:${stat.toLowerCase()}_select change:${stat.toLowerCase()}_mod sheet:opened`, () => {     getAttrs([`${stat}_select`, `${stat}_mod`], function(values) {       let setting = {};       setting[`${stat}_subtotal`] = +values[`${stat}_select`] + +values[`${stat}_mod`];       setAttrs(setting);     });   });   on(`change:${stat.toLowerCase()}_subtotal sheet:opened`, function() {     let dieFromSubtotal = {       '0': 'd0',       '1': 'd2',       '2': 'd4',       '3': 'd6',       '4': 'd8',       '5': 'd10',       '6': 'd12',       '7': 'd12+d2',       '8': 'd12+d4',       '9': 'd12+d4',       '10': 'd12+d8',       '11': 'd12+d10',       '12': 'd12+d12'     };     getAttrs([`${stat}_subtotal`], function(values) {       let setting = {};       setting[stat] = dieFromSubtotal[values[`${stat}_subtotal`]]; // Might want to add some defaults here in case something is undefined       setAttrs(setting);     });   }); }); I don't if there's a whole lot to explain, basically, you need to replace every string with Agility in it by the templated version, which also requires you to write the objects a bit differently, because you cannot use a variable name as the key for an object if you write it as a literal (or at least I don't know how you could do it).
1497788107
Coal Powered Puppet
Pro
Sheet Author
I will try it out.  Thank you
1497868239
Coal Powered Puppet
Pro
Sheet Author
This works great.  Thank you very much. Another question: does this work at all with repeating fields?
1497881158
Jakob
Pro
Sheet Author
API Scripter
Coal Powered Puppet said: This works great.  Thank you very much. Another question: does this work at all with repeating fields? Yes, with the right alterations depending on what exactly you want to do :). If you have a bunch of repeating sections, with a bunch of attributes within each section, plus a bunch of non-repeating attributes whose changes you still care about to calculate stuff within the section, you will probably have to loop over several objects to do it.
1497902134
Coal Powered Puppet
Pro
Sheet Author
So far, all I have done is use this code for the attributes and non-repeating skills.  This is amazing , Jakob.  Thank you.