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

Set Rollable Table Token as avatar in script

1509573991
GiGs
Pro
Sheet Author
API Scripter
I am making a script for generating random npcs, and I have a rollable token that has multiple faces. Is there a way when creating a new character via the API to attach a rollable token to the sheet, something like var character = createObj("character", {             avatar: <put link to token here>,             name: npcname         });
1509574841
The Aaron
Pro
API Scripter
Probably what you want is the default token. setDefaultTokenForCharacter(CharacterObj,TokenObj);
1509575600

Edited 1509575819
Bast L.
API Scripter
Heya, I used this getCleanImg thing that Aaron showed me, and this script seems to work. It only sets the avatar, not default token. I figure there's work to do on the token anyways. There's no error checking, and you need a macro !RandomNPC ?{name} You also need a table with some uploaded images. Edit: Oh, it also only does first names. I don't know regex! Edit Further: I think maybe I misread what you wanted, and this won't exactly do what you want. This just makes a new character with the name supplied in the macro, and attaches a random image from the NPC-Pictures table to its avatar. If that works, great. If not, well, maybe it gives some ideas :) on("ready", function() {          var getCleanImgsrc = function (imgsrc) {        var parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)([^\?]*)(\?[^?]+)?$/);        if(parts) {           return parts[1]+'thumb'+parts[3]+(parts[4]?parts[4]:`?${Math.round(Math.random()*9999999)}`);        }        return;     };     on("chat:message", function (msg) {         if (msg.type === "api" && msg.content.split(" ")[0] === "!RandomNPC")         {             var npcName = msg.content.split(" ")[1];             var npcs = findObjs({_type: 'tableitem', _rollabletableid: findObjs({_type: 'rollabletable', _name: 'NPC-Pictures'})[0].id});                          var character = createObj("character", {                 avatar: getCleanImgsrc(npcs[randomInteger(npcs.length) - 1].get('avatar')),                 name: npcName                              });         }     }); });
Looking over the wiki, I don't see anything in the api objects section about multi-sided tokens. I guess I'm not sure how or if we can access that.
1509577959

Edited 1509578047
GiGs
Pro
Sheet Author
API Scripter
Aaron, that looks ideal. In that set default token function, how do I get the token object? Does it have to be on the map already, or can I access it directly from the table where it was created? Bast - thanks for the script snippet, but you're right, it's not a single image I want to assign, but a rollable token with full access to all the images in that token. Edit: that said, now that I look at the way of accessing the table images, it's handy for another script I'm working on, so thank you!
1509578477
The Aaron
Pro
API Scripter
You can either create a token: let character = createObj('character',{name: 'my character'}); let token = createObj('graphic',{ name: 'My token', represents: character.id, imgsrc: someImageSrc}); setDefaultTokenForCharacter(character,token); or use one you get from either a findObjs() command or getObj() based on msg.selected. When creating the token, you can only use images in a user library, so using one on the table lets you use ones that have images from the marketplace.
1509579430
GiGs
Pro
Sheet Author
API Scripter
That create token process looks like it creates a token with a single image. Is it possible to create a rollable table token? To be clear, I'm trying to create the script so it's only dependency is that the rollable table exists, but that that token itself might not be on the table. If that's not possible, I can live with it, but I'm trying to find out
1509583307
GiGs
Pro
Sheet Author
API Scripter
I've tried using The Aaron's script snippet let character = createObj('character',{name: 'my character'}); let token = createObj('graphic',{ name: 'My token', represents: character.id, imgsrc: someImageSrc}); setDefaultTokenForCharacter(character,token); But I don't how to get a valid imgsrc, and it errors out.  I tried incorporating Bast's suggestion: on("ready", function() {          var getCleanImgsrc = function (imgsrc) {        var parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)([^\?]*)(\?[^?]+)?$/);        if(parts) {           return parts[1]+'thumb'+parts[3]+(parts[4]?parts[4]:`?${Math.round(Math.random()*9999999)}`);        }        return;     };     on("chat:message", function (msg) {         if (msg.type === "api" && msg.content.split(" ")[0] === "!tokentest")         {             var npcs = findObjs({_type: 'tableitem', _rollabletableid: findObjs({_type: 'rollabletable', _name: 'imageTest'})[0].id});             var img = getCleanImgsrc(npcs[randomInteger(npcs.length) - 1].get('avatar'));             log(img);             let character = createObj('character',{name: 'my character'});             let token = createObj('graphic',{ name: 'My token', represents: character.id, imgsrc: img});             setDefaultTokenForCharacter(character,token);                      }     }); }); But it no worky. I also tried this after a bit of googling to figure out how to use getobjs. if (msg.selected && msg.selected.length) {                 let character = createObj('character',{name: 'my character'});                 token = getObj('graphic', msg.selected[0]._id);                 setDefaultTokenForCharacter(character,token);                                                   } And anotehr failure. Incidentally the error message I am getting is: "ERROR: You cannot set the imgsrc or avatar of an object unless you use an image that is in your Roll20 Library. See the API documentation for more info." Even though I selected a rollable table token on the map, that was created using 3 images that were in my library. The same error message cropped up on the earlier efforts, again when I tried to refer to the rollable table directly. This is so frustrating! One thing I miss about maptools is how extensive the documentation was, with examples for everything. Ropll20's documentation feels like its written by javascript pros for javascript pros, and as javascript newbie who is learning javascript by writing roll20 scripts, there's so much left unsaid. Sorry for that rant. Any ideas?
Is the image actually uploaded to your library, or just from a purchase or something? In order for the API to access it, you have to upload it.
1509588028

