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

use ChatSetAttr to adjust repeating resource

I am using D&D 5e by Roll20 sheet, and have the 5e OGL companion script installed too (for ammo tracking). I want to track food & water, and reduce food and water supplies with a macro button once a day. I have set "Waterskin" and "Rations" as resources, and they show up in the resources area as @{repeating_resource_left} and @{repeating_resource_right} respectively. How would I use ChatSetAttr in a script/macro to reduce that resource by one for all characters, or a selected character's token? I have done a little bit of script and macro fiddling, and am an accomplished beginner, but I can't quite seem to figure out how to do this with ChatSetAttr. Thanks for any guidance on this.
1608181665

Edited 1608181841
Oosh
Sheet Author
API Scripter
The very top two resource fields aren't part of the repeating section - those are @{class_resource} and @{other_resource} (I know you're not using those two, just clarifying). The repeating resource section starts at the second row, and is indexed from 0. So for the first row of the repeating section (or the second row overall), you would access that with @{bob|repeating_resource_$0_resource_left} and @{bob|repeating_resource_$0_resource_right}. Using the row index this way can be unreliable, though it is the simplest. If your players are OK with leaving the food and drink in that row and not moving it, you should be able to decrement it pretty easily with something like this: !modattr --charid @{bob|character_id} --repeating_resource_$0_resource_left|-1 --repeating_resource_$0_resource_right|-1 Note that this section doubles up on the word resource - once in the repeating section name, and once in the attribute name. That's got me a couple of times. If your players are likely to rearrange their resource area, you can replace the row index with the attribute's unique row id. If you use your browser's HTML inspector by right clicking on the resource input area, and clicking "inspect element", you should be able to find the row id a few lines up from where it opens the sheet's HTML. There should be a line with something like this: <div class="repitem" data-reprowid="-ME4BZrxi0ep7DNm9D3u"> The -ME4BZrxi0ep7DNm9D3u part is the repeating row id, including the hyphen. If you use that instead of $0, it'll keep referencing the same row even if it's reordered by the player. These row id's are unique to each row, so if you choose to use these you'll need to find the id for each relevant row on each character sheet, whereas the indexing is common to every repeating section - you can just copy & paste the same line and change the character name.
1608183202

Edited 1608183676
Thanks Oosh, i understand this, and was able to collect the row id for both the Waterskin and the Rations for each character. But I'm now quite sure how to replace the " --repeating_resource_$0_resource_left"  with the row id. Would it be like... !modattr --charid @{bob|character_id} --repeating_resource_-MOhFUKRX1S0ijhT6c1o|-1 --repeating_resource_-LptV8NRhzc1gCMRdVzH|-1   ? But that doesn't seem to work.... (edit) Oh, I see, I need to replace the placeholder "bob" with a real character name. Got it. (edit2) Nope, I'm still stuck on swapping out the row ID... Errors Could not understand repeating attribute name repeating_resource_$-MOhFUKRX1S0ijhT6c1o. Could not understand repeating attribute name repeating_resource_$-LptV8NRhzc1gCMRdVzH. No attributes supplied. (I tried both with and without the '$')
1608183906

Edited 1608185009
Getting? somewhere... I tried... !modattr --charid @{Snowy|character_id} ---MOhFUKRX1S0ijhT6c1o|-1 ---LptV8NRhzc1gCMRdVzH|-1 For the Character "Snowy", and got this result in chat... Setting -MOhFUKRX1S0ijhT6c1o to -1, -LptV8NRhzc1gCMRdVzH to -1 for character Snowy. But the quantities of the Rations and Waterskin don't change. Furthermore, it on subsequent uses, it just changed the value by -1, but nothing happens to the amount on the character sheet, so... Setting -MOhFUKRX1S0ijhT6c1o to -2, -LptV8NRhzc1gCMRdVzH to -2 for character Snowy. Setting -MOhFUKRX1S0ijhT6c1o to -3, -LptV8NRhzc1gCMRdVzH to -3 for character Snowy.
1608185359

