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

Using sendChat and setting a token bar value

I am trying to write a script that rolls the HP for each token I drag onto the board. I have the dice to roll and some defaults for the HP and HP_max. The pertinent variables with example values are: npc_HP_hit_dice = '2d6+8' npc_HP = 15 npc_HP_max = 15 I have created a character with an NPC character sheet (D&D 5e if you think it is pertinent), and assigned it a default token with values for the HP and HP max. Then, I added this script on("add:graphic", function(obj) { // log('obj'); log(obj); var token = getObj('graphic', obj.id); // log('token'); log(token); var character = getObj('character', token.get('represents')); // log('character'); log(character); var npchd = getAttrByName(character.id, 'npc_HP_hit_dice'); // log('npchd'); log(npchd); var roll = "/r " + npchd; // log('roll'); log(roll); sendChat('', roll, function(res) { var result = res[0].content; result = JSON.parse(result); // log('result'); log(result); var rolledHP = result.total; log('rolledHP'); log(rolledHP); log('obj.id'); log(obj.id); token.bar3_value = rolledHP; token.bar3_max = rolledHP; log('token2'); log(token); }); }); Please excuse all the log()s, I am debugging.
I figured it out, `add:graphic` only applies to the first time a token is added to the board. Additionally, I may not have been setting the value correctly. The correct code is as follows: on("change:graphic", function(obj) { var token = getObj('graphic', obj.id); var character = getObj('character', token.get('represents')); var npchd = getAttrByName(character.id, 'npc_HP_hit_dice'); if (getAttrByName(character.id, 'is_npc') !== 1) return; var roll = "/r " + npchd; sendChat('', roll, function(res) { var result = res[0].content; result = JSON.parse(result); var rolledHP = result.total; obj.set({ 'bar3_value': rolledHP, 'bar3_max': rolledHP }); }); });
1411269246
The Aaron
Roll20 Production Team
API Scripter
I'm pretty sure that's going to reroll the HP any time you do anything to the token, like move it, rotate it, turn on statuses...
Aaron said: I'm pretty sure that's going to reroll the HP any time you do anything to the token, like move it, rotate it, turn on statuses... Yes, I just noticed that. I am trying add:graphic again, I want the API to run 1 time for each token that I drag on...
1411269485
The Aaron
Roll20 Production Team
API Scripter
Here is a script I wrote a while back to do this. I seem to recall that the trick is you have to grab the token with the add, but it isn't set up yet, so you have to wait and get it again: GIST: <a href="https://gist.github.com/shdwjk/7377de58100f4e813432" rel="nofollow">https://gist.github.com/shdwjk/7377de58100f4e813432</a>
1411271148

Edited 1411271413
@Aaron I ended up solving it by setting a token status when I drag it onto the board and then checking for the status and exiting if its there. on("ready", function() { on("change:graphic", function(obj) { if (obj.get('status_chained-heart') === true) return; var token = getObj('graphic', obj.id); var character = getObj('character', token.get('represents')); var npchd = getAttrByName(character.id, 'npc_HP_hit_dice'); var roll = "/gmroll " + npchd; sendChat('', roll, function(res) { var result = res[0].content; result = JSON.parse(result); var rolledHP = result.total; obj.set({ 'bar3_value': rolledHP, 'bar3_max': rolledHP, 'status_chained-heart': true }); var msg = 'An NPC "' + obj.get('name') + '" rolled ' + rolledHP + ' HP'; log(msg); sendChat('The API', '/w gm ' + msg); }); }); });