Edited 1509588097
GiGs
Pro
Sheet Author
API Scripter
The images are ones I uploaded from my hard drive. I am wondering if it's not finding them because they are in the table, but I cant test them individually because I dont know how to get the address directly of an image in my library.
Could you post the entire script? I'm wondering if you're using what Aaron wrote directly, specifically the "someImageSrc", which I'm pretty sure is intended to be swapped out with some image source.
1509601865
GiGs
Pro
Sheet Author
API Scripter
This is the last version I tried, that I referred to above. You can see in the commented out sections, two versions I'd tried previously (which is why the getcleanimg function is in there even though its not used in the current version). on("ready", function() {          var getCleanImgsrc = function (imgsrc) {        var parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)([^\?]*)(\?[^?]+)?$/);        if(parts) {           return parts[1]+'thumb'+parts[3]+(parts[4]?parts[4]:`?${Math.round(Math.random()*9999999)}`);        }        return;     };     on("chat:message", function (msg) {         if (msg.type === "api" && msg.content.split(" ")[0] === "!tokentest")         {             /*             var npcName = msg.content.split(" ")[1];             var npcs = findObjs({_type: 'tableitem', _rollabletableid: findObjs({_type: 'rollabletable', _name: 'imageTest'})[0].id});                          var character = createObj("character", {                 avatar: getCleanImgsrc(npcs[randomInteger(npcs.length) - 1].get('avatar')),                 name: npcName                              });             */             /*             var npcs = findObjs({_type: 'tableitem', _rollabletableid: findObjs({_type: 'rollabletable', _name: 'imageTest'})[0].id});             var img = getCleanImgsrc(npcs[randomInteger(npcs.length) - 1].get('avatar'));             log(img);             let character = createObj('character',{name: 'my character'});             let token = createObj('graphic',{ name: 'My token', represents: character.id, imgsrc: img});             setDefaultTokenForCharacter(character,token);                          */                          if (msg.selected && msg.selected.length) {                 let character = createObj('character',{name: 'my character'});                 token = getObj('graphic', msg.selected[0]._id);                 setDefaultTokenForCharacter(character,token);                                                   }                      }     }); });
Oh, it's working. At least for me. This error actually came up the other day, and I should have remembered. If you check your journal, you should see a bunch of "my character" characters, with no avatar, but, which, if you edit, you'll see they have a default token. The error is reported because you're creating a character with no avatar, so you're.. I dunno, setting the avatar to be "", which reports as not in your library or something.&nbsp; Scott explained it to me here:&nbsp; <a href="https://app.roll20.net/forum/post/5704462/how-to-c" rel="nofollow">https://app.roll20.net/forum/post/5704462/how-to-c</a>...
1509622412
GiGs
Pro
Sheet Author
API Scripter
Weird. I see it is creating a token, but its not a rollable table token.&nbsp; I first created a rollable table token, placed it on the map, unchecked Is Drawing to make it a token, selected it, and ran the script. Then when I dragged the token from the new character, its was a token with no Multi-sided option. It just copied the image i had selected on the first token. I'm still hoping there's a way to bypass these steps: (placed token on the map, unchecked Is Drawing to make it a token, select it), but now I'm wondering if its even possible. Is there a way to copy a rollable token? Or maybe to create one from scratch, the script adding each image in turn?
So, I can't find any way to create a table token via API. Which is not to say there isn't one, but I'm not seeing it. As for your script, it's working for me. I click "token" to create one, uncheck is drawing, type !tokentest, drag in "my character" onto the map, and click multi-sided -&gt; random. Result: works. As for making it easier, you could always set "isdrawing" to false before saving it as the default token. However, I would actually go about all of this in a different way. It might not be what you're looking for, especially if you want to choose the picture. But maybe it does work for you. Also, the macro could have a bunch of buttons to choose the picture... Anyways, instead of saving tokens as multisided, and then randomly rolling what they look like, I would change the picture using an API command.&nbsp; Something like this (alternatively, to choose the picture, don't make it random, but have it pick from the table): on("ready", function() { var getCleanImgsrc = function (imgsrc) { var parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)([^\?]*)(\?[^?]+)?$/); if(parts) { return parts[1]+'thumb'+parts[3]+(parts[4]?parts[4]:`?${Math.round(Math.random()*9999999)}`); } return; }; on("chat:message", function (msg) { if (msg.type === "api" && msg.content === "!RandomizeNPC") { var npcs = findObjs({type: 'tableitem', rollabletableid: findObjs({type: 'rollabletable', name: 'NPC-Pictures'})[0].id}); var selectedToken = getObj(msg.selected[0]._type, msg.selected[0]._id); selectedToken.set("imgsrc", getCleanImgsrc(npcs[randomInteger(npcs.length) - 1].get('avatar'))); } }); });
1509625628

