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

Get repeating attribute name?

Is there a way to get the name of a repeating attribute? Like Repeating_45356462466_Attack1_Ammo. I've seen losely spoken solutions of using a hidden attribute to collect the repeating id as a workaround, but I'm uncertain how that would look in practice. Essentially I'm using the following command inside of a roll, using the chatsetattr script: !modbattr| --silent --charid @{character_id} --Attack1_Ammo|-1 And Attack1_Ammo is the repeating attribute, but obviously this one doesn't change it the way I want, it merely mods the attribute Attack1_ammo, not the repeating attribute. Is there a way to do this the way I want?
1585918822
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
I don't know which sheet this is, but that doesn't look to follow the repeating attribute pattern, which is usually something like: repeating_attributegroup_ID_attribute_name example repeating_npctraits_$01_description where the ID is the part that changes.  "Attack1_Ammo" looks like a static attribute that has a number in the name. If you look for whichever sheet this is from on the Roll20 sheet repository on github , you can look through the html file and see if you can find it, or you can open your character sheet using the Web Inspector of you browser and try to find the name.
It's a custom sheet I'm currently working on, and I'm wondering if there's a way to get that ID part so I can reference the Attack1_Ammo properly in a roll within that section of the sheet.
1585921379
Finderski
Pro
Sheet Author
Compendium Curator
If all the fields being referenced are part of the same repeating section, you don't need it, just reference the fields the way you would in any other part of the sheet. If you need that information available outside of that particular repeating row (even for another row within the same repeating section), the only way I now of is t use a sheet worker.
Hmm, interesting, and how would I use that sheetworker? Like what would it be trying to accomplish?
1585924252
Finderski
Pro
Sheet Author
Compendium Curator
Sheet workers are JavaScript that do whatever you tell them to.  They just have a function called getSectionIDs that all you to get all the section IDs for a particular repeating section (every row, not just the one you're working on). They are useful for things like calculating total weight if you have weight fields in a repeating section (such as gear or armor, etc).
All right, so this is what I've got so far: on("change:repeating_melee:attack1_ammo_max change:repeating_melee:attack1_ammo", function() {   getAttrs(["repeating_Melee_Attack1_ID"], function(values) { var id = ; // ?? setAttrs({ repeating_melee_Attack1_ID: id })    }); });  how would I get the ID of the row of the attribute that was changed?
1586254044

Edited 1586255554
GiGs
Pro
Sheet Author
API Scripter
There are two ways to get in a sheet worker - using the getSectionIDs function, and using eventInfo.sourceAttribute. Do you have any sheet workers that do anything with the repeating section yet? If so, it would be best to roll it into them. If not, try this on('change:repeating_melee:attack1_ammo_max change:repeating_melee:attack1_ammo', function(eventInfo) {       getAttrs(['repeating_melee_Attack1_ID'], function(values) {         // get the id that is already in each attribute name         // full names are always like repeating_melee_-567fghvy6_attack1_ammo, and the ID is always after the second underscore.         const id = eventInfo.sourceAttribute.split('_')[2] || '';         // get the ID saved in the destination attribute if any         const ID = values.repeating_melee_Attack1_ID;         // if no saved ID exists, or it differs from the one in the actual attribute name, save it.         if(!ID || ID != id) {             setAttrs({                 repeating_melee_Attack1_ID: id             });         }     }); }); By the way, I would recommend you change your repeating section name from repeating_Melee to repeating_melee. Upper case letters in repeating section names can cause issues with roll buttons, and using those sections in macros. The above code assumes you do that. Edit: corrected a typo in the code.
That's pretty clever! It doesn't quite seem to work though, even after changing the uppercase melee to lowercase. It doesn't seem like that entire section is firing. Is there something I can insert to the code to log if it even runs when the attribute is changed?
1586262142

Edited 1586262156
Oh nvm! The typo you fixed seems to have fixed my issue as well! Out of curiosity, what was the typo? I didn't even notice it myself after several look-overs And thank you very much for the help!
1586262986

Edited 1586271653
GiGs
Pro
Sheet Author
API Scripter
You're welcome! I had an error in this section if(!ID || ID != id) If I remember correctly it was if(!ID || !ID != id) I just started writing the logic function one way, then rewrote it and didnt remove everything from the previous version. If you want to check an event is firing, put a console.log statement immediately after the onchange line, like so on('change:repeating_melee:attack1_ammo_max change:repeating_melee:attack1_ammo', function(eventInfo) {       console.log('====== repeating_melee ammo event started =======');     getAttrs(['repeating_melee_Attack1_ID'], function(values) { Then look in the browser console (F12 usually launches it), and try changing a value. If that text doesnt appear in the console, something is wrong - either with the change line itself (usually attribute name is incorrect), or there is something wrong with the function syntax after this line.  If that text does appear, but the worker isnt doing what it should, then worker is running and you need to check the code to see what its actually doing :) If the change event doesnt fire it means the attributes stated in the repeating_me
Awesome, thanks again! Really helped me out!