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

Computed roll value issue

I'm trying to create a rolltemplate that displays the roll (a 1d100), the sum of the individual dice rolled (so if a roll is 23 the sum would be 5) and some other bits and bobs. Now, my issue is one of two things I think: I'm passing the calculated value (the 5 in the example above) incorrectly, or I'm making an error in how I display the value in the rolltemplate. I can't see which, or if it's something else. Please help! :) Rolltemplate: <rolltemplate class="sheet-rolltemplate-simple1d100template">     <div class="sheet-template-container">         <h2>{{name}}</h2>         <div class="sheet-results">             <h3>Roll: {{roll}} </h3> <h3>Skill: {{skill}}</h3> <h3>Damage: {{damage}}</h3> <h3>Dmg: {{computed::damage}}</h3>         </div>     </div> </rolltemplate> Worker: //Roll an attack const std_roll = '1d100cs11cs22cs33cs44cs55cs66cs77cs88cs99'; const my_weapons = { weapon1: '&{template:simple1d100template} {{name=@{character_name} attacks with @{weapon1_name} }} {{roll=[[ '+std_roll+' ]]}} {{skill=@{weapon1_skill_value} }} {{damage=@{weapon1_damage} }} }',  weapon2: '&{template:simple1d100template} {{name=@{character_name} attacks with @{weapon2_name} }} {{roll=[[ '+std_roll+' ]]}} {{skill=@{weapon2_skill_value} }} {{damage=@{weapon2_damage} }} }',  weapon3: '&{template:simple1d100template} {{name=@{character_name} attacks with @{weapon3_name} }} {{roll=[[ '+std_roll+' ]]}} {{skill=@{weapon3_skill_value} }} {{damage=@{weapon3_damage} }} }',  weapon4: '&{template:simple1d100template} {{name=@{character_name} attacks with @{weapon4_name} }} {{roll=[[ '+std_roll+' ]]}} {{skill=@{weapon4_skill_value} }} {{damage=@{weapon4_damage} }} }',  }; const weapons = ['weapon1', 'weapon2', 'weapon3', 'weapon4']; const weapon_changes = weapons.map(weapon => `clicked:${weapon}`).join(' '); on(weapon_changes, eventInfo => { const trigger = eventInfo.triggerName.replace('clicked:',''); const roll_string = my_weapons[trigger]; console.log("Roll string "+roll_string); startRoll(roll_string, roll => { let result = String(roll.results.roll.result); const dmg = calculateDamage(result); console.log("Result "+result + " damage " +dmg); finishRoll( roll.rollId, { damage: dmg, } ); }); }); Calculator function: function calculateDamage(num) { let len = num.length; let sum = 0; switch(len){ case(1): //1-9 sum = parseInt(num); break; case(2): //10-99 let tens = parseInt(num.slice(0,1)); console.log("Tens "+tens); let ones = parseInt(num.slice(-1)); console.log("Ones "+ones); sum = tens+ones; console.log("Sum "+sum); break; case(3): //100 sum = 0; break; default: sum = 0; break; } console.log("Length "+len+" sum "+sum); return sum; }
1723495682

Edited 1723496136
GiGs
Pro
Sheet Author
API Scripter
What specifically is not working correctly. What happens that should not be happening? Unless I'm missing something (very possible), this looks like a simple rollbutton. If I understand it correctly, you can eliminate the entire sheet worker and function section, and have a roll button. The HTML would be something like this: <input type="text" name="attr_weapon1_name" value="example"> <input type="number" name="attr_weapon1_skill_value" value="0"> <input type="text" name="attr_weapon1_damage" value="2d10"> <button type="roll" name="roll_whatever" value=" &{template:simple1d100template} {{name=@{character_name} attacks with @{weapon1_name} }} {{roll=[[ 1d100cs11cs22cs33cs44cs55cs66cs77cs88cs99 ]]}} {{skill=@{weapon1_skill_value} }} {{damage=@{weapon1_damage} }} }" Then get rid of all the code in your initial post except for the roll template. What do you want to do that requires a custom roll parsing solution?
I want to:  1. Roll 1d100 and compare it to a skill value (under or equal is success) 2. Get the sum of the two dice rolled (so if the 1d100 results in 78 the sum is 15) 3. Add that to a damage value of the thing rolled (a regular attribute of the weapon) 4. Calculate floor(result/10) for any successful roll 5. Crit success on equals (22, 33 etc) if also a success 6. Crit fail on equals if also a fail.
Oh, and what doesn't happen that should happen is that the value  {{computed::damage}} is not printed.
1723500184
GiGs
Pro
Sheet Author
API Scripter
So you want to use the d100 roll in multiple ways? as a d100 roll, to compare against a skill value, and a 1/1th total is calculated (what does that represent?) As a 2d10 roll, where the two dice are added together, and a modifier is added. Custom Roll Parsing does look needed now. You have several console.log statements in there. Do they always show the correct value? And if not, what does it show instead?
1723500618