Edited 1509634417
The Aaron
Pro
API Scripter
Sorry, I should have mentioned that was just meant to be illustrative of the order of operations, not a full script.&nbsp; Hard to write full scripts on my phone. =D To create a token as a Rollable Table Token, you have fill in the sides property and set the currentSide .&nbsp; sides needs to be a URL Encoded pipe separated list of urls. You will need to set the imgsrc to the first image manually and set the sides property to 1. You can do that with: let imgs=['http://url1','http://url2',...]; let token = createObj('graphic',{ sides: encodeURIComponent(imgs.join('|')), imgsrc: imgs[0], currentSide: 0, /* other initialization properties here */ });
1509625718
The Aaron
Pro
API Scripter
Also, this "error":&nbsp; "ERROR: You cannot set the imgsrc or avatar of an object unless you use an image that is in your Roll20 Library. See the API documentation for more info." You can ignore.&nbsp; It just means the character wasn't created with an avatar image.&nbsp; The default value for the field is the empty string and the validation logic treats that as invalid, but it isn't actually an error.
1509634120
Jakob
Sheet Author
API Scripter
The Aaron said: sides needs to be a URL Encoded comma separated list of urls Small correction, the sides should be joined by vertical bars "|", not commas. Also I think you mean "currentSide: 0" instead of "sides: 1" in the code.
1509634383
The Aaron
Pro
API Scripter
Ah, good call, I'll adjust it. =D
1509845446
GiGs
Pro
Sheet Author
API Scripter
Sorry about the delay responded, I wasn't able to get back to work on this again till today. I tried incorporating your changes, Aaron, but I'm getting this error: TypeError: Cannot read property 'toJSON' of undefined TypeError: Cannot read property 'toJSON' of undefined at setDefaultTokenForCharacter (/home/node/d20-api-server/api.js:2100:32) at apiscript.js:8784:17 at eval (eval at &lt;anonymous&gt; (/home/node/d20-api-server/api.js:146:1), &lt;anonymous&gt;:65:16) at Object.publish (eval at &lt;anonymous&gt; (/home/node/d20-api-server/api.js:146:1), &lt;anonymous&gt;:70:8) at /home/node/d20-api-server/api.js:1510:12 at /home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:560 at hc (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:39:147) at Kd (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:546) at Id.Mb (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:489) at Zd.Ld.Mb (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:94:425) at /home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:111:400 The code I'm using: on("ready", function() { &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; var getCleanImgsrc = function (imgsrc) { &nbsp; &nbsp; &nbsp; &nbsp;var parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)([^\?]*)(\?[^?]+)?$/); &nbsp; &nbsp; &nbsp; &nbsp;if(parts) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return parts[1]+'thumb'+parts[3]+(parts[4]?parts[4]:`?${Math.round(Math.random()*9999999)}`); &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp;return; &nbsp; &nbsp; }; &nbsp; &nbsp; on("chat:message", function (msg) { &nbsp; &nbsp; &nbsp; &nbsp; if (msg.type === "api" && msg.content.split(" ")[0] === "!token-test") &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (msg.selected && msg.selected.length) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let character = createObj('character', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {name: 'my character'} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let imgs=[ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="https://s3.amazonaws.com/files.d20.io/images/3237296/rP0Dso1PAU1DjpL3uAI7Mg/thumb.gif?1393455436" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237296/rP0Dso1PAU1DjpL3uAI7Mg/thumb.gif?1393455436</a>', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="https://s3.amazonaws.com/files.d20.io/images/3237299/dT7cOYw8rJMU0cXVQFqJwA/thumb.gif?1393455438" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237299/dT7cOYw8rJMU0cXVQFqJwA/thumb.gif?1393455438</a>', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="https://s3.amazonaws.com/files.d20.io/images/3237293/Xk2rXRp50UEXfSf9FW_gLQ/thumb.gif?1393455435" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237293/Xk2rXRp50UEXfSf9FW_gLQ/thumb.gif?1393455435</a>' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let token = createObj('graphic',{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sides: encodeURIComponent(imgs.join('|')), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgsrc: imgs[0], &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentSide: 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* other initialization properties here */ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setDefaultTokenForCharacter(character,token); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; }); });
1509891290
The Aaron
Pro
API Scripter
You need to fill in those other initialization properties. Pageid is definitely required, maybe some other things. Add a check on token around the set default: if(token) { /* Token is valid, do something with it */ }
1509921488
GiGs
Pro
Sheet Author
API Scripter
Woohoo, we are getting there. The following code creates a token, but there's still a problem. When i right click on the token, I see the multi-sided option, but there is only one side. The slider is stuck in the zero position, the left side of the bar,&nbsp; and wont move. Choose Random always generates the first option, because it is the only one. on("ready", function() { &nbsp;&nbsp; &nbsp; &nbsp; on("chat:message", function (msg) { &nbsp; &nbsp; &nbsp; &nbsp; if (msg.type === "api" && msg.content.split(" ")[0] === "!token-test") &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if (msg.selected && msg.selected.length) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var gridSize = 70; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var currentPageID = Campaign().get('playerpageid'), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentPage = getObj('page', currentPageID); &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let character = createObj('character', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {name: 'my character'} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let imgs=[ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="https://s3.amazonaws.com/files.d20.io/images/3237296/rP0Dso1PAU1DjpL3uAI7Mg/thumb.gif?1393455436" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237296/rP0Dso1PAU1DjpL3uAI7Mg/thumb.gif?1393455436</a>', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="https://s3.amazonaws.com/files.d20.io/images/3237299/dT7cOYw8rJMU0cXVQFqJwA/thumb.gif?1393455438" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237299/dT7cOYw8rJMU0cXVQFqJwA/thumb.gif?1393455438</a>', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="https://s3.amazonaws.com/files.d20.io/images/3237293/Xk2rXRp50UEXfSf9FW_gLQ/thumb.gif?1393455435" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237293/Xk2rXRp50UEXfSf9FW_gLQ/thumb.gif?1393455435</a>' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let token = createObj('graphic',{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sides: encodeURIComponent(imgs.join('|')), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgsrc: imgs[0], &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentSide: 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* other initialization properties here */ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _pageid: currentPageID, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left: gridSize, &nbsp; &nbsp; top: 70, &nbsp; &nbsp; rotation: 0, &nbsp; &nbsp; width: gridSize, &nbsp; &nbsp; height: gridSize, &nbsp; &nbsp; layer: "token" &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(token) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* Token is valid, do something with it */ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setDefaultTokenForCharacter(character,token); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendChat("Token Test", "Token Is Invalid"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("Token is Invalid"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; }); });
1509922412
The Aaron
Pro
API Scripter
hmm... try switching this line: sides: images.map(encodeURIComponent).join('|'),
1509924483
GiGs
Pro
Sheet Author
API Scripter
Yay! that works! Is there a way to get the mouse position, for setting the left and top coordinates of the token? I am still getting a weird error message in the log, but the token creation is working. The error message is: {"who":"error","type":"error","content":"Unable to find a player or character with name: Token"} "1: NaN/NaN" I don't see the word "Token" (title case) used anywhere in the code, so it is confusing. The current version of the code is on("ready", function() { &nbsp;&nbsp; &nbsp; &nbsp; on("chat:message", function (msg) { &nbsp; &nbsp; &nbsp; &nbsp; if (msg.type === "api" && msg.content.split(" ")[0] === "!token-test") &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("====================================="); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var gridSize = 70; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var currentPageID = Campaign().get('playerpageid'), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentPage = getObj('page', currentPageID); &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let imgs=[ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="https://s3.amazonaws.com/files.d20.io/images/3237299/dT7cOYw8rJMU0cXVQFqJwA/thumb.gif?1393455438" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237299/dT7cOYw8rJMU0cXVQFqJwA/thumb.gif?1393455438</a>', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="https://s3.amazonaws.com/files.d20.io/images/3237296/rP0Dso1PAU1DjpL3uAI7Mg/thumb.gif?1393455436" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237296/rP0Dso1PAU1DjpL3uAI7Mg/thumb.gif?1393455436</a>', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="https://s3.amazonaws.com/files.d20.io/images/3237293/Xk2rXRp50UEXfSf9FW_gLQ/thumb.gif?1393455435" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237293/Xk2rXRp50UEXfSf9FW_gLQ/thumb.gif?1393455435</a>' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let character = createObj('character', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {name: 'my character', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; avatar: imgs[0], &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //var barHealth = findObjs({name: "HP-current", _type: "attribute", _characterid: character.id}, {caseInsensitive: true})[0]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let token = createObj('graphic',{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sides: imgs.map(encodeURIComponent).join('|'), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgsrc: imgs[0], &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentSide: 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* other initialization properties here */ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _pageid: currentPageID, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left: gridSize + gridSize/2, &nbsp; &nbsp; top: gridSize + gridSize/2, &nbsp; &nbsp; rotation: 0, &nbsp; &nbsp; width: gridSize, &nbsp; &nbsp; height: gridSize, &nbsp; &nbsp; layer: "objects", &nbsp; &nbsp; // character stuff &nbsp; &nbsp; represents:character.id, &nbsp; &nbsp; //bar1_link: barHealth, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(token) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* Token is valid, do something with it */ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setDefaultTokenForCharacter(character,token); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendChat("Token Test", "/w Token Created"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendChat("Token Test", "/w Token Is Invalid"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("Token is Invalid"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; }); });
1509927327
The Aaron
Pro
API Scripter
The error is from this line: sendChat("Token Test", "/w Token Is Invalid"); You probably want: sendChat("Token Test", "/w gm Token Is Invalid"); You can't get the mouse position, but the position will be overridden when the token is dragged to the table top from the character, so it only matters if you're trying to create the initial token in some particular location.
1509931280
GiGs
Pro
Sheet Author
API Scripter
Aha, that was a bit silly of me. Thanks. thanks for all your help so far, I'm so close to the finish line now, but I'm having trouble assigning a bar to a health attribute. Here's the current macro. The assignAttribute function is just there for testing, and to ensure a stat exists before I call it. The bit I'm having trouble with is between the double lines I create an attribute, to make sure it exists. Then I try to find that attribute and assign it to a variable. But that doesn't seem to work. Once that's sorted,&nbsp;in the token creation, I have the bar1_link property being assigned: is that the correct syntax? function assignAttribute(stat,value, character, max) { &nbsp; &nbsp; &nbsp; &nbsp; //return; // temp &nbsp; &nbsp; &nbsp; &nbsp; if (value === undefined ) { log(stat + " has returned an undefined value."); sendChat("Error on " + stat + " attribute", "This attribute has been ignored."); } else if (max) { createObj("attribute", { name: stat, current: value, max: value, characterid: character }); } else { createObj("attribute", { name: stat, current: value, characterid: character }); //use the line below for diagnostics! //log(stat + ", " + value); } }; on("ready", function() { &nbsp;&nbsp; &nbsp; &nbsp; on("chat:message", function (msg) { &nbsp; &nbsp; &nbsp; &nbsp; if (msg.type === "api" && msg.content.split(" ")[0] === "!token-test") &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("====================================="); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var gridSize = 70; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var currentPageID = Campaign().get('playerpageid'), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentPage = getObj('page', currentPageID); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var charName = "NewNPC Test"; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let imgs=[ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="https://s3.amazonaws.com/files.d20.io/images/3237299/dT7cOYw8rJMU0cXVQFqJwA/thumb.gif?1393455438" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237299/dT7cOYw8rJMU0cXVQFqJwA/thumb.gif?1393455438</a>', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="https://s3.amazonaws.com/files.d20.io/images/3237296/rP0Dso1PAU1DjpL3uAI7Mg/thumb.gif?1393455436" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237296/rP0Dso1PAU1DjpL3uAI7Mg/thumb.gif?1393455436</a>', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="https://s3.amazonaws.com/files.d20.io/images/3237293/Xk2rXRp50UEXfSf9FW_gLQ/thumb.gif?1393455435" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237293/Xk2rXRp50UEXfSf9FW_gLQ/thumb.gif?1393455435</a>' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let character = createObj('character', { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: charName, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; avatar: imgs[0], &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; // ====================================================== &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; assignAttribute('CON',12,character.id); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var CON = parseInt(getAttrByName(character.id, 'CON', 'current'))||0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("CON: " + CON); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var barHealth = findObjs({_type: "attribute", name: 'CON', _characterid: character.id})[0]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("barHealth: " + barHealth.name); //======================================================&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let token = createObj('graphic',{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sides: imgs.map(encodeURIComponent).join('|'), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgsrc: imgs[0], &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentSide: 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* other initialization properties here */ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _pageid: currentPageID, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left: gridSize + gridSize/2, &nbsp; &nbsp; top: gridSize + gridSize/2, &nbsp; &nbsp; rotation: 0, &nbsp; &nbsp; width: gridSize, &nbsp; &nbsp; height: gridSize, &nbsp; &nbsp; layer: "objects", &nbsp; &nbsp; // character stuff &nbsp; &nbsp; represents:character.id, &nbsp; &nbsp; name: charName, &nbsp; &nbsp; showname:true, &nbsp; &nbsp; //bar1_link: barHealth._id, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(token) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* Token is valid, do something with it */ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setDefaultTokenForCharacter(character,token); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendChat("Token Test", "/w gm Token Created"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendChat("Token Test", "/w gm Token Is Invalid"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("Token is Invalid"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; }); });
1509936347
GiGs
Pro
Sheet Author
API Scripter
Update: i figured out you need to use .get for attributes. So, it is working, almooooost. In the blow code, I first create a HPbar attribute and set its current and max values to 24, then verify they exist. Then in the token creation code, I set the bar1_link to that stat. When I do that, the HPbar stat is erased, and so the token has the link set properly, but the stats are empty. If I run the code without all the bar1 statements (bar1_link, bar1_value, bar1)max), the token is created, and on the character sheet, the HPbar stat is set properly. So according to the documentation, setting bar1_link is supposed to link the token, and update the attributes properly. the bar1_value and max lines are not supposed to be necessary. But if I run the code without them, the stat the bar is linked to gets emptied.&nbsp; Am I still doing somethign wrong? function assignAttribute(stat,value, character, max) { &nbsp; &nbsp; &nbsp; &nbsp; //return; // temp &nbsp; &nbsp; &nbsp; &nbsp; if (value === undefined ) { log(stat + " has returned an undefined value."); sendChat("Error on " + stat + " attribute", "This attribute has been ignored."); } else if (max) { createObj("attribute", { name: stat, current: value, max: value, characterid: character }); } else { createObj("attribute", { name: stat, current: value, characterid: character }); //use the line below for diagnostics! //log(stat + ", " + value); } }; on("ready", function() { &nbsp;&nbsp; &nbsp; &nbsp; on("chat:message", function (msg) { &nbsp; &nbsp; &nbsp; &nbsp; if (msg.type === "api" && msg.content.split(" ")[0] === "!token-test") &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("====================================="); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var gridSize = 70; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var currentPageID = Campaign().get('playerpageid'), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentPage = getObj('page', currentPageID); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var charName = "NewNPC Test"; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let imgs=[ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="https://s3.amazonaws.com/files.d20.io/images/3237299/dT7cOYw8rJMU0cXVQFqJwA/thumb.gif?1393455438" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237299/dT7cOYw8rJMU0cXVQFqJwA/thumb.gif?1393455438</a>', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="https://s3.amazonaws.com/files.d20.io/images/3237296/rP0Dso1PAU1DjpL3uAI7Mg/thumb.gif?1393455436" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237296/rP0Dso1PAU1DjpL3uAI7Mg/thumb.gif?1393455436</a>', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="https://s3.amazonaws.com/files.d20.io/images/3237293/Xk2rXRp50UEXfSf9FW_gLQ/thumb.gif?1393455435" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237293/Xk2rXRp50UEXfSf9FW_gLQ/thumb.gif?1393455435</a>' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let character = createObj('character', { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: charName, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; avatar: imgs[0], &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ); /* ====================================================================== &nbsp; &nbsp; &nbsp;The following lines initialise the attribute to use in healthbar. */&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; assignAttribute('HPbar',24,character.id,true); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var HPbar = parseInt(getAttrByName(character.id, 'HPbar', 'max'))||0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("HPbar: " + HPbar); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var barHealth = findObjs({ type: 'attribute', name: 'HPbar', characterid: character.id})[0]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("barHealth: " + barHealth.get('name')); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("barHealth: " + barHealth.id); // At this point, we have an attribute, with a current and max value of 24.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let token = createObj('graphic',{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sides: imgs.map(encodeURIComponent).join('|'), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgsrc: imgs[0], &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentSide: 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* other initialization properties here */ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _pageid: currentPageID, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left: gridSize + gridSize/2, &nbsp; &nbsp; top: gridSize + gridSize/2, &nbsp; &nbsp; rotation: 0, &nbsp; &nbsp; width: gridSize, &nbsp; &nbsp; height: gridSize, &nbsp; &nbsp; layer: "objects", &nbsp; &nbsp; // character stuff &nbsp; &nbsp; represents:character.id, &nbsp; &nbsp; name: charName, &nbsp; &nbsp; showname:true, &nbsp; &nbsp; bar1_link: barHealth.id, /* =============================================================== &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; The above line erases the barHealth attributes values, setting them to undefined. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Following two lines should not be necessary according to documentation &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; But without them, the above line erases the HPbar stat. &nbsp;================================================================ */ &nbsp; &nbsp; bar1_value: HPbar, &nbsp; &nbsp; bar1_max: HPbar, &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(token) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* Token is valid, do something with it */ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setDefaultTokenForCharacter(character,token); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendChat("Token Test", "/w gm Token Created"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendChat("Token Test", "/w gm Token Is Invalid"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("Token is Invalid"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; }); });
1509971610
The Aaron
Pro
API Scripter
When setting the bar links from a script, I’ve found you also need to set the bar’s value and max manually (as in, on the object create next to the linking). If you don’t do that, the bar will be blank until the first time you change the created token, such as moving or dragging it.&nbsp; Additionally, if you are linking to an autocalc field, which isn’t backed by an attribute object, to need to use its name, rather than its ID.&nbsp;
1510032349
GiGs
Pro
Sheet Author
API Scripter
What would the syntax be for using the name rather than id? This code produces a undefined result, when HP is an autocorrect field defined in the character sheet I'm using. var barHP = findObjs({ type: 'attribute', name: 'HP', characterid: character.id})[0];
1510060093
The Aaron
Pro
API Scripter
Here's the code modified from TokenMod: // token === the token to set on // character === the character the attribute is on and the token represents // attributeName === the name of the attribute let attrObj = findObjs({type: 'attribute', characterid: character.id, name: attributeName}, {caseInsensitive: true})[0]; if(attrObj) { &nbsp; token.set({ &nbsp; &nbsp; bar1_link:attrObj.id, &nbsp; &nbsp; bar1_value: attrObj.get('current'), &nbsp; &nbsp; bar1_max: attrObj.get('max') &nbsp; }); } else { &nbsp; token.set({ &nbsp; &nbsp; bar1_link: `sheetattr_${attributeName}` &nbsp; }); }