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

Sheetworker does not get default values from input

1699917680

Edited 1699978728
Ines
Pro
Sheet Author
Hello, I am currently working on a character sheet for a homebrew system and I am stumbling upon some problems now. I have got a repeating section for armor, where every attribute gets a default value. <fieldset class="repeating_ruestungen"> <span><input type="checkbox" title="rs_an" name="attr_rs_an" value="1" unchecked></span> <span><input type="text" style="width: 8em" class="sheet-input" name="attr_rs_name" value="Rüstung"/></span> <span> <select style="width: 6em" name="attr_rs_zonen"> <option value="7" selected="selected">--Zonen--</option> <option value="0">R</option> <option value="1">A</option> <option value="2">B</option> <option value="3">R,A</option> <option value="4">R,B</option> <option value="5">R,A,B</option> <option value="6">K</option> </select> </span> <span><input type="number" style="width: 4em" name="attr_rs" value="0"></span> <span><input type="number" style="width: 4em" name="attr_rs_magisch" value="0"></span> <span><input type="number" style="width: 4em" name="attr_rs_rk" value="0"></span> <span><input type="number" style="width: 4em" name="attr_rs_at" value="0"></span> <span><input type="number" style="width: 4em" name="attr_rs_pa" value="0"></span> <span><input type="number" style="width: 4em" name="attr_rs_rz" value="0"></span> <span><input type="number" style="width: 4em" name="attr_rs_talent" value="0"></span> <span><input type="number" style="width: 4em" name="attr_rs_rw" value="0"></span> </fieldset> And I have got a sheetworker that is supposed to get some of the values of all rows and do something with them. on("change:repeating_ruestungen remove:repeating_ruestungen", function() { getSectionIDs("repeating_ruestungen", function(idArray) { var tmp; var tmp_list = []; for (var i=0; i < idArray.length; i++) { tmp = "repeating_ruestungen" + idArray[i] + "_rs_an"; tmp_list.push(tmp); tmp = "repeating_ruestungen" + idArray[i] + "_rs_zonen"; tmp_list.push(tmp); tmp = "repeating_ruestungen" + idArray[i] + "_rs"; tmp_list.push(tmp); tmp = "repeating_ruestungen" + idArray[i] + "_rs_magisch"; tmp_list.push(tmp); }; console.log(tmp_list); getAttrs(tmp_list, function(values) { console.log(values); var rs_list = Object.values(values); console.log(rs_list); for (var j=0; j < rs_list.length; j+=4) { /** do something **/ }; }); }); }); Since I put some logging into my code, here is the output: VM4:2692 (8) ['repeating_ruestungen-nj9mskfwkx8ln02e4w-_rs_an', 'repeating_ruestungen-nj9mskfwkx8ln02e4w-_rs_zonen', 'repeating_ruestungen-nj9mskfwkx8ln02e4w-_rs', 'repeating_ruestungen-nj9mskfwkx8ln02e4w-_rs_magisch', 'repeating_ruestungen-nj9mxtzbiaomz9gks3b_rs_an', 'repeating_ruestungen-nj9mxtzbiaomz9gks3b_rs_zonen', 'repeating_ruestungen-nj9mxtzbiaomz9gks3b_rs', 'repeating_ruestungen-nj9mxtzbiaomz9gks3b_rs_magisch'] 0: "repeating_ruestungen-nj9mskfwkx8ln02e4w-_rs_an" 1: "repeating_ruestungen-nj9mskfwkx8ln02e4w-_rs_zonen" 2: "repeating_ruestungen-nj9mskfwkx8ln02e4w-_rs" 3: "repeating_ruestungen-nj9mskfwkx8ln02e4w-_rs_magisch" 4: "repeating_ruestungen-nj9mxtzbiaomz9gks3b_rs_an" 5: "repeating_ruestungen-nj9mxtzbiaomz9gks3b_rs_zonen" 6: "repeating_ruestungen-nj9mxtzbiaomz9gks3b_rs" 7: "repeating_ruestungen-nj9mxtzbiaomz9gks3b_rs_magisch"length: 8[[Prototype]]: Array(0) VM4:2694 {repeating_ruestungen-nj9mskfwkx8ln02e4w-_rs_zonen: '2', repeating_ruestungen-nj9mskfwkx8ln02e4w-_rs_magisch: '3', repeating_ruestungen-nj9mxtzbiaomz9gks3b_rs_zonen: '2', repeating_ruestungen-nj9mxtzbiaomz9gks3b_rs_magisch: '3'} repeating_ruestungen-nj9mskfwkx8ln02e4w-_rs_magisch: "3" repeating_ruestungen-nj9mskfwkx8ln02e4w-_rs_zonen: "2" repeating_ruestungen-nj9mxtzbiaomz9gks3b_rs_magisch: "3" repeating_ruestungen-nj9mxtzbiaomz9gks3b_rs_zonen: "2" [[Prototype]]: Object VM4:2696 (4) ['2', '3', '2', '3'] 0: "2" 1: "3" 2: "2" 3: "3" length: 4 [[Prototype]]: Array(0) As you can see tmp_list is an array and has 8 enties. But when trying to get the values of these attributes I only get 4 of them. Also these values are wrong since both rows have different attributes. What I expected was something like ['1', '5', '3', '0', '0', '2', '0', '3'] since these are the values I see when opening the page. Funny enough I used almost the same coding with some other repeating sections and default values and they worked without problem. I do not see the problem right now. So hopefully someone out there is able to help. Thanks.   Edit: Is seems getAttrs only gets the values from the row/id that was changed even through several ids are requested.  So is there an method to  forcibly clear the event object? Edit2: I tried a work around by adding an action button an changing the event trigger to on clicked. But now getAttrs does not return any kind of values. It is as if non of the variables aktually exists, so there is nothing to retrive or I do not have the right to retrive the values. I even tried renaming the section but ot no avail.
1699984148

