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

Fieldset calculate cost question

1597324668
Senjak
Pro
Sheet Author
I've been beating my head against this for a while now, so I'd love any suggestions/pointers/sample code. I've a repeating item that is a dynamic list of skills on a character sheet. Each level of a skill costs the cost of the previous level plus the value of the current level. A table of the cost and value looks sort of like the following: value    cost 1            1 2            3 3            6 4            10 5            15 and so on. I was reading about how to do a sheet worker with stats to calculate the cost of the stat; however, that seemed dependent upon having a fixed number of stats. How do I implement this in a sheet with an unknown and variable number of items? Is it possible to have it automatically update, or would I need to have a 'recalculate' button? Thank you for any guidance you can offer.
1597336607
GiGs
Pro
Sheet Author
API Scripter
You can calculate the cost of each individual skill with this formula: cost = (Lvl^2 + Lvl)/2 How to implement that in your sheet worker depends how the sheet worker and html are written. You can use Aaron's TAS (I'd include a link but it never shows up in the first few google results) or my repeatingSum function to total up all costs in a repeating section. I can help you with the last one. Post your html for the repeating section, and I'll show you how to do it.
1597346418
Senjak
Pro
Sheet Author
I have several scrapped (not working at all) so at this point I'd love to get information about how to do it with your repeatingSum function and then I'll work on the html to go around it. each line will have Skill-name Linked-attribute Skill-level Skill-cost HIDDEN-Skill-cost-multiplier Skill-roll Linked-attribute will be the ability that is associated with the skill -- whole number, drop down, pulls the integer number. Skill-level is the integer number of the skill inputted by the player Skill-cost is the calculated integer value which eventually will be part of the total skill cost for the section. HIDDEN-Skill-cost-multiplier because some skill cost 2x or 4x others. Skills will be grouped into broad sections (General Skills, Magic Skills, Psionic Skills, Martial Arts) with everything in the section having the same multiplier. Thanks for any assistance you can give me! Matt G
1597348200

