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

New to the API, been trying to cobble some stuff together [HELP]

Brief aside, really enjoying the API so far. Building my character sheet was a fun challenge and playing with JavaScript has been interesting.  As someone who's only played around with this stuff, there have been a lot of great resources on the wiki that I was able to use to put my sheet together frankenstein-style. Anyway, I've got a custom rules system I'm using for my campaign, one which doesn't use a grid.  While I like the freedom this route provides in terms of movement, it does make things trickier when dealing with threat ranges and such.  To that end, I have been trying to put together a small script that we can use as a token action that will show the threat range of a unit by using the Aura 2 property.  It's supposed to fetch a few values from the character that the token represents and make that the aura radius.  Here's what I've got so far. on("chat:message", function(msg) { if(msg.type == "api" && msg.content === ("!attackrange")) { for each (var token in msg.selected) { if (token.get("aura2_radius") === 0) { token.set("aura2_radius", token.get("movement")) } else { token.set("aura2_radius", 0) } } } }); My main question is, how do I get info from the Character Sheet that is tied to the token?  I think if I could get that down I'd have a much easier time of things.  I have two number attributes on the sheet that I'd like to fetch- one is for movement, and the other is for weapon range.  Movement range should be easy, since that's just a value I have on the sheet, so I can get that no problem.  The weapon range will probably be trickier, and I haven't tried to tackle that part yet.  Weapon range as I have it on my sheet is tied to individual weapon entries in a <fieldset>, and I use a checkbox to determine which one(s) is/are equipped.  So ideally I'd also like to fetch the value of the weapon range from the weapon(s) that have their checkboxes checked.  I'm not really sure how to approach this part of it, so any advice would be appreciated.
1502658718
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
The represents value of the token will be the I'd of the character. You can then use that to search for attributes ssociated with that character
1502659124
The Aaron
Forum Champion
API Scripter
It's going to be slightly more complicated than what you have. token.get('movement') will return undefined as tokens don't have a movement property. What you have to do is get the character is the token represents: const charid = token.get('represents'); then get the attribute by name based on the character id: const movement = findObjs({ type: 'attribute', characterid: charid, name: 'movement' },{caseInsensitive: true})[0]; Then check to make sure you got one with if(movement){ hope that helps. 
Thanks both of you, that's very helpful!  I am running into an error, though, and I've been trying to figure out why.  It says: TypeError: token.get is not a function at apiscript.js:32:30 I don't understand why this would be a problem, token.get works fine in other scripts that I've put together from examples in the Wiki.  I added in a line to test if token was getting assigned a null value or something but it still gave me that error, so I didn't think that was the issue.  Here's what I've got with what you guys gave me: on("chat:message", function(msg) { if(msg.type === "api" && msg.content.indexOf("!attackrange") !== -1) { for (var token in msg.selected) { const charid = token.get('represents'); const movement = findObjs({ type: 'attribute', characterid: charid, name: 'movement' },{caseInsensitive: true})[0]; if(movement){ if (token.get("aura1_radius") === 0) { token.set("aura1_radius", movement); } else { token.set("aura1_radius", 0); } } } } });
1502667128
The Aaron
Forum Champion
API Scripter
Ah, right.  I assumed you had the token already.   Try this: on("chat:message", function(msg) {     if(msg.type === "api" && msg.content.indexOf("!attackrange") !== -1) {         _.chain(msg.selected)             .map((o)=>getObj('graphic',o._id))             .reject(_.isUndefined)             .each((token)=>{                 const charid = token.get('represents');                 const movement = findObjs({                     type: 'attribute',                     characterid: charid,                     name: 'movement'                 },{caseInsensitive: true})[0];                 if(movement){                     if (token.get("aura1_radius") === 0) {                         token.set("aura1_radius", movement);                     }                     else {                         token.set("aura1_radius", 0);                     }                 }             });         }     } });
Ahhh, got it.  I will remember to get the token in the future.  Sorry!  Really appreciate all the help so far. I tried the script, it gave me a syntax error so I removed one of the }s near the end, after that it mostly works.  It will set the radius to 0, but after that it errors out if I try to use it again to set the radius to the value of movement. Error: Firebase.update failed: First argument contains a function in property 'aura1_radius.doSave' with contents: function () { context = this; args = arguments; timestamp = _.now(); var callNow = immediate && !timeout; if (!timeout) timeout = setTimeout(later, wait); if (callNow) { result = func.apply(context, args); context = args = null; } return result; } I tried playing with it a bit and had it log the value of movement, here's what I got. {"name":"movement","current":"503","max":"","_id":"-KrSdR2eN0XuIfTGuzty","_type":"attribute","_characterid":"-KpMrJ_g-H4W3pfqQDrk"} So it seems like it has the right value, but for whatever reason it's just not writing that to the aura field?  Maybe I'm missing something.  I tried adding "movement.current" in that part, thinking that maybe it only had the property and not that specific value that I needed, but that didn't help either.
1502671874
The Aaron
Forum Champion
API Scripter
Ah, sorry.. whoops. =D You're right, you need to get the current property from the movement attribute. on("chat:message", function(msg) {     if(msg.type === "api" && msg.content.indexOf("!attackrange") !== -1) {         _.chain(msg.selected)             .map((o)=>getObj('graphic',o._id))             .reject(_.isUndefined)             .each((token)=>{                 const charid = token.get('represents');                 const movement = findObjs({                     type: 'attribute',                     characterid: charid,                     name: 'movement'                 },{caseInsensitive: true})[0];             if(movement){                 if (token.get("aura1_radius") === 0) {                     token.set("aura1_radius", movement.get('current'));                 }                 else {                     token.set("aura1_radius", 0);                 }             }         });     } });
Wow, I can't believe I missed that... I had the right idea, but I completely whiffed on the syntax.  And it was all there too.  That's my bad, I should've caught that.  Thank you for helping me so much!  It works like a charm.
1502673578
The Aaron
Forum Champion
API Scripter
No worries!