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

[Help] API & Attributes

September 07 (9 years ago)
I realize this is a pretty basic question but something still isn't clicking after several hours of trying to figure out existing scripts and goofing around with the API, so I'm hoping someone can save me further frustration. I feel like I am just overlooking something simple and a nudge from one of you might help it all click.

I'm having trouble figuring out how to get things to and from Characters and my script.

Lets say I have a character Bob with the attributes Will and Morale.

1) I want to assign the value that is currently in the Will attribute to a variable within the script that I can do things with.
2) After I do stuff in the script, I would like to assign a value from a variable in the script to the Morale attribute.

This will be for a finite, predetermined set of characters so I'm not worried about making it too dynamic or fancy.

As best I can figure this should use a getObj() and the character and attribute _id, but something isn't clicking for me.

Sorry for what is probably a pretty obvious and simple question, but I am hoping one of you won't mind pointing me down the right path.

Thanks!
September 08 (9 years ago)

Edited September 08 (9 years ago)
Lithl
Pro
Sheet Author
API Scripter
on('chat:message', function(msg) {
    if (msg.type !== 'api') return;
if (msg.content !== '!example') return;

var bob = findObjs({ type: 'character', name: 'Bob' })[0];
var bobWill = findObjs({ type: 'attribute', characterid: bob.id, name: 'Will' })[0];
var bobWillValue = bobWill.get('current');
var bobMorale = findObjs({ type: 'attribute', characterid: bob.id, name: 'Morale' })[0];

// Do stuff with bobWillValue

bobMorale.set('current', bobWillValue);
});
September 08 (9 years ago)
Thank you very much Brian! That makes sense and it finally clicks. You just made my life 1000% easier. Thanks again!
September 08 (9 years ago)
The Aaron
Pro
API Scripter
You'll also want to make sure that bob and the like are valid values:
on('chat:message', function(msg) {
    if (msg.type !== 'api') return;
    if (msg.content !== '!example') return;

    var bob = findObjs({ type: 'character', name: 'Bob' })[0];
    if( bob ) {
        var bobWill = findObjs({ type: 'attribute', characterid: bob.id, name: 'Will' })[0];
        if( bobWill ) {
            var bobWillValue = bobWill.get('current');
        }
        var bobMorale = findObjs({ type: 'attribute', characterid: bob.id, name: 'Morale' })[0];

        // Do stuff with bobWillValue (if it exists)

        if( bobMoral ) {
            bobMorale.set('current', bobWillValue);
        }
    }
});
You can also use getAttrByName() if you only want the value (See: https://wiki.roll20.net/API:Objects#Working_with_C... )
            var bobWillValue = getAttrByName(bob.id,'Will');
getAttrByName() will return undefined if the attribute doesn't exist, so you will want to check for it.

September 08 (9 years ago)
Just had the chance to test it all out and I am going to be able to do everything I wanted to through the API now. Thank you very much to you both again for the help! I really appreciate it!
September 08 (9 years ago)
The Aaron
Pro
API Scripter
Great!  Definitely post back if you have any more questions or want to share.  =D
September 08 (9 years ago)
Okay, I hit a speed bump.

Using some of the above code, when I log bob to the console I get this:

{"name":"Bob","bio":"","gmnotes":"","_defaulttoken":"","archived":false,"inplayerjournals":"all","controlledby":"all","_id":"-JoaFYmM--ec8ng4ZsVq","_type":"character","avatar":""}

Which makes sense to me, it is an array of all the different things associated with the character named "Bob".

When I log bob.id to the console I get this:

"-JoaFYmM--ec8ng4ZsVq"

Which makes sense to me since that is the value at id.

However, when I try logging something like bob.name or bob.type it returns undefined. What am I not understanding or missing here?
September 09 (9 years ago)
Nevermind. Had the epiphany just after I posted.

bob.get('name')
September 09 (9 years ago)
The Aaron
Pro
API Scripter
Glad you figured it out!  That messes with everyone initially. 
September 09 (9 years ago)
Lithl
Pro
Sheet Author
API Scripter
Yes, only the id property can be accessed directly, because it is so critical to things like getObj, finding character attributes, etc. All other properties must be accessed with the get function. (And all writable properties must be changed with the set function.)
September 10 (9 years ago)
Okay, yet another question.

Are attributes created by a character sheet template not accessible by the API?

I can get attributes that I created myself just fine, but the others listed on the Attributes & Abilities tab of the character that were put there by the character sheet (Pathfinder) come back as "undefined"...
September 10 (9 years ago)
Lithl
Pro
Sheet Author
API Scripter
The API should be able to grab them just fine. You say these are attributes that you can actually see on the Attributes & Abilities tab? Could you share the code you're using to try and access them?
September 11 (9 years ago)

Edited September 11 (9 years ago)
Correct, I can actually see them on the Attributes & Abilities Tab.  Here is the code:

on("chat:message", function(msg) {
var cmdName = "!test";
var msgTxt = msg.content;
var cmdNamePortion = msgTxt.slice (0, cmdName.length);
if (msg.type !== 'api') return;
if (cmdNamePortion !== cmdName) return;
sendChat(msg.who, "/w gm I Ran");
//TEST CODE HERE
var selectedTokenID = msg.selected[0]._id
var getCharacterAttribute = function (tokenID) {
var token = findObjs({type: 'graphic', _id: tokenID })[0];
var character = findObjs({type: 'character', _id: token.get('represents')})[0];
var attribute = findObjs({ type: 'attribute', characterid: character.id, name: 'Disposition' })[0];
log (attribute)

}
log (getCharacterAttribute(selectedTokenID))
});


When I use 'Disposition' (an attribute that I created myself) the code returns block of text for that attribute including "Friendly" when I log "attribute".  When I try something generated by the character sheet like "GP" or "Save-notes" I get "undefined"



September 11 (9 years ago)

Edited September 11 (9 years ago)
Apparently it is a bad character. I created a new character and it seems to be working there. It was working initially on the first character but something I did must have broken it... weird, but issue resolved.
September 11 (9 years ago)
The Aaron
Pro
API Scripter
Good!  Post back if you have more issues!