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

Updating Character Sheets using API

I've searched and I've tinkered and I'm stuck.

Using the "D&D 5E by Roll20" Character sheets I am trying to use a script to make updates to level, strength, and the other 5 basic attributes.

var sheetInsp = findObjs({ type: 'character', name: 'character name' })[0];
var koreyValue = getAttrByName(sheetInsp.id,'strength');

returns what it should, the strength score. Same with any other attribute I try.

However, I want to be able to update (set) them, so I add:

var sheetSTR = findObjs({ type: 'attribute', characterid: sheetInsp.id, name: 'strength' }) [0];

However, var sheetValue = sheetSTR.get('current'); errors back as a get call to undefined.


I've seen other examples that use this method and work. What am I missing?

October 02 (4 years ago)

Edited October 03 (4 years ago)
timmaugh
Forum Champion
API Scripter

'Name' isn't promoted to be available in that way. I think you can only use the promoted properties in the findObj() options argument. Anything you have to reference via a get() has to be handled with a filter(). Try this instead:

var sheetSTR = findObjs({ type: 'attribute', characterid: sheetInsp.id })
    .filter(a => a.get('name') === 'strength')[0];

EDIT: Looking at this again, I could be wrong. There are times when you have to use a filter to match on name, but I'm not sure this is one.

October 03 (4 years ago)

Edited October 03 (4 years ago)

That should work. I use very similar code. Have you added a few log() lines to see what the values are? I'd try log(sheetInsp), log(sheetInsp.id), and log(sheetSTR) without the [0] first.

I'm beginning to wonder if I am working from a false premise.

Using the API I want to be able to modify the character sheet: specifically, level, strength, dexterity, wisdom, intelligence, and constitution.

I used Mike S. suggestion to log results and found that: var sheetSTR = findObjs({ type: 'attribute', characterid: sheetInsp.id}) returns a bunch of objects, but none of them are of the ones I've listed and it doesn't seem the majority of what is returned has any stats.

So, can this not be done? (setcharatt seems to access the character sheet - though I have trouble pulling the new modified results and the sheet seems to have 2 strength numbers)

Should I be accessing the sheet differently?

Do I need learn what the helper sheets are and modify those?

(and Tim, thank you for the help, but that returned nothing as well)

October 04 (4 years ago)

Edited October 04 (4 years ago)
GiGs
Pro
Sheet Author
API Scripter

This can definitely be done. If all you want to do is modify or create attributes, the chatSetAttr script can already do that.

If you need something that CSA isnt suitable for, can you post the full code you are attempting so we can see where it's going wrong?

If it's long, post it somewhere like pastebin and link it here.

Also the specific attributes you want to use it for. I'm not familiar with the 5e sheet, but I have seen numerous posters express confusion and frustration with the way the attributes on that sheet are organised, so knowing exactly which attributes you want to manipulate might reveal an issue.

I think I have everything working now. One of the main problems seems to have been that strength uses "Strength" while all the other main attributes (dexterity, constitution, etc) are fully lower case. (Unless I did something to the test character sheet...suppose I'll find out when I do more testing.)

October 11 (4 years ago)
GiGs
Pro
Sheet Author
API Scripter

Interesting. In sheet workers and in macros, the case of roll20 attribute names makes no difference. I have no idea if case matters for attribute names in the API.

October 11 (4 years ago)


GiGs said:

Interesting. In sheet workers and in macros, the case of roll20 attribute names makes no difference. I have no idea if case matters for attribute names in the API.


Case matters for attributes. I ran into this very problem with one of my scripts.

The chatSetAttr script handles the case issue expertly, but I decided not to take the time to dig through that code to learn how he does it.  :)

But it's nice to know it can be done if I need to.

October 12 (4 years ago)
GiGs
Pro
Sheet Author
API Scripter

If you use the filter method timmaugh suggested, you can eliminate case as an issue like this

var sheetSTR = findObjs({ type: 'attribute', characterid: sheetInsp.id })
    .filter(a => a.get('name').toLowerCase() === 'strength')[0];