Edited 1608185442
And finally, using this (literally replacing $0 with the row ID) doesn't work... !modattr --charid @{Snowy|character_id} --repeating_resource_-MOhFUKRX1S0ijhT6c1o_resource_left|-1 --repeating_resource_-LptV8NRhzc1gCMRdVzH_resource_right|-1 Result: (From ChatSetAttr): Errors Repeating row id -mohfukrx1s0ijht6c1o invalid for character Snowy and repeating section repeating_resource. Repeating row id -lptv8nrhzc1gcmrdvzh invalid for character Snowy and repeating section repeating_resource. 12:07AM (From ChatSetAttr): Setting attributes Nothing to do for character Snowy.
So basically, I just don't get the syntax here.
1608199689
Oosh
Sheet Author
API Scripter
That last one you tried should be right (replacing $0 with the id), but the row id's don't look right. If the Rations & Water are on the same row (one left, one right) the id's should be the same. Basically, everything on that row will have the same row id left/left max/left name, and right/right max/right name. The attributes themselves also also have a unique id which is not shared across the repeating row, but I don't think they're visible to HTML inspection, and I don't think the chatSetAttr command line is set up to use them. Is it possible those id's are from something else? Otherwise the macro looks like it should work.
Success! That was it - I had the wrong row id. With the correct one, it is now working. THANK YOU Oosh for the help! For anyone following this thread, the final, working macro was... !modattr --charid @{Snowy|character_id} --repeating_resource_-MONkfCVVz8L52wfri1d_resource_left|-1 --repeating_resource_-MONkfCVVz8L52wfri1d_resource_right|-1
One more issue I've come across in this, is that while the script above works correctly to lower the food and water resources by one, it does NOT change the quantity of the inventory item. Manually changing the value in the repeating_resource section of the sheet does change the quantity on the inventory item, but using this script, the quantity stays the same in inventory (and thus the carried weight doesn't change!). I'll need to find a way to trigger that. Or, alternatively, is there a script that will allow me to change the quantity of inventory items, and then I wouldn't need to worry about the repeating_resource change. Hmmmm.
1608248094
Oosh
Sheet Author
API Scripter
I'm going to assume from the post in the API section that you switched to the !ammo script, and don't need the question answered? :)
To be honest, I'm am looking at both as ways to skin the cat. Both !ammo and !ChatSetAttr will do the deed, I have now learned, but neither will adjust the quantity in the inventory. Unless you later click on the input field on the sheet for the repeating_resource, then the quantity in the inventory item updates. As that happens to both scripts, its seems more of a general issue (bug?). Now, if there was a way to change the inventory quantity directly, regardless of whether the item was a resource or not, that would solve all the issues.
1608252744
Oosh
Sheet Author
API Scripter
Interesting, I didn't realise that. Funnily enough, it does seem to work if you adjust the inventory instead. As long as the resource counter was created by ticking the "use as a resource" checkbox in the inventory, the resource counter will adjust as you change the inventory. You can get the row id the same way as the resource section, and the Attribute you want is: @{bob|repeating_inventory_-rowid_itemcount} I'm not really sure how the sheet handles linking the two, and why it only works one way when adjusted by API.
Hah! YES, this totally works. By changing the itemcount in inventory, everything updates as desired - the inventory count changes, and thus the weight, AND the quantity in the repeating_resource area changes too. Thanks Oosh! For those of you following, this script decrements the quantity of the character "Snowy"'s Rations by one... !modbattr --charid @{Snowy|character_id} --repeating_inventory_-MOlHVeo-mG15wtJB8-Z_itemcount|-1
1608313266

Edited 1608314334
Just another point I noticed, while this works to decrement, the --reset function doesn't work, because there is no value for the "maximum". You would have to get the value from the repeating_resource maximum, so I guess... attr_resource_left_max ... so maybe you'd need... repeating_resource_-000000000000000000_resource_left- attr_resource_left_max ... I am just guessing that this point. Time for some testing/guessing. Update: I still am struggling with the syntaxt to get the value into the script. So far I have... !setattr --charid @{Snow|character_id} --repeating_inventory_-MJIKU85ZtSto1X85CXV_itemcount|???????? but I can't figure out how to express the max where the ???????? is. I've tried... --repeating_resource_left_max result, it puts that TEXT as the value --repeating_resource_-0TheRowIDForTheRepeatingReource_left_max ... same and variations of ... @{stuff} and those get me... TypeError: Cannot read property 'substring' of undefined
1608333923
Oosh
Sheet Author
API Scripter
Ah ok, there's a couple of ways to do that. One way is to store the max value in the inventory Attribute - all Attributes have a max value, whether or not it's visible on the sheet, or used at all - it's a handy place to store data sometimes, if you want it attached to a particular existing Attribute and you know it isn't used by the sheet. So if you first use a !setattr to set the max value (it just goes after where you would put the 'current' value), separated by another |. If you don't want to modify the current value, just use a double || !setattr --charid @{bob|character_id} --repeating_inventory_-rowid_itemcount||5 You can then reset it without needing to reference the resource section: !resetattr --charid @{bob|character_id} --repeating_inventory_-rowid_itemcount If you wanted to reference the resource section directly with chatsetattr, it would be done like this: !setattr --charid @{bob|character_id} --repeating_inventory_-rowid_itemcount|@{bob|repeating_resource_-rowid_resource_left|max} This combines the two different Attribute references - the second @{reference} is parsed by chat before chatSetAttr even sees anything, so it's already resolved to '5' (or whatever the max is). So the script just sees a simple "set this Attribute to '5' command. Something to keep in mind when using the API - all the @{Attribute}, %{Ability}, ?{Query} and [[math]] is parsed by chat before the script gets a look in, unless somekind of pass-through or escaping is done intentionally. So they're not dynamic references of any kind, they're just flat values by the time a script processes its command line.
So, this is now working perfectly. I set Max values, and can have people consume food & water, and then refresh their levels. Hooray! To take this further, how would I check to see if one of the value hits "0", and then chat out a warning, like "Bob has drunk the last of their water!" ? Some sort of... if  @{bob| repeating_inventory_-rowid_itemcount } = 0 then say "blah blah... ... sort of thing
1608607520

Edited 1608607562
Oosh
Sheet Author
API Scripter
Hmmm I can't see any way to automate that with ChatSetAttr, though you can use its custom output to report the modified amount: !modbattr --charid @{bob|character_id} --repeating_inventory_$0_itemcount|-1 --fb-header Food & Drink --fb-content _CHARNAME_ consumes _TCUR0_ @{bob|repeating_inventory_$0_itemname}, and has _CUR0_ remaining --fb-public I don't think it's quite what you want, as it won't alert you if they don't have enough left to actually eat. Are you using any other scripts in this macro? One of those might have that functionality. It's not difficult to add in, I had a similar function in a guns script I half wrote... when I get time later today I can have a look at that if you haven't found another solution.