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

Need help getting repeating resource section in GM sheet

Is there a way to add the repeating section of resources to this section of code?     //! class resources     resourceName = resolveAttr(cid, 'class_resource_name').current;     otherresourceName = resolveAttr(cid, 'other_resource_name').current;     var classResourceTotal = resolveAttr(cid, 'class_resource').max;     var classResourceCurrent = resolveAttr(cid, 'class_resource').current;     var otherResourceTotal = resolveAttr(cid, 'other_resource').max;     var otherResourceCurrent = resolveAttr(cid, 'other_resource').current;     if (resourceName && classResourceTotal > 0) output += '<br>' + resourceName + ': ' + classResourceCurrent + '/' + classResourceTotal;     if (otherresourceName && otherResourceTotal > 0) output += '<br>' + otherresourceName + ': ' + otherResourceCurrent + '/' + otherResourceTotal;     resourceName = '';     return output;
1610418861

Edited 1610420364
Oosh
Sheet Author
API Scripter
I'm not sure if you've ever done any sheet editing, but you basically need functions to replace the sheet helpers getSectionIDs, getAttrs and setAttrs to deal with repeating sections. Here's a pastebin with versions I made for a script. It doesn't use callbacks like the sheet helpers, and someone else probably has much more efficient versions, but they should work. There'll be some lines in there that were specific to what I was doing at the time, but shouldn't break anything. There's also some commented out logging that you might want to uncomment, or add to, so you can see what's happening. getRepIds is the first you'll need to find the active rows in the section. This needs an Attribute passed in that is always created with the row. This is sheet dependent, but the names you're using look like the 5e sheet (I'm not sure what GM sheet means?). So you'd run that with something like this: idArray = getRepIds(cid, 'repeating_resource_$0_resource_left') You now have the active row IDs stored as an Array in idArray for the character Id 'cid'. Next is the repeating Attribute values you need. The getRepAttrs function takes an Array of names as input, so you'd probably want to create that variable first to make it easier to change later. It also take a boolean argument at the end to create missing Attributes when they're not found in a row. I've found you generally want this set to true , as otherwise you can get script crashes if certain fields in a repeating row are not created automatically by the sheet, and later referenced. The repeating section name needs to go up to at least the $ row index marker: attrNames = ['resource_left', 'resource_left_name', 'resource_right', 'resource_right_name']; repResources = getRepAttrs(cid, 'repeating_resource_$0', idArray, attrNames, true); You'll now have all the values stored in repResources that you need, and can iterate through that as you please. I've never done any sheet writing, and never used getAttrs and setAttrs, I think the way I've done it is a little different. The repeating attributes are stored like this: [{ "prefix": "repeating_npcaction_", "rowId": "-MKk9QEjtVCV6fuJBru3", "name": { "id": "-MKk9QEnMidxsAPpfeYJ", "current": "Bite", "max": "" }, "description": { "id": "-MKk9QEpdYeC9UIILty2", "current": "Bite someone I guess", "max": "" }, }, { "prefix": "repeating_npcaction_", "rowId": "-MO0BpSyp9VPnNESbpNs", "name": { "id": "-MO0BpT3gEEqX_E6pIlq", "current": "Claw", "max": "" }, "description": { "id": "-MO0BpT3gEEqX_E6pIlq", "current": "description of Claw blah blah blah", "max": "" }, }] An Array with each entry being a repeating row, each row has two special keys at the start with the section name and row ID, in case you need to construct the full Attribute name. Each other key is one of the repeating Attributes you grabbed with getRepAttrs (in this example it's ['name', 'description']), and is an object with the current & max values and the direct Attribute ID - this is handy, as it allows the setRepAttrs function to run very cheaply using getObj(), instead of running another findObjs(). From what you've posted above it doesn't look like you need to set any values though, just read them. Anyway, now that you have the values you need in repResources, you'd want something like: if (repResources && repResource.length > 0) { repResources.forEach(row => { if (row.resource_left.current > 0 && row.resource_left_name.current) output += `<br>${row.resource_left_name.current}: ${row.resource_left.current}/${row.resource_left.max}`; if (row.resource_right.current > 0 && row.resource_right_name.current) output += `<br>${row.resource_right_name.current}: ${row.resource_right.current}/${row.resource_right.max}`; }) } That probably seems like overkill for what is probably only a handful of values in the resources section, but I don't think there's really a shortcut for dealing with repeating sections without errors. You can try getAttrByName with the $0 row indexes and plenty of error checking, but there's been a few reports of issues with the ordering, ghost rows and so forth.
Thank you, Ill play around with that a bit.  Also GMSheet is an API that give you a quick overview of player sheet in chat.