Edited 1723500677
GiGs
Pro
Sheet Author
API Scripter
aha, I think I see the issue (one isue anyway). You can only save a computed value to a roll, which means an inline roll: a number in brackets like this [[ number ]] So this in your roll examples {{damage=@{weapon4_damage} }} would need to be {{damage=[[@{weapon4_damage}]] }} If @{weapon4_damage} is not something that can be expressed as a number, you'll need to write this a different way. Also, each of your weapon strings seem to end with a superfluous } bracket. Should this weapon1: '&{template:simple1d100template} {{name=@{character_name} attacks with @{weapon1_name} }} {{roll=[[ '+std_roll+' ]]}} {{skill=@{weapon1_skill_value} }} {{damage=@{weapon1_damage} }} }', be weapon1: '&{template:simple1d100template} {{name=@{character_name} attacks with @{weapon1_name} }} {{roll=[[ '+std_roll+' ]]}} {{skill=@{weapon1_skill_value} }} {{damage=@{weapon1_damage} }}', ??
{{damage=[[@{weapon4_damage}]] }} Allright! Very nice, that works. Thank you! And yes, a bracket too many, although it appears it didnt throw a hissy fit about it. Many thanks!
1723501048

Edited 1723501059
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Yep Gigs has the solution, but I'd like to add that you can massively streamline your  calculateDamage function: const calculateDmg = (roll) => { return `${roll}`.split('').reduce((m,n) => (+m) + (+n)); }
const calculateDmg = (roll) => { return `${roll}`.split('').reduce((m,n) => (+m) + (+n)); } Lol!!! Indeed. Thanks!
(what does that represent?) The Quality of the roll... I.e. it's a roll under but as high as possible system.
1723504388
GiGs
Pro
Sheet Author
API Scripter
Scott C. said: Yep Gigs has the solution, but I'd like to add that you can massively streamline your  calculateDamage function: const calculateDmg = (roll) => { return `${roll}`.split('').reduce((m,n) => (+m) + (+n)); } I had a similar idea, looking at the earlier roll string: const std_roll = '1d100cs11cs22cs33cs44cs55cs66cs77cs88cs99'; const my_weapons = { weapon1: '&{template:simple1d100template} {{name=@{character_name} attacks with @{weapon1_name} }} {{roll=[[ '+std_roll+' ]]}} {{skill=@{weapon1_skill_value} }} {{damage=@{weapon1_damage} }} }',  weapon2: '&{template:simple1d100template} {{name=@{character_name} attacks with @{weapon2_name} }} {{roll=[[ '+std_roll+' ]]}} {{skill=@{weapon2_skill_value} }} {{damage=@{weapon2_damage} }} }',  weapon3: '&{template:simple1d100template} {{name=@{character_name} attacks with @{weapon3_name} }} {{roll=[[ '+std_roll+' ]]}} {{skill=@{weapon3_skill_value} }} {{damage=@{weapon3_damage} }} }',  weapon4: '&{template:simple1d100template} {{name=@{character_name} attacks with @{weapon4_name} }} {{roll=[[ '+std_roll+' ]]}} {{skill=@{weapon4_skill_value} }} {{damage=@{weapon4_damage} }} }',  }; Since the trigger is always weapon1, weapon2, ect, this could be const my_weapons = weapon => `&{template:simple1d100template} {{name=@{character_name} attacks with @{ ${ weapon } _name} }} {{roll=[[  1d100cs11cs22cs33cs44cs55cs66cs77cs88cs99 ]]}} {{skill=@{ ${ weapon } _skill_value} }} {{damage=@{ ${ weapon } _damage} }}`; then you'd call it with const roll_string = my_weapons (trigger);
Excellent idea! Thank you!