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 .
×
Advertisement Create a free account

Generating multiple row IDs for repeating section

I am working on a GURPS character sheet and I have a repeating section for hit locations. Most characters will have the same hit locations, but some may not have all of them, or may have alternate hit locations. I want to create the list the first time the sheet is opened, but be editable in a repeating section.  I have modified this code that GiGs helped me with for a different sheet:     getSectionIDs('repeating_locations', idarray => {         if(idarray.length > 0) {             return;         } else {             const rowid = generateRowID();             setAttrs({                 [`repeating_locations_${rowid}_hitLocation`]: 'Skull',                 [`repeating_locations_${rowid}_hitLocationDescription`]: '-7 to hit. The part of the head that houses the brain. The skull gets an extra DR 2, the wounding modifier for all attacks increases to ¥4, knockdown rolls are at -10, and critical hits use the Critical Head Blow Table (B. 556). Exception: None of these effects apply to toxic damage.'             });         }     }); }); But I need to add a number of rows. How do I generate multiple row IDs and populate them?
1627225119
The Aaron
Forum Champion
API Scripter
You can just call GenerateRowID() multiple times to get multiple IDs. 
1627239677
GiGs
Pro
Sheet Author
API Scripter
This question should really be in the character sheet forum, it's definitely not "general use". To add to Aaron's post, it's a good idea not to call setAttrs multiple times, which you might end up doing the way you have the code setup. If you are creating a loop to create your rows, save your attributes to a variable, then use setAttrs to save all of the stats in one go, like:     getSectionIDs('repeating_locations', idarray => {         const output = {};         idarray.forEach(rowid => {             const rowid = generateRowID();             output[`repeating_locations_${rowid}_hitLocation`]= 'Skull',             output[`repeating_locations_${rowid}_hitLocationDescription`]: '-7 to hit. The part of the head that houses the brain. The skull gets an extra DR 2, the wounding modifier for all attacks increases to ¥4, knockdown rolls are at -10, and critical hits use the Critical Head Blow Table (B. 556). Exception: None of these effects apply to toxic damage.'         });         if(output) {             setAttrs(output);         }     }); Using forEach to loop through the rowids is nice because you don't have to test for idarray.length = 0. It'll just skip over that if length is zero. The if(output) tests if output is empty - if it contains any attributes, the setAttrs is run. Obviously you'd need to set that up differently if you are having different things on each row, not just repeating the head over and over... If you are setting up a hit location table, with a different location on each table, you would be better off creating it without a repeating section. Since it's fixed, it's easier for players to access the row items with macros. Repeating sections make things harder, and are best used when handling things that cant be predicted in advance and vary from character to character, like exactly what skills or equipment someone has.
I was able to do it by calling GenerateRowID() multiple times, as Aaron suggested. It has to be in a repeating section, because, while most characters will have the basic locations, some may not. A robot character might not have vital organs, or an amorphous blob character might not have a head, arms or legs, etc. My hit location tables will be rollable tables, this is just for characters to be able to see the effects of hits they have received and mark limbs as crippled.
What is the disadvantage of calling setAttrs multiple times?
1627253150

Edited 1627253208
GiGs
Pro
Sheet Author
API Scripter
Randall S. said: What is the disadvantage of calling setAttrs multiple times? It adds significant lag to a sheet, and makes it less pleasant for the player to use the sheet. You can easily accidentally create sheets that take several seconds to update every time you change an attribute - which is a nightmare to use. Admittedly in this case, for creating hit locations, it's likely to be something you only do once per character so that wouldnt really be an issue-  but its good to be mindful of this issue generally. Back to the repeating sections: aren't you likely to have several body types, with the body types themselves each having a standard hit location? In that case having a non-repeating approach is probably still better: you create each table in html, and use css to hide all but the active one. Players can then just select which body type from a dropdown, and the correct hit location table is shown.
GURPS allows characters to do things like buy extra limbs, take disadvantages where you have no legs, and all sorts of other options, as well as taking advantages where you have no vitals, or a distributed neural network that means no brain location, etc. This allows a tremendous degree of flexibility (i.e. letting players design their own races, or mutant superheroes or pretty much anything else), but creates challenges in tracking it all. I wanted to make a prettier, more sci fi looking sheet, but still keep as much flexibility as possible.
1627262129
GiGs
Pro
Sheet Author
API Scripter
That does sound pretty complex, so the repeating section may be the best way to go there.