Currently Flabergasted

1526553960
Kastion
Pro
API Scripter
I wrote a script to change a token's HP (bar3_value) either by healing or doing damage and it doesn't seem to be working on PCs. It works PERFECT on NPCs I've had no issues at all there but when I use it on a PCs token nothing happens. Here is the script: //By Kastion  //https://app.roll20.net/users/3173313/kastion //version: 1.0 //use !change-hp <damage|healing> @{selected|character_id} to update character HP. on('ready', function() {     on('chat:message', function(msg) {        if (msg.type == "api" && msg.content.indexOf("!change-hp") !== -1) {        let args = msg.content.split(/\s+/);            var c = getObj('character', args[3].trim());            var t = findObjs({                      _type: 'graphic',                       represents: args[3].trim()                      })[0];                       if(t && !isNaN(args[2])) {                 var bar_value = parseInt(t.get("bar3_value"));                 var hp_dif = parseInt(args[2])                 var old_hp = bar_value;                                  if (args[1] == "damage")                     var new_hp = bar_value - hp_dif;                 else if (args[1] == "healing")                      var new_hp = bar_value + hp_dif;                 else {                     log("Type of HP change not specified!");                     return;                 }                 t.set({ bar3_value: new_hp });                                  if (new_hp < old_hp)                     sendChat('', "/desc " + c.get("name") + " takes " + hp_dif + " points of damage.");                 else                     sendChat('', "/desc " + c.get("name") + " heals " + hp_dif + " points of damage.");             }        };     }); }); And the script is called using this macro: !change-hp ?{Change:|damage|healing} ?{Total:|0} @{selected|character_id} I have no idea   why this would work on NPCs but none of the player characters. Can someone help me out here?
1526554231

