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

Problems with script and sheet worker 2

I am trying to create a script for when the player selects the name of the weapon from the list, the other fields are automatically filled, but something is not working, could someone help? <!--Weapon table start--> <label class="sheet-weapongrouptable">Tabela de ataques</label> <div class="sheet-tablebody"> <div> <div class="sheet-weapontablerow1 weapontablefirstline"><label class="sheet-selectedweapons sheet-weaponname">Nome</br></label></div> <div class="sheet-weapontablerow2 weapontablefirstline"><label class="sheet-selectedweapons sheet-weapontype">Proficiência</label></div> <div class="sheet-weapontablerow3 weapontablefirstline"><label class="sheet-selectedweapons sheet-weaponnivel">Nível</label></div> <div class="sheet-weapontablerow4 weapontablefirstline"><label class="sheet-selectedweapons sheet-weapondamage">Dano</label></div> <div class="sheet-weapontablerow5 weapontablefirstline"><label class="sheet-selectedweapons sheet-weaponrange">Alcance padrão</label></div> <div class="sheet-weapontablerow6 weapontablefirstline"><label class="sheet-selectedweapons sheet-weaponrangemax">Alcance máx.</label></div> <div class="sheet-weapontablerow7 weapontablefirstline"><label class="sheet-selectedweapons sheet-weaponcalibre">Calibre</label></div> <div class="sheet-weapontablerow8 weapontablefirstline"><label class="sheet-selectedweapons sheet-weaponrdf">RDF</label></div> <div class="sheet-weapontablerow9 weapontablefirstline"><label class="sheet-selectedweapons sheet-weaponammo">Pente</label></div> <div class="sheet-weapontablerow10 weapontablefirstline"><label class="sheet-selectedweapons sheet-weaponweight">Peso</label></div> <div class="sheet-weapontablerow11 weapontablefirstline"><label class="sheet-selectedweapons sheet-weaponstrenght1hand">Força mín(1mão)</label></div> <div class="sheet-weapontablerow11 weapontablefirstline"><label class="sheet-selectedweapons sheet-weaponstrenght2hand">Força mín(2mãos)</label></div> </div> <fieldset class="repeating_weaponselection"> <select name="attr_weaponname"> <option class="sheet-weapongrouplevel" value="0" selected>N/A</option> <option class="sheet-weapongrouplevel" value="1" selected>Tantalum</option> </select> <input type="number" class="sheet-weapontablerow2 sheet-inputweaponproperty" name="attr_weaponprof" value="0"> <input type="number" class="sheet-weapontablerow3 sheet-inputweaponproperty" name="attr_weaponnivel" value="0"> <input type="number" class="sheet-weapontablerow4 sheet-inputweaponproperty" name="attr_weapondamage" value="0"> <input type="number" class="sheet-weapontablerow5 sheet-inputweaponproperty" name="attr_weaponrange" value="0"> <input type="number" class="sheet-weapontablerow6 sheet-inputweaponproperty" name="attr_weaponrangemax" value="0"> <input type="number" class="sheet-weapontablerow7 sheet-inputweaponproperty" name="attr_weaponcalibre" value="0"> <input type="number" class="sheet-weapontablerow8 sheet-inputweaponproperty" name="attr_weaponrdf" value="0"> <input type="number" class="sheet-weapontablerow9 sheet-inputweaponproperty" name="attr_weaponammo" value="0"> <input type="number" class="sheet-weapontablerow9 sheet-inputweaponproperty" name="attr_weaponweight" value="0"> <input type="number" class="sheet-weapontablerow11 sheet-inputweaponproperty" name="attr_weaponstrength1hand" value="@{weaponweight}/0.2" disabled="true"> <input type="number" class="sheet-weapontablerow12 sheet-inputweaponproperty" name="attr_weaponstrength2hand" value="@{weaponweight}/0.7" disabled="true"> <button type="roll" class="sheet-weapontablerow11" value="&{template:tagmar_rolagem_completa} {{title_battle=@{weaponname}}} {{charname=@{nomepersonagem}}} {{subtags=@{racapersonagem} | @{profissaopersonagem}}} {{subinformacao=@{weapondamagetype}}} {{dano1=[[@{weapon25d}+@{weapondamagebonus}]]}} {{dano2=[[@{weapon50d}+@{weapondamagebonus}]]}} {{dano3=[[@{weapon75d}+@{weapondamagebonus}]]}} {{dano4=[[@{weapon100d}+@{weapondamagebonus}]]}} {{dano5=[[@{weapon125d}+@{weapondamagebonus}]]}} {{dano6=[[@{weapon150d}+@{weapondamagebonus}]]}} {{dano7=[[@{weapon175d}+@{weapondamagebonus}]]}} {{dano8=[[@{weapon200d}+@{weapondamagebonus}]]}} {{dano9=[[@{weapon225d}+@{weapondamagebonus}]]}} {{dano10=[[@{weapon250d}+@{weapondamagebonus}]]}} {{dano11=[[@{weapon275d}+@{weapondamagebonus}]]}} {{dano12=[[@{weapon300d}+@{weapondamagebonus}]]}} {{coluna=[[?{Bônus ou penalidade:|0}+@{weaponbonus}+[[@{weapontotal}+?{Tipo de defesa do alvo?|L, @{weaponL}|M, @{weaponM}|P, @{weaponP}}]]-?{Modificador de defesa?|0} ]]}} {{d20=[[1d20]]}} {{falha=Falha}} {{rotineiro=0% │ 0}} {{facil=25% │ [[@{weapon25d}+@{weapondamagebonus}]]}} {{medio=50% │ [[@{weapon50d}+@{weapondamagebonus}]]}} {{dificil=75% │ [[@{weapon75d}+@{weapondamagebonus}]]}} {{muitodificil=100% │ [[@{weapon100d}+@{weapondamagebonus}]]}} {{muitodificil+=125% │ [[@{weapon125d}+@{weapondamagebonus}]]}} {{critico=Crítico}} {{falha+20=Falha}} {{rotineiro+20=50% │ [[@{weapon50d}+@{weapondamagebonus}]]}} {{facil+20=75% │ [[@{weapon75d}+@{weapondamagebonus}]]}} {{medio+20=100% │ [[@{weapon100d}+@{weapondamagebonus}]]}} {{dificil+20=125% │ [[@{weapon125d}+@{weapondamagebonus}]]}} {{muitodificil+20=150% │ [[@{weapon150d}+@{weapondamagebonus}]]}} {{muitodificil++20=175% │ [[@{weapon175d}+@{weapondamagebonus}]]}} {{critico+20=Crítico}} {{falha+40=Falha}} {{rotineiro+40=100% │ [[@{weapon100d}+@{weapondamagebonus}]]}} {{facil+40=125% │ [[@{weapon125d}+@{weapondamagebonus}]]}} {{medio+40=150% │ [[@{weapon150d}+@{weapondamagebonus}]]}} {{dificil+40=175% │ [[@{weapon175d}+@{weapondamagebonus}]]}} {{muitodificil+40=200% │ [[@{weapon200d}+@{weapondamagebonus}]]}} {{muitodificil++40=225% │ [[@{weapon225d}+@{weapondamagebonus}]]}} {{critico+40=Crítico}} {{descricao=@{atackextrainformations}}}" ></button> </div> </fieldset> </div> </div> <script type="text/worker"> //Set Active Defense, Movement, and Description according to the Maneuver value on("sheet:opened change:repeating_weaponselection_weaponname", function() { getAttrs(["repeating_weaponselection_weaponname", "repeating_weaponselection_weaponprof", "repeating_weaponselection_weaponnivel", "repeating_weaponselection_weapondamage", "repeating_weaponselection_weaponrange","repeating_weapon_calibre","repeating_weaponselection_weaponrdf","repeating_weaponselection_weaponammo", "repeating_weaponselection_weaponheight", "repeating_weaponselection_weaponstreght1hand", "repeating_weaponselection_weaponstrength2hand"], function(value) { if (value.weaponname === "0") { console.log("Setting repeating_weaponselection_weaponprof, repeating_weaponselection_weaponnivel, repeating_weaponselection_weapondamage,repeating_weaponselection_weaponrange,repeating_weaponselection_weaponweaponcalibre,repeating_weaponselection_weaponrdf, repeating_weaponselection_weaponammo, repeating_weaponselection_weaponheight, repeating_weaponselection_weaponstrenght1hand, repeating_weaponselection_weaponstrength2hand"); setAttrs({repeating_weaponselection_weaponprof: " ", repeating_weaponselection_weaponnivel: " ", repeating_weaponselection_weapondamage: " ", repeating_weaponselection_weaponrange: " ", repeating_weaponselection_weaponcalibre: " ", repeating_weaponselection_weaponrdf: " ", repeating_weaponselection_weaponammo: " ", repeating_weaponselection_weaponheight: " ", repeating_weaponselection_weaponstrenght1hand: " ", repeating_weaponselection_weaponstrenght2hand: " "}); } else if (value.weaponname === "1") { console.log("Setting repeating_weaponselection_weaponprof, repeating_weaponselection_weaponnivel, repeating_weaponselection_weapondamage,repeating_weaponselection_weaponrange,repeating_weaponselection_weaponweaponcalibre,repeating_weaponselection_weaponrdf, repeating_weaponselection_weaponammo, repeating_weaponselection_weaponheight, repeating_weaponselection_weaponstrenght1hand, repeating_weaponselection_weaponstrength2hand"); setAttrs({repeating_weaponselection_weaponprof: "Energia", repeating_weaponselection_weaponnivel: "@{energia} ", repeating_weaponselection_weapondamage: "mortal", repeating_weaponselection_weaponrange: "100/400m", repeating_weaponselection_weaponcalibre: "4mm", repeating_weaponselection_weaponrdf: "10", repeating_weaponselection_weaponammo: "20", repeating_weaponselection_weaponheight: "0.7", repeating_weaponselection_weaponstrenght1hand: "@{weaponweight}/0.2", repeating_weaponselection_weaponstrenght2hand: "@{weaponweight}/0.7"}); } else { console.log("Didn't update anything..."); } console.log("Finished with doing stuff!"); }); }); </script> <div>
1594867168
GiGs
Pro
Sheet Author
API Scripter
Your html has a few too many <divs>. That isnt stopping the sheet worker, but you hsould try to fix that. Also your select has a problem:                         <select name="attr_weaponname"> <option class="sheet-weapongrouplevel" value="0" selected>N/A</option> <option class="sheet-weapongrouplevel" value="1" selected>Tantalum</option> </select> You cant have selected on more than one item Also, you can use classes there, but you probably want it on the select instead. I'd also suggest not use numbers there, just use the name of the weapon. So, I'd rewrite that as                         <select name="attr_weaponname" class="sheet-weapongrouplevel" > <option value="na" selected>N/A</option> <option value="tantalum" selected>Tantalum</option> </select> Using all lower case for the values, and removing characters that dont work in javascript variable names (like spaces and /) is a good idea - it makes a suggestion I'm going to offer later easier to use. On to your problem. There are two issues. The first line should be on("change:repeating_weaponselection:weaponname", function() { You need a colon between the repeating section name and the field name on change line. Also, sheet:opened  doesnt work in workers like this - it only works for repeating sections when you supply the row id using getSectionIDs. The second problem: this line if (value.weaponname === "0") { should be if (value.repeating_weaponselection_weaponname === "0") { You need to use the repeating section name when grabbing the values of attributes. The overall structure of the sheet worker is much clunkier than it needs to be - I'll post another approach to this type of lookup in a little bit. But I'd recommend not doing this:
1594868259
GiGs
Pro
Sheet Author
API Scripter
Here's another way to build that sheet worker. It assumes you have followed my suggestion on the select option values. The advantage of this approach is its much easier to add extra weapon details.  on('change:repeating_weaponselection:weaponname', function() {         getAttrs(['repeating_weaponselection_weaponname'], function(value) {             const weaponstats = {                 na: {                     repeating_weaponselection_weaponprof: ' ',                     repeating_weaponselection_weaponnivel: ' ',                     repeating_weaponselection_weapondamage: ' ',                     repeating_weaponselection_weaponrange: ' ',                     repeating_weaponselection_weaponcalibre: ' ',                     repeating_weaponselection_weaponrdf: ' ',                     repeating_weaponselection_weaponammo: ' ',                     repeating_weaponselection_weaponheight: ' ',                     repeating_weaponselection_weaponstrenght1hand: ' ',                     repeating_weaponselection_weaponstrenght2hand: ' '                 },                  tantalum: {                     repeating_weaponselection_weaponprof: 'Energia',                     repeating_weaponselection_weaponnivel: '@{energia} ',                     repeating_weaponselection_weapondamage: 'mortal',                     repeating_weaponselection_weaponrange: '100/400m',                     repeating_weaponselection_weaponcalibre: '4mm',                     repeating_weaponselection_weaponrdf: '10',                     repeating_weaponselection_weaponammo: '20',                     repeating_weaponselection_weaponheight: '0.7',                     repeating_weaponselection_weaponstrenght1hand: '@{weaponweight}/0.2',                     repeating_weaponselection_weaponstrenght2hand: '@{weaponweight}/0.7'                 },             };             const weapon = weaponstats[value.repeating_weaponselection_weaponname];             setAttrs(weapon);         });     }); For extra weapons, just copy one of the existing ones (na or tantalum) and paste it directly below, then change the values. You dont need to change anything else in the worker. Note also you dont need anything in the getAttrs apart from the weaponname attribute. You only need to put in getAttrs those attributes you are reading from the sheet. You are only reading weaponname, the rest you are setting and dont care about their previous values.
1594868760
GiGs
Pro
Sheet Author
API Scripter
Final note: you have a couple more issues in your html. The last two fields in the repeating section are set as disabled="true" . You need to change those to readonly . Sheet workers cant modify disabled attributes. Also, all of those attributes are set as type="number" , but most of the values are not actually numbers. You should change them to type="text" . From the above example, only weaponrdf, weaponammo, and weaponheight will work as type="number".
GiGs thank you so much for your patience and class! It worked really well! I noticed only 2 other problems that I couldn't solve, could you help me again? In the field: repeating_weaponselection_weaponnivel: '@ {energy}', I would like the weapon level to be updated by the value of the energy proficiency that is on the character sheet, but the sheet work only fills with the field "@ {energy}". In the fields: repeating_weaponselection_weaponstrenght1hand: '@ {weaponweight} /0.2',   repeating_weaponselection_weaponstrenght2hand: '@ {weaponweight} /0.7' I would like to use the weaponweight value to do this calculation but it only displays the formula.
1594902627
GiGs
Pro
Sheet Author
API Scripter
I just noticed couple of typos in the attribute values. In the html the attribute is called weapon w eight, but in the sheet worker its called weapon h eight. The 1h and 2h forms mispell repeating_weaponselection_weaponstreng ht 1hand - notice the the are the wrong way around. That typo also occurs in the html labels above the repeating section. The weaponweight can be done, but before coding it, you need to account for all weapons not listed here. Is every single weapon  in the same format: @{weaponweight}/ some number?   If so the code doesnt need changing much to handle it. If the rules vary, you'll need to tell me all the possible variations so we can code it properly. For @{energia} : is this an attribute outside the repeating section? That requires the sheet worker be written a little differently. It's still doable,  Is  repeating_weaponselection_weaponnivel  always exactly equal to the attribute listed in  repeating_weaponselection_weaponprof ?
About weight: 1 - I made the corrections, thanks again! 2 - the formula is always the same 1hand = weaponweight / 0.2 and 2hand = weaponweight / 0.7 About @ {energia}: 1 - yes, it is an attribute outside the replay session 2 - Is repeating_weaponselection_weaponnivel always exactly equal to the attribute listed in repeating_weaponselection_weaponprof? Yes.
1594908679
GiGs
Pro
Sheet Author
API Scripter
ThiagoR said: About weight: 1 - I made the corrections, thanks again! 2 - the formula is always the same 1hand = weaponweight / 0.2 and 2hand = weaponweight / 0.7 About @ {energia}: 1 - yes, it is an attribute outside the replay session 2 - Is repeating_weaponselection_weaponnivel always exactly equal to the attribute listed in repeating_weaponselection_weaponprof? Yes. That makes weight easy to handle. Can you supply a list of all the attributes that can be used there? I'm off to bed now, I'll come back to this tomorrow.
are all values of the repeating session and: energy -> captured from another character sheet attribute outside the repeating session energiavalue -> captured from another attribute of the token outside the repeating session weaponstreght1hand -> is always calculated as weaponweight / 0.2 weaponstreght2hand -> is always calculated as weaponweight / 0.7 a good long rest for you!
1594971904
GiGs
Pro
Sheet Author
API Scripter
ThiagoR said: energy -> captured from another character sheet attribute outside the repeating session energiavalue -> captured from another attribute of the token outside the repeating session The strengthhand calculations being consistent makes that part very easy. The energy/energyvalue: are they the only stats outside the repeating section ? I asked for a list of all the stats that any weapon might need.
1594980407
GiGs
Pro
Sheet Author
API Scripter
So, here is a sheet worker that does what you need.  While doing it i noticed you are mising the weaponrangemax  attribute in your weapons, but you have a place for it in the repeating section.  This is pretty complex, since it has to account for changes in stats outside the repeating section. For instance, when the energia  stat changes, every weapon using it has to have its value updated. Here is the worker, and I'll explain what changes you need to make for extra stuff below: const weaponskills = ['energia']; const weaponskillchanges = weaponskills.map(skill => `change:${skill}`).join(' '); on(`sheet:opened change:repeating_weaponselection:weaponname ${weaponskillchanges}`, function() {     getSectionIDs('repeating_weaponselection', idArray => {         const getFieldName = (id, field) => `repeating_weaponselection_${id}_${field}`;         const fields = ['weaponname', 'weaponprof', 'weaponweight'];         const fieldnames = [];         idArray.forEach(id => fields.forEach(field => fieldnames.push(getFieldName(id, field))));                          getAttrs([...fieldnames, ...weaponskills], value => {             const rounding = value => Math.round(value*100) /100;             const weaponstats = {                 na: {                     weaponprof: ' ',                     weaponnivel: ' ',                     weapondamage: ' ',                     weaponrange: ' ',                     weaponrangemax: ' ',                     weaponcalibre: ' ',                     weaponrdf: ' ',                     weaponammo: ' ',                     weaponweight: ' ',                     weaponstrength1hand: ' ',                     weaponstrength2hand: ' '                 },                  tantalum: {                     weaponprof: 'Energia',                     weapondamage: 'mortal',                     weaponrange: '100/400m',                     weaponcalibre: '4mm',                     weaponrdf: '10',                     weaponammo: '20',                     weaponweight: '0.7'                 }             };             const weapons = {};             idArray.forEach(id => {                 const weaponName = value[getFieldName(id, 'weaponname')];                 Object.keys(weaponstats[weaponName]).forEach(key => {                     weapons[getFieldName(id, key)] = weaponstats[weaponName][key];                 });                 if(weaponName !== 'na') {                     const skill = value[weaponstats[weaponName].weaponprof.toLowerCase()] || 0;                     weapons[getFieldName(id, 'weaponnivel')] = skill;                                  const weight = parseFloat(weaponstats[weaponName].weaponweight) ||0;                     weapons[getFieldName(id, 'weaponstrength1hand')] = rounding(weight / 0.2);                     weapons[getFieldName(id, 'weaponstrength2hand')] = rounding(weight / 0.7);                 }             });             setAttrs(weapons);         });     }); }); Notice the first line: const   weaponskills  = [ 'energia' ]; If you have any weapons that use different attributes, you must add them to this array, separated by commas, like say: const weaponskills = ['energia', 'anotherskill', 'yet-another-skill']; Then for your individual weapons, notice the tantalum example:                 tantalum: {                     weaponprof: 'Energia',                     weapondamage: 'mortal',                     weaponrange: '100/400m',                     weaponcalibre: '4mm',                     weaponrdf: '10',                     weaponammo: '20',                     weaponweight: '0.7'                 }, Notice the weaponnivel, weaponstrength1hand, and weaponstrength2hand attributes are not listed here. Thats because the script calculates them. Every possible weaponprof entry must be added to the weaponskills array at the start.