Edited 1699984658
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
I'm actually surprised you're getting any values at all as the assembly of the repeating attribute names in the sheetworker isn't right. I'd guess that getAttrs defaults back to the non repeating id format and somehow is able to parse out the id that is passed. There needs to be an underscore between repeating_ruestungen  and the id from the idArray. Your assembly should look like this (note the underscore at the end of repeating_ruestungen ): for (var i = 0; i < idArray.length; i++) { tmp = "repeating_ruestungen_" + idArray[i] + "_rs_an"; tmp_list.push(tmp); tmp = "repeating_ruestungen_" + idArray[i] + "_rs_zonen"; tmp_list.push(tmp); tmp = "repeating_ruestungen_" + idArray[i] + "_rs"; tmp_list.push(tmp); tmp = "repeating_ruestungen_" + idArray[i] + "_rs_magisch"; tmp_list.push(tmp); }; There are also some code improvements that could be made, but aren't directly causing the problem. In the html, there is no reason to wrap everything in spans. This just adds complexity to your html that will make it harder to target elements with css In the html, the unchecked  on the checkboxes doesn't actually do anything. You only need to specify if the checkbox is checked. In the js, the getArray assembly can be greatly streamlined so that a single variable isn't being reassigned continually. In the js, I would not deconstruct the values object down into just an array of values. This makes it very easy to lose track of which row you are in. Instead, use the idArray that you have access to from the getSections callback to iterate over each row and do the operations that you need on("change:repeating_ruestungen remove:repeating_ruestungen", function() { const ruestungenAttrs = ['rs_an','rs_zonen','rs','rs_magish']; getSectionIDs("repeating_ruestungen", function(idArray) { const tmp_list = idArray.reduce((arr,id) => { ruestungenAttrs.forEach(attr => { arr.push(`repeating_ruestungen_${id}_${attr}`) }) return arr; },[]); console.log(tmp_list); getAttrs(tmp_list, function(values) { // empty object to collect changes to be made. const setObj = {}; console.log(values); idArray.forEach(id => { const row = `repeating_ruestungen_${id}`; const rs_an = values[`${row}_rs_an`]; // Do something with the id. // Change the forEach to a map if you need to assemble an array of data // Use a reduce if you need to do a totalling of values // etc. }) // Once all your changes have been accumulated in the setObj, apply them to the sheet. setAttrs(setObj); }); }); }); Edit: Had a typo on the setObj variable name. Edit: Also, note that I changed from the string concatenation to template literal syntax . This is a more legible way to assemble strings from complex data.
1699988185
Ines
Pro
Sheet Author
Thanks alot. I knew I messed up somewhere. I just did not see where. It works fine now. Too much code, only one file and no one else to look at it but me. And I do know my code is kind of oldfashioned and bloated. But usually it makes it easier for me to read and identify mistakes. Once everything works as intended I am going to clean it up.
1699988982
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Glad it's working for you! and no worries about the code. I just tend to tack on code feedback whenever because I'm never sure where people's code experience is at.