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] Determining a _characterid

1464860208
Finderski
Pro
Sheet Author
Compendium Curator
On my character sheet I have a checkbox called attr_is_npc. I want the API to watch for a change in that field and when that field is checked I need to change some Attributes that exist on that character sheet. I know I can use several functions to get the specific Attributes but for the life of me, I can't figure out how to assign the _characterid to a variable so I can make those calls. So, can someone point me in the right direction? How do I set it up to watch for a change in that particular ability (is_npc), and then get the id of the character that change was for? I have tried multiple things, but this is the closest I seem to get: on("change:attribute", function(obj){     var cID = obj.characterid;     var oID = obj.id;     log(obj);     log("Object ID: " + oID);     log("Character ID: " + cID); }); This is the output I get: {"name":"is_npc","current":"1","max":"","_id":"-KJ8reNsB60Jr9T9kNbE","_type":"attribute","_characterid":"-KJ8prwV5tFCPW3Fu711"} "Object ID: -KJ8reNsB60Jr9T9kNbE" "Character ID: undefined" So, I can see the character ID, but can't seem o assign it to anything.  Why does the oID assignment work, but the no cID assignment? Thanks.
1464865484

Edited 1464865771
Lithl
Pro
Sheet Author
API Scripter
id is a special property for Roll20 objects. All other properties must be accessed with the get function. on('change:attribute', function(obj) { var cID = obj.get('characterid'), oID = obj.id; log(obj); // {"name":"is_npc",...} log(`Object ID: ${oID}`); // "Object ID: -KJ8... log(`Character ID: ${cID}`); // "Character ID: -KJ8... }); Also note: for change:X events, the callback function actually has two parameters. The first parameter (obj in the above example) is the current state of the object, as a Roll20 object. The second parameter is the state of the object before  the change, as a plain old JavaScript object (in which case you'd use something like prev._characterid and prev._id and you can't omit the underscore like you can with get). Of course, in JavaScript, you can omit the last parameters from your function declarations and the system won't care at all, so if you don't need the previous state of the object, you don't have to include it in your parameter list. Also also, you can try the  Extended Syntax Roll20 Objects API script to omit the need for direct calls to get in your own code.
1464869167
Finderski
Pro
Sheet Author
Compendium Curator
Awesome! Thanks Brian. :)