Edited 1597348869
GiGs
Pro
Sheet Author
API Scripter
Do you have the html for that section? Also very important , what is the name of the repeating section?
1597348852
Senjak
Pro
Sheet Author
Not yet. I'll toss some together today.
1597349009
GiGs
Pro
Sheet Author
API Scripter
In case you missed my edit, I asked for the name of the repeating section. Also, suggestion: when you name those attributes (I need to know their names exactly), use underscores, not dashes, and make them all lower case, like skill_level , skill_cost , etc. It will make all of your sheet workers easier to write.
1597349940
GiGs
Pro
Sheet Author
API Scripter
Okay, some assumptions. I;m assuming your repeating section is called repeating_skills, like <fieldset class="repeating_skills"> I'm also going to assume that your linked attribute has no effect on the skill cost. The attributes that are relevant are skill_level , skill_cost_multiplier , and skill_cost , defined something like <input type="number" name="attr_skill_level" value="0" /> <input type="hidden" name="attr_skill_cost_multiplier" value="1" /> <input type="number" name="attr_skill_cost" value="0" readonly /> I am also going to assume that the total cost of skills goes in an attribute called total_skills_cost , which is somewhere on the sheet outside the repeating section. So the sheet worker to calculate the cost of individual skills is as follows: on(`change:repeating_skills:skill_level change:repeating_skills:skill_cost_multiplier`, () => {     getAttrs(['repeating_skills_skill_level', 'repeating_skills_skill_cost_multiplier'], v => {         const level = parseInt(v['repeating_skills_skill_level']) || 0;         const multiplier = parseInt(v['repeating_skills_skill_cost_multiplier']) || 1;         const cost = (level * level + level) /2 * multiplier;         setAttrs({             repeating_skills_skill_cost: cost         });     }); }); This will calculate the cost of each individual skill, as you enter the level, or change the multiplier. Now to calculate the total for the section, you need to enter the repeatingSum function, unchanged from the website, and then follow it with a sheet worker which uses it to calculate the total. That looks like this: /* ===== PARAMETERS ========== destinations = the name of the attribute that stores the total quantity         can be a single attribute, or an array: ['total_cost', 'total_weight']         If more than one, the matching fields must be in the same order.      section = name of repeating fieldset, without the repeating_     fields = the name of the attribute field to be summed           destination and fields both can be a single attribute: 'weight'           or an array of attributes: ['weight','number','equipped'] */ const repeatingSum = (destinations, section, fields) => {     if (!Array.isArray(destinations)) destinations = [destinations.replace(/\s/g, '').split(',')];     if (!Array.isArray(fields)) fields = [fields.replace(/\s/g, '').split(',')];     getSectionIDs(`repeating_${section}`, idArray => {         const attrArray = idArray.reduce((m, id) => [...m, ...(fields.map(field => `repeating_${section}_${id}_${field}`))], []);         getAttrs([...attrArray], v => {             const getValue = (section, id, field) => v[`repeating_${section}_${id}_${field}`] === 'on' ? 1 : parseFloat(v[`repeating_${section}_${id}_${field}`]) || 0;             const commonMultipliers = (fields.length <= destinations.length) ? [] : fields.splice(destinations.length, fields.length - destinations.length);             const output = {};             destinations.forEach((destination, index) => {                 output[destination] = idArray.reduce((total, id) => total + getValue(section, id, fields[index]) * commonMultipliers.reduce((subtotal, mult) => subtotal * getValue(section, id, mult), 1), 0);             });             setAttrs(output);         });      });  }; on(`change:repeating_skills:skill_cost`, () => {     repeatingSum("total_skills_cost","skills","skills_cost"); }); And there you go. Note if you havent created any sheet workers on your sheet, you need to create a scropt open and closing tag, and put the above code inside it. That looks like this: <script type="text/worker"> // put your sheet workers here. </script>
1597351787
Senjak
Pro
Sheet Author
Thank you very much! I've was organizing my thoughts on the above and put together some code; however, I'm going to have to revisit it and expand it greatly with your example in mind. I'll likely be silent for a while as I go through a code/test/recode/test/play/drink coffee/recode/test cycles. With your example I will build the code up to match the example! <em dances> Matt G <!-- Stats will range from 1 to 20. Skill-name Linked-attribute Skill-level Skill-cost HIDDEN-Skill-cost-multiplier Skill-roll Linked-attribute will be the ability that is associated with the skill -- whole number, drop down, pulls the integer number. Skill-level is the integer number of the skill inputted by the player Skill-cost is the calculated integer value which eventually will be part of the total skill cost for the section. HIDDEN-Skill-cost-multiplier because some skill cost 2x or 4x others.  Skills will be grouped into broad sections (General Skills, Magic Skills, Psionic Skills, Martial Arts) with everything in the section having the same multiplier. -->   <fieldset class="repeating_Skills">     <input type="text" style="width: 300px" name="attr_skills_title" placeholder="Skill Name" />     <select name="attr_dtype" class="input-medium">       <option value="attr_str">Strength</option>       <option value="attr_int">Intelligence</option>       <option value="attr_coo">Coordination</option>       <option value="attr_agi">Agility</option>       <option value="attr_per">Personality</option>       <option value="attr_con">Condition</option>       <option value="attr_spe">Speed</option>       <option value="attr_app">Appearance</option>       <option value="attr_att">Attitude</option>     </select>     <input type="number" value="0" min="0" max="20" name="attr_skill_level"/>     <input type="number" value="1" min="2" max="4" name="attr_skill_multiplier"/>     <input type="text" style="width: 45px" name="attr_skill_cost"/>     <button type='roll' name='skill_roll' value='&{template:default} {{name=@{character_name}}} {{ result=[[2d10 + @{attr_dtype} + @{attr_skill_level} ]] }}'>Skill Roll</button>   </fieldset>
1597352935
GiGs
Pro
Sheet Author
API Scripter
Warning! You must never include upper case letters in a fieldset name. This:   <fieldset class="repeating_Skills"> will cause problems later on, change it to   <fieldset class="repeating_skills"> Also, you probably realise this part, but just in case: Because of this line:     <input type="number" value="1" min="2" max="4" name="attr_skill_multiplier"/> you'll need to change wherever I've used skill_cost_multiplier to skill_multiplier .
1597353035
GiGs
Pro
Sheet Author
API Scripter
One more thing, when referring to attribute names, you never use the attr_ part. That only belongs in the input or select where they are being defined. So your button code should be: <button type='roll' name='skill_roll' value='&{template:default} {{name=@{character_name}}} {{ result=[[2d10 + @{dtype} + @{skill_level} ]] }}'>Skill Roll</button>
1597356846
Andreas J.
Forum Champion
Sheet Author
Translator
GiGs said: Warning! You must never include upper case letters in a fieldset name. I didn't remember this, but looking at the documentation, it says so clear as day: <a href="https://wiki.roll20.net/Building_Character_Sheets#Repeating_Sections" rel="nofollow">https://wiki.roll20.net/Building_Character_Sheets#Repeating_Sections</a> It's serving it's purpose, at least for me. :)