Edited 1526554251
Kastion
Pro
API Scripter
I'm thinking this has something to do with there being multiple tokens that are representing the character and not all of them are updating when one is updated by this function..
1526555164
Kastion
Pro
API Scripter
I figured it out. By looping over findObjs instead of getting just the 0 position in the array I'm updating every single token and represents the character by looping through the findObjs list.  Some times you just need to talk it out (or write it out I suppose) to get your brain working to figure something out.
1526555688
Kastion
Pro
API Scripter
Here's the updated script in case anyone wants to use it: //By Kastion  //https://app.roll20.net/users/3173313/kastion //version: 1.0 //use !change-hp <damage|healing> @{selected|character_id} to update character HP. on('ready', function() {     log("-=> Change HP Tool Loaded <-= [Last Edited by Kastion May 17th 2018]")     on('chat:message', function(msg) {        if (msg.type == "api" && msg.content.indexOf("!change-hp") !== -1) {        let args = msg.content.split(/\s+/);            var c = getObj('character', args[3].trim());            var t = findObjs({                      _type: 'graphic',                       represents: args[3].trim()                      });                  var i = 0, changed = 0;                         for (i = 0; i < t.length; i++)                    {                if(t[i] && !isNaN(args[2]))                 {                     var bar_value = parseInt(t[i].get("bar3_value"));                     var hp_dif = parseInt(args[2])                     var old_hp = bar_value;                                          if (args[1] == "damage")                         var new_hp = bar_value - hp_dif;                     else if (args[1] == "healing")                          var new_hp = bar_value + hp_dif;                     else {                         log("Type of HP change not specified!");                         return;                     }                          t[i].set({ bar3_value: new_hp });                     changed = 1;                 }                            }             if (changed)             {                if (new_hp < old_hp)                     sendChat('', "/desc " + c.get("name") + " takes " + hp_dif + " points of damage.");                 else                     sendChat('', "/desc " + c.get("name") + " heals " + hp_dif + " points of damage.");             }        };     }); });
1526595184
Naughty Zoot
Plus
Marketplace Creator
good job
1526617996
Hi Kast, can you explain in more detail what this script does? Instead of the player adding/subtracting the #, you are controlling the hp?
1526629156
G G
Pro
Sheet Author
It allows you to create a macro that anyone can use (ideally, set it up as a token action), like the one suggested: !change-hp ?{Change:|damage|healing} ?{Total:|0} @{selected|character_id} And it assumes whatever you use for HP is linked to a token's bar3 Whenever you have a token selected, and click the macro button, you'll be prompted for damage or healing, then prompted for a number, and it will add or subtract that to the number in bar3. That will in turn alter the HP on the character sheet, for those tokens that are linked that way. Kastion: you might want to look into msg.selected. You could get rid of the final element of the macro and with a loop, set up the macro so that the GM could apply damage or healing to multiple characters in one action.
1526636713
Kastion
Pro
API Scripter
Kastion: you might want to look into msg.selected. You could get rid of the final element of the macro and with a loop, set up the macro so that the GM could apply damage or healing to multiple characters in one action. I'll do some reading and see what I can come up with. It would certainly help with AoE heals/damage.
1526638395
G G
Pro
Sheet Author
A quick look at your script and from memory (I'm about to rush out), the key line is: var c = getObj('character', args[3].trim()); For single selections, cChange that to  var c = msg.selected[0]; For groups: var selected = msg.selected; then just loop through selected, the same way you do the looping through t later, with the code you have after that inside the loop. for(let s = 0; s < msg.selected.length; s++) { var c = msg.selected[s]; etc. (The above is typing quickly from memory, but you're already using all the skills you need to do this, so you can figure it out.)
1526639975
Kastion
Pro
API Scripter
Thanks for the quick mockup of what I need to update. Appreciate it G G!
1526651501
The Aaron
Pro
API Scripter
Note that msg.selected contains {_type: 'something', _id: 'id string'} objects, so you'll need to turn those into tokens: let chars = msg.selected.map( (o) => getObj('graphic',o._id) ).filter( (t) => undefined !== t); I generally ignore the _type and specify my own then filter on undefined.  It's an easy way to get rid of any drawings or text you accidentally select.  Then you can either take the first entry, or loop over them.  Idiomatic Javascript these days is to use the Array.forEach(): chars.forEach( (c) => { // use c for something.. });
1526656684
Completely off topic but a good example for me to say this.  This is a very odd forum to me because of the enormous spread in the level of expertise between those who create content for the community to use, and the community who uses it.  Sometimes threads are full of great advice and cool stuff that I can use, and other times the threads might as well be written in Sanskrit.  It's unusual to me to see users and creators sharing the same forum. Don't get me wrong, I like it.  It just sometimes makes me feel pretty dumb.  :P
1526656817
Jakob
Pro
Sheet Author
API Scripter
Idiomatic Javascript these days is to use the Array.forEach(): aha! I knew you'd come around some day.
1526657114

Edited 1526657137
The Aaron
Pro
API Scripter
Jakob said: aha! I knew you'd come around some day. HA!  I wrote that EXPLICITLY for you Jakob.  =D  Old habits are hard to break (does that mean they don't make habits like they used to? =D ).  Must be an Eastern Hemisphere thing... Kryx is always harassing me about underscore. =D Walrus said: Completely off topic but a good example for me to say this.  This is a very odd forum to me because of the enormous spread in the level of expertise between those who create content for the community to use, and the community who uses it.  Sometimes threads are full of great advice and cool stuff that I can use, and other times the threads might as well be written in Sanskrit.  It's unusual to me to see users and creators sharing the same forum. Don't get me wrong, I like it.  It just sometimes makes me feel pretty dumb.  :P Pretty cool, right?   We all have to start somewhere and you're never to old to learn more (Thanks Jakob!).  If there's ever anything you'd like to know, I'm happy to explain it in excruciating detail.  It might always be Sanskrit to you, but everyone is wired differently and I don't mind helping if I can. =D
1526659398
One the helping topic, I see that this is built for bar 3 health, but I might be mistaken  but isn't the default on most tokens, especially drag and drop, that it's bar 1. Why would you make it for bar 3, I'm guessing you are not playing DnD? Also is this the only place I would have to change for this to work for bar 1?  t[i].set({ bar3_value: new_hp }); t[i].set({ bar1 _value: new_hp });
1526659646

Edited 1526659681
The Aaron
Pro
API Scripter
I use bar3 for DnD as well.  You can change the default bars in the Game Settings, and on the character sheet settings, generally.  In my case, I'm using shaped, which will handle it automatically in the script. Bar3 is nice for HP (in my opinion) because it's the leftmost bubble, and defaults to red.  I use Bar1 (green) for AC, and Bar2 (blue) for speed or other.  I realize you can change the colors in the Settings Tab, but I also like where the bar ends up on the token (mostly) which you can't change.  (I do change my status markers to be along the bottom.) Edit: That is the right place to change if you want to use bar1 instead.
1526662910
Yeah I use Bar 1 for health on Shaped sheet, blue for AC, and the Red for Temp HP. Because there isn't a real good way of handling temp HP without the sheet open.
1526664851
keithcurtis
Roll20 Mod Team
Walrus said: Completely off topic but a good example for me to say this.  This is a very odd forum to me because of the enormous spread in the level of expertise between those who create content for the community to use, and the community who uses it.  Sometimes threads are full of great advice and cool stuff that I can use, and other times the threads might as well be written in Sanskrit.  It's unusual to me to see users and creators sharing the same forum. Don't get me wrong, I like it.  It just sometimes makes me feel pretty dumb.  :P I feel this occasionally. We have an odd confluence of technologies and skill sets. Programmers, Web developers, Artists, Writers, Dabblers in one but tyros in another. I'm pretty comfortable with most stuff but the Javascript. Fortunately, it's a pretty level-headed and helpful community.
1526670344
Jakob
Pro
Sheet Author
API Scripter
The Aaron said: Jakob said: aha! I knew you'd come around some day. HA!  I wrote that EXPLICITLY for you Jakob.  =D  Old habits are hard to break (does that mean they don't make habits like they used to? =D ).  Must be an Eastern Hemisphere thing... Kryx is always harassing me about underscore. =D That's what I suspected :D.