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 .
×

Custom Character Sheet - Complicated calculation with scriptworker button

I'm working on a custom character sheet and I want to streamline some of the button pressing for attacks. Right now I have one button for determining a hit, and then another button to calculate damage, but I want to combine those. I also have a bunch of pop-ups that ask for information you can already find on the character sheet. I most of the work done, by brute force, I don't really know javascript. But basically I just want to pass a bunch of values to the startRoll function so I can do a calculation and spit out an answer. <rolltemplate class="sheet-rolltemplate-atkhit"> <div class="sheet-template-container"> <div class="sheet-template-header">{{name}}</div> <div class="sheet-template-row">Target: {{target}}</div> <div class="sheet-template-row">{{keys}}</div> <div class="sheet-template-row">Range: {{range}}</div> <div class="sheet-template-row">Accuracy: {{accr}}</div> <div class="sheet-template-row">Crit: {{critr}}</div> <div class="sheet-template-row">{{pwr}}{{atype}}{{type1}}{{type2}}{{mod}}{{cat}}{{atk}}{{satk}}{{def}}{{sdef}}</div> <div class="sheet-template-row">Damage: [[{{computed::dmg}}]]</div> <div class="sheet-template-row">Effect: {{effect}}</div> </div> </rolltemplate> on('clicked:atk1test', (info) => { startRoll("&{template:atkhit} {{name=@{atk1_name}}} {{target=@{target|character_name}}} {{keys=@{atk1_type} @{atk1_cat} @{atk1_con}}} {{range=@{atk1_range}}} {{accr=[[1d100cs<[[@{Atk1_Acc}+@{Acc}*10-@{target|Evas}*10]]]]}} {{critr=[[1d100cs<[[@{Crit}]]]]}} {{pwr=[[@{atk1_pwr_tot}]]}} {{atype=@{atk1_type}}} {{type1=@{target|type1}}} {{type2=@{target|type2}}} {{mod=[[?{Modifiers|1}]]}} {{cat=@{atk1_cat}}} {{atk=[[@{atk}]]}} {{satk=[[@{satk}]]}} {{def=[[@{target|def}]]}} {{sdef=[[@{target|sdef}]]}} {{effect=[[@{atk1_effect}]]}}", (results) => { const pwr = results.results.pwr.result const atype = results.results.atype.result; const type1 = results.results.type1.result; const type2 = results.results.type2.result; const mod = results.results.mod.result; const cat = results.results.cat.result; const atk = results.results.atk.result; const satk = results.results.satk.result; const def = results.results.def.result; const sdef = results.results.sdef.result; const efftable = { none: {normal: 1, fire: 1, water: 1, electric: 1, grass: 1, ice: 1, fighting: 1, poison: 1, ground: 1, flying: 1, psychic: 1, bug: 1, rock: 1, ghost: 1, dragon: 1, dark: 1, steel: 1, fairy: 1}, normal: {normal: 1, fire: 1, water: 1, electric: 1, grass: 1, ice: 1, fighting: 1, poison: 1, ground: 1, flying: 1, psychic: 1, bug: 1, rock: 0.5, ghost: 0, dragon: 1, dark: 1, steel: 0.5, fairy: 1}, fire: {normal: 1, fire: 0.5, water: 0.5, electric: 1, grass: 2, ice: 2, fighting: 1, poison: 1, ground: 1, flying: 1, psychic: 1, bug: 2, rock: 0.5, ghost: 1, dragon: 0.5, dark: 1, steel: 2, fairy: 1}, water: {normal: 1, fire: 2, water: 0.5, electric: 1, grass: 0.5, ice: 1, fighting: 1, poison: 1, ground: 2, flying: 1, psychic: 1, bug: 1, rock: 2, ghost: 1, dragon: 0.5, dark: 1, steel: 1, fairy: 1}, electric: {normal: 1, fire: 1, water: 2, electric: 0.5, grass: 0.5, ice: 1, fighting: 1, poison: 1, ground: 0, flying: 2, psychic: 1, bug: 1, rock: 1, ghost: 1, dragon: 0.5, dark: 1, steel: 1, fairy: 1}, grass: {normal: 1, fire: 0.5, water: 2, electric: 1, grass: 0.5, ice: 1, fighting: 1, poison: 0.5, ground: 2, flying: 0.5, psychic: 1, bug: 0.5, rock: 2, ghost: 1, dragon: 0.5, dark: 1, steel: 0.5, fairy: 1}, ice: {normal: 1, fire: 0.5, water: 0.5, electric: 1, grass: 2, ice: 0.5, fighting: 1, poison: 1, ground: 2, flying: 2, psychic: 1, bug: 1, rock: 1, ghost: 1, dragon: 2, dark: 1, steel: 0.5, fairy: 1}, fighting: {normal: 2, fire: 1, water: 1, electric: 1, grass: 1, ice: 2, fighting: 1, poison: 0.5, ground: 1, flying: 0.5, psychic: 0.5, bug: 0.5, rock: 2, ghost: 0, dragon: 1, dark: 2, steel: 2, fairy: 0.5}, poison: {normal: 1, fire: 1, water: 1, electric: 1, grass: 2, ice: 1, fighting: 1, poison: 0.5, ground: 0.5, flying: 1, psychic: 1, bug: 1, rock: 0.5, ghost: 0.5, dragon: 1, dark: 1, steel: 0, fairy: 0.5}, ground: {normal: 1, fire: 2, water: 1, electric: 2, grass: 0.5, ice: 1, fighting: 1, poison: 2, ground: 1, flying: 0, psychic: 1, bug: 0.5, rock: 2, ghost: 1, dragon: 1, dark: 1, steel: 2, fairy: 1}, flying: {normal: 1, fire: 1, water: 1, electric: 0.5, grass: 2, ice: 1, fighting: 2, poison: 1, ground: 1, flying: 1, psychic: 1, bug: 2, rock: 0.5, ghost: 1, dragon: 1, dark: 1, steel: 1, fairy: 1}, psychic: {normal: 1, fire: 1, water: 1, electric: 1, grass: 1, ice: 1, fighting: 1, poison: 1, ground: 1, flying: 1, psychic: 1, bug: 1, rock: 1, ghost: 1, dragon: 1, dark: 1, steel: 0.5, fairy: 1}, bug: {normal: 1, fire: 0.5, water: 1, electric: 1, grass: 2, ice: 1, fighting: 0.5, poison: 0.5, ground: 1, flying: 0.5, psychic: 2, bug: 1, rock: 1, ghost: 0.5, dragon: 1, dark: 2, steel: 0.5, fairy: 0.5}, rock: {normal: 1, fire: 2, water: 1, electric: 1, grass: 1, ice: 2, fighting: 0.5, poison: 1, ground: 0.5, flying: 2, psychic: 1, bug: 1, rock: 1, ghost: 1, dragon: 1, dark: 1, steel: 0.5, fairy: 1}, ghost: {normal: 0, fire: 1, water: 1, electric: 1, grass: 1, ice: 1, fighting: 1, poison: 1, ground: 1, flying: 1, psychic: 2, bug: 1, rock: 1, ghost: 2, dragon: 1, dark: 0.5, steel: 1, fairy: 1}, dragon: {normal: 1, fire: 1, water: 1, electric: 1, grass: 1, ice: 1, fighting: 1, poison: 1, ground: 1, flying: 1, psychic: 1, bug: 1, rock: 1, ghost: 1, dragon: 2, dark: 1, steel: 0.5, fairy: 0}, dark: {normal: 1, fire: 1, water: 1, electric: 1, grass: 1, ice: 1, fighting: 0.5, poison: 1, ground: 1, flying: 1, psychic: 2, bug: 1, rock: 1, ghost: 2, dragon: 1, dark: 0.5, steel: 1, fairy: 0.5}, steel: {normal: 1, fire: 0.5, water: 0.5, electric: 0.5, grass: 1, ice: 2, fighting: 1, poison: 1, ground: 1, flying: 1, psychic: 1, bug: 1, rock: 2, ghost: 1, dragon: 1, dark: 1, steel: 0.5, fairy: 2}, fairy: {normal: 1, fire: 0.5, water: 1, electric: 1, grass: 1, ice: 1, fighting: 2, poison: 0.5, ground: 1, flying: 1, psychic: 1, bug: 1, rock: 1, ghost: 1, dragon: 2, dark: 2, steel: 0.5, fairy: 1} }; const eff = efftable[atype[type1]] * efftable[atype[type2]]; console.log(eff); if (cat = "Physical") { const bonus = atk-def; } else { const bonus = satk-sdef; } console.log(bonus); const final = pwr * eff * mod + bonus; console.log(final); const computed = Math.round(final); finishRoll( results.rollId, { dmg: computed, } ); }); }); The rolltemplate works fine up until the Damage line, and that's where all the startRolling code comes in. Some more details pwr is the base power of the attack atype is the type of the attack, type1 and type2 are the types of the target, this is for Pokemon, so these 3 are used to determine the attacks effectiveness (4x, 2x, 1x, 0.5x, 0.25x, 0x) efftable is supposed to be a look up table for effectiveness mod is any other temporary modifiers for the attack cat determines if you use atk vs def or satk vs sdef
1647989219
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
We're going to need more details to really critique this, but I do see some general issues. The general issues are: const eff = efftable[atype[type1]] * efftable[atype[type2]]; This lookup is not going to work the way you want it to. The way this is currently setup, you are trying to grab a property of  atype  based on  type2 , and then using that to look up a value on your efftable. Based on what you've described, I think you are trying to look up the  atype  in  efftable , and then look up your various types in the resulting object (e.g. if  atype=fire ,  type1=normal , and  type2=water ; you'd want  eff=1*0.5 ). The proper JS syntax to do this is this (but see the next issue): const eff = efftable[atype][type1] * efftable[atype][type2]; The startRoll function can only return values for fields that contain an inline roll (and will only return the result of the first inline roll in that field). However, your template has atype , type1 , and type2  as just straight attribute calls. Unless these attributes are themselves inline rolls, this won't work. You could wrap these in an inline roll for extracting a non numerical value from the roll as seen in Oosh's Adventure's with startRoll thread  discussing his getQuery function. However, as these values are present in the sheet itself, I'd recommend just getting the values using a getAttrs that would be wrapped around the startRoll If that's not what you're attempting to do with this code, post some more details about your goal. Hope that helps, Scott
That seems to cover what I want; grab a bunch of attrs from the selected and target sheet and do a calculation so I can make a roll. More specifically, I want to be able to calculate the number of dice to roll. One problem I see is being able to get information from a target without using the startRoll function, because I would need to do a startRoll to select a target, then do a getAttrs to grab all the attrs, then another startRoll to do the calculations? Unless I can just use the getAttrs to just return a value instead of setAttrs. Here is an example: Slowpoke uses Water Gun on an Onix. Water Gun rolls [5 (base power) x 4 (effectiveness) x 2 (Crit) x 1 (no modifier, 1 by default)]dice + slowpoke's satk attr - onix's sdef attr