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

How do I reference character sheet attributes when creating a new token on the map?

1597897427

Edited 1597898221
So, I'm new to API scripting and I ran into an issue that I can't seem to find an answer for. I am playing an Echo Knight fighter, and I want to be able to summon my "Echo" token onto the map, but the problem I am running into is that the Echo's AC is calculate using your character's proficiency bonus (Prof bonus + 14 to be exact) but I can't seem to get it to pull the pb from the character sheet. Currently, I just have to type in the total value for it (16), but I'd like for it to update as I level up. Any suggestions as to how I can do this? It would go into bar2. on("chat:message",function(msg){ &nbsp; &nbsp; if(msg.type=="api" &amp;&amp; msg.content.indexOf("!echo")==0) &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; var selected = msg.selected; &nbsp; &nbsp; &nbsp; &nbsp; if (selected===undefined) &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sendChat("API","Please select a character."); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var tok = getObj("graphic",selected[0]._id); &nbsp; &nbsp; &nbsp; &nbsp; var character = getObj("character",tok.get("represents")); &nbsp; &nbsp; &nbsp; &nbsp; var player = getObj("player",character.get("controlledby")); &nbsp; &nbsp; &nbsp; &nbsp; createObj("graphic",{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left:tok.get("left")+70, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; top:tok.get("top"), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height:70, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width:70, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageid:tok.get("pageid"), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; layer:"objects", &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgsrc:"<a href="https://s3.amazonaws.com/files.d20.io/images/158402412/SavO2bnpKaQ6z8eNffK1mg/thumb.png?159770993955" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/158402412/SavO2bnpKaQ6z8eNffK1mg/thumb.png?159770993955</a>", &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name:tok.get("name") + "'s Echo", &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; controlledby:player.get("id"), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; light_radius:60, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; light_dimradius:-5, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; light_hassight:true, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bar2_value:16, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bar1_value:1, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bar1_max:1 &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendChat(tok.get("name"),"Manafested an Echo of himself."); &nbsp; &nbsp; &nbsp; &nbsp; spawnFx(tok.get("left")+70,tok.get("top"),"burn-charm",tok.get("pageid")); &nbsp; &nbsp; } });
1597936677
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
attributes are associated with a character, not a token, so you just need to get your character id from the represents field of the token, and then look for your proficiency bonus attribute that is associated with that ID. Something like this: on("chat:message",function(msg){ if(msg.type=="api" &amp;&amp; msg.content.indexOf("!echo")==0) { var selected = msg.selected; if (selected===undefined) { sendChat("API","Please select a character."); return; } var tok = getObj("graphic",selected[0]._id); var character = getObj("character",tok.get("represents"));//You're already getting the character, so we'll just use the ID from it var player = getObj("player",character.get("controlledby")); let prof = findObjs({type:'attribute',name:'pb',characterid:character.id})[0]*1 || 2;//get the prof bonus. I'm assuming you're using the 5e by Roll20 sheet here. createObj("graphic",{ left:tok.get("left")+70, top:tok.get("top"), height:70, width:70, pageid:tok.get("pageid"), layer:"objects", imgsrc:"<a href="https://s3.amazonaws.com/files.d20.io/images/158402412/SavO2bnpKaQ6z8eNffK1mg/thumb.png?159770993955" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/158402412/SavO2bnpKaQ6z8eNffK1mg/thumb.png?159770993955</a>", name:tok.get("name") + "'s Echo", controlledby:player.get("id"), light_radius:60, light_dimradius:-5, light_hassight:true, bar2_value:14+prof*1,//add 14 + the prof. The *1 converts the text string of the attribute value to a number bar1_value:1, bar1_max:1 }); sendChat(tok.get("name"),"Manifested an Echo of himself."); spawnFx(tok.get("left")+70,tok.get("top"),"burn-charm",tok.get("pageid")); } });
Scott C. said: attributes are associated with a character, not a token, so you just need to get your character id from the represents field of the token, and then look for your proficiency bonus attribute that is associated with that ID. Something like this: on("chat:message",function(msg){ if(msg.type=="api" &amp;&amp; msg.content.indexOf("!echo")==0) { var selected = msg.selected; if (selected===undefined) { sendChat("API","Please select a character."); return; } var tok = getObj("graphic",selected[0]._id); var character = getObj("character",tok.get("represents"));//You're already getting the character, so we'll just use the ID from it var player = getObj("player",character.get("controlledby")); let prof = findObjs({type:'attribute',name:'pb',characterid:character.id})[0]*1 || 2;//get the prof bonus. I'm assuming you're using the 5e by Roll20 sheet here. createObj("graphic",{ left:tok.get("left")+70, top:tok.get("top"), height:70, width:70, pageid:tok.get("pageid"), layer:"objects", imgsrc:"<a href="https://s3.amazonaws.com/files.d20.io/images/158402412/SavO2bnpKaQ6z8eNffK1mg/thumb.png?159770993955" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/158402412/SavO2bnpKaQ6z8eNffK1mg/thumb.png?159770993955</a>", name:tok.get("name") + "'s Echo", controlledby:player.get("id"), light_radius:60, light_dimradius:-5, light_hassight:true, bar2_value:14+prof*1,//add 14 + the prof. The *1 converts the text string of the attribute value to a number bar1_value:1, bar1_max:1 }); sendChat(tok.get("name"),"Manifested an Echo of himself."); spawnFx(tok.get("left")+70,tok.get("top"),"burn-charm",tok.get("pageid")); } }); When I paste that code in it gives me the correct value of 16, but when I increase my character's prof bonus, it doesn't increase with it. It just stays 16.
1597950909
Kraynic
Pro
Sheet Author
Just out of curiosity, how are you making use of the api?&nbsp; Access to the api is a pro subscription perk.&nbsp; Is the creator of the game you are in a pro subscriber?
1597951275
GiGs
Pro
Sheet Author
API Scripter
The code above is not creating a link to the attribute, its just grabbing its value at the time the token was created. You'd need to set the token's 'represents' value to the character, and the token bar to a link to the attribute (bar2_link, IIRC). You'd need to get the attribute that holds the total proficiency, and if its an autocalc field, this wont work.
Kraynic said: Just out of curiosity, how are you making use of the api?&nbsp; Access to the api is a pro subscription perk.&nbsp; Is the creator of the game you are in a pro subscriber? My DM is a pro subscriber, and is allowing me to try my hand at scripting because he has no idea how to.
GiGs said: The code above is not creating a link to the attribute, its just grabbing its value at the time the token was created. You'd need to set the token's 'represents' value to the character, and the token bar to a link to the attribute (bar2_link, IIRC). You'd need to get the attribute that holds the total proficiency, and if its an autocalc field, this wont work. So, how would I set the bar2_link to the attribute? And how would I then get it to add 14 to it for the total ac?
1597955256
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
As cool as it might be to press a button and have your echo appear, is there a reason not to just create an echo character and pull it to the map when you wish? As the controller of the token, you have this ability. Just looking for the simplest solution.
1597957997
GiGs
Pro
Sheet Author
API Scripter
Levi H. said: GiGs said: The code above is not creating a link to the attribute, its just grabbing its value at the time the token was created. You'd need to set the token's 'represents' value to the character, and the token bar to a link to the attribute (bar2_link, IIRC). You'd need to get the attribute that holds the total proficiency, and if its an autocalc field, this wont work. So, how would I set the bar2_link to the attribute? And how would I then get it to add 14 to it for the total ac? You cant do the second part. You need an attribute that shows the total, including the 14. You link the bar to that &nbsp;attribute. You do it the same way as the script above sets bar1_value, only you are setting bar2_link instead. (I'd include code but I'm not at my pc.) Remember to set the represents field to the correct character too. But Keith raises a good point. If you just want to make a copy of a token, it would be a lot easier to just copy the token. That way you get all the settings the original has automatically.
1597962035
David M.
Pro
API Scripter
+1 for the second character sheet. You should be able to set the AC attribute to something like [[@{BaseCharName|pb}+14]]. Then set up the default token with all your linked values, and either just drag it out or spawn it with your script. Note the image for the token will have to be in your personal art library due to limitations of the roll20 api. Note that there are also two existing scripts that perform spawning of default tokens, King's !Summon and my !Spawn. They don't support FX (yet, but you gave me a good idea!), but might work for you. At the very least you might check out some of the code. !Summon description: <a href="https://app.roll20.net/forum/post/1609859/scripts-gm-kings-apis-summon-monster-stat-adjuster-ammo-tracker" rel="nofollow">https://app.roll20.net/forum/post/1609859/scripts-gm-kings-apis-summon-monster-stat-adjuster-ammo-tracker</a> &nbsp; !Spawn dcescription: (the actual spawning function is called spawnTokenAtXY if you want to search the github) <a href="https://app.roll20.net/forum/post/9046603/anybody-wanna-beta-test-a-default-token-spawning-script-with-toys/?pageforid=9046603#post-9046603" rel="nofollow">https://app.roll20.net/forum/post/9046603/anybody-wanna-beta-test-a-default-token-spawning-script-with-toys/?pageforid=9046603#post-9046603</a>
keithcurtis said: As cool as it might be to press a button and have your echo appear, is there a reason not to just create an echo character and pull it to the map when you wish? As the controller of the token, you have this ability. Just looking for the simplest solution. That is a good point. I suppose the only reason is that it would be cool, and, as I'm new to scripting, I figured it'd be an easy first api to build. But, sometimes the simplest solution is the best one! Thanks for the help!
David M. said: +1 for the second character sheet. You should be able to set the AC attribute to something like [[@{BaseCharName|pb}+14]]. Then set up the default token with all your linked values, and either just drag it out or spawn it with your script. Note the image for the token will have to be in your personal art library due to limitations of the roll20 api. Note that there are also two existing scripts that perform spawning of default tokens, King's !Summon and my !Spawn. They don't support FX (yet, but you gave me a good idea!), but might work for you. At the very least you might check out some of the code. !Summon description: <a href="https://app.roll20.net/forum/post/1609859/scripts-gm-kings-apis-summon-monster-stat-adjuster-ammo-tracker" rel="nofollow">https://app.roll20.net/forum/post/1609859/scripts-gm-kings-apis-summon-monster-stat-adjuster-ammo-tracker</a> &nbsp; !Spawn dcescription: (the actual spawning function is called spawnTokenAtXY if you want to search the github) <a href="https://app.roll20.net/forum/post/9046603/anybody-wanna-beta-test-a-default-token-spawning-script-with-toys/?pageforid=9046603#post-9046603" rel="nofollow">https://app.roll20.net/forum/post/9046603/anybody-wanna-beta-test-a-default-token-spawning-script-with-toys/?pageforid=9046603#post-9046603</a> Thank you for the suggestions!