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

[API Request] I have an api, would like to modify and add 2 other things to it please

1658960818

Edited 1658960890
This is a HP announcement that is whispered to GM when a player changes their HP up or down. Sometimes, I have caught cheater during the off week fudge their hp. But other than that - to ensure the correct amount has been added/removed during gameplay is the main usage. I'd like for it to also include AC and "Durability". Basically - the 3 bars are as followed: Bar 1 (Green) = HP Bar 2 (Blue) = Durability (This is a attribute that I will be adding to their character sheet)  Bar 3 (Red) = AC So I'd like the api to do the same as it does with HP, but with the other 2 bars as well.  I tried copy/pasting the HP section and changing what it needed to be and failed miserably.  on("change:attribute", function(obj, prev) { if(obj.get("name") !== "hp") return; var damageTaken = 0; var currenthp =0; var oldhp = 0; var max = 0; var charName = getObj("character", obj.get("_characterid")); currenthp = obj.get("current"); oldhp = prev["current"]; max = obj.get("max"); if (currenthp > max) { currenthp = currenthp; obj.set("current", currenthp); } damageTaken = currenthp - oldhp; if (damageTaken < 0 ) { sendChat(charName.get("name"), "/w gm has taken " + -1 * damageTaken + " damage"); } if (damageTaken > 0 ) { sendChat(charName.get("name"), "/w gm has healed " + damageTaken + " damage"); } }); on("change:graphic:bar1_value", function(obj, prev) { //Ignores players, handled above if(obj.get("represents") != "") return; if (Campaign().get("initiativepage") == false) return; var damageTaken = 0; var currenthp =0; var oldhp = 0; var max = 0; var charName = obj.get("name"); currenthp = obj.get("bar1_value"); oldhp = prev["bar1_value"]; max = obj.get("bar1_max"); if (currenthp > max) { currenthp = currenthp; obj.set("bar1_value", currenthp); } damageTaken = currenthp - oldhp; if (damageTaken < 0 ) { sendChat(charName, "/me has taken " + -1 * damageTaken + " damage"); } if (damageTaken > 0 ) { sendChat(charName, "/me has healed " + damageTaken + " damage"); } });
1658963682

Edited 1658975984
timmaugh
Pro
API Scripter
Try this on. Air-coded during a break from working on the house. Post if this doesn't do what you want, and I'll check back later. on("change:attribute", function (obj, prev) {     if (!['hp', 'ac', 'durability'].includes(obj.get("name").toLowerCase())) return;     let delta = 0;     let currentVal = 0;     let oldVal = 0;     let max = 0;     let character = getObj("character", obj.get("_characterid"));     currentVal = obj.get("current");     oldVal = prev["current"];     max = obj.get("max");     if (currentVal > max) {         currentVal = max;         obj.set("current", currentVal);     }     delta = currentVal - oldVal;     sendChat(character.get("name"), `/w gm has ${delta < 0 ? 'lost' : 'gained'} ${(delta < 0 ? -1 : 1) * delta} ${obj.get('name')}.`); }); on("change:graphic", function (obj, prev) {     //Ignores players, handled above     if (!(obj.get('bar1_value') !== prev.bar1_value ||         obj.get('bar2_value') !== prev.bar2_value ||         obj.get('bar3_value') !== prev.bar3_value ||         obj.get('represents') ||         !Campaign().get('initiativepage'))     ) return;     let delta = 0;     let currentVal = 0;     let oldVal = 0;     let max = 0;     let tokenName = obj.get("name");     let msgObj = ['1', '2', '3'].reduce((m, v) => {         currentVal = obj.get("bar" + v + "_value");         oldVal = prev["bar" + v + "_value"];         max = obj.get("bar" + v + "_max");         if (currentVal > max) {             currentVal = max;             obj.set("bar"+v+"_value", currentVal);         }         if (currentVal !== oldVal) {             m.delta = currentVal - oldVal;             m.barid = v;         }         return m;     }, {});     barObj = {         '1': 'HP',         '2': 'Durability',         '3': 'AC'     };     sendChat(tokenName, `/me has ${msgObj.delta < 0 ? 'lost' : 'gained'} ${(msgObj.delta < 0 ? -1 : 1) * msgObj.delta} ${barObj[msgObj.barid]}.`); }); EDIT: caught 2 errors (that only would crop up if the current cal was more than the max. I fixed it in the above code.
PERFECT! Thank you so much Tim!
1658971195
timmaugh
Pro
API Scripter
Right on! High five!
1658976065
timmaugh
Pro
API Scripter
Caught an error and threw a fix at the code, above.
ty ty! For those interested in the 5E Durability System, lmk so I can explain it. 
ohhh, apparently I ran into an issue, a big oversight on my part. Is it possible to turn this on/off or assign it to certain tokens only? Cause I also use another api called Reporter, Supernotes & Sceneswticher which uses the bar3 and it made those unfunctionable now. Or is it possible to change it to read the attribute names and not the bars themselves? The 4 attributes would be called  "hp" "ac" "class_resource" "other_resource"
1659012845
timmaugh
Pro
API Scripter
Easiest way to do that would be just to drop the second function (the one watching "change:graphic"). Then change the second line in the first function to include all of the attributes you need to monitor. Add them in lowercase:     if (!['hp', 'ac', 'durability', 'class_resource', 'other_resource'].includes(obj.get('name').toLowerCase())) return; I started to expand the script to let you designate tokens to monitor and add more attributes to monitor during, and to let you make these designations during the game. But to do that right I'd want panels of information to show you the tokens on a given page with buttons to start/stop monitoring them. Another panel for configuring the attributes. Command line switches to make the configurations... All of that adds complexity... when, really, I think the above change does what you want it to do.
I am getting errors when deleting the 2nd function or turning on to off. If I delete everything from change grahpic - it kinda works but doesnt report the numerial value changes in the whisper. 
1659038330
timmaugh
Pro
API Scripter
There is -- unfortunately -- no "off" function, though that might be handy! There is only restarting your sandbox once you've removed the "on" listener from your code. In any case, I had a chance to test things out, and I found the script too overbearing when it came to trying to make entries to a new character (it kept trying to reset the current value because I hadn't given the attribute a max value). This version works (I tested), and it doesn't try to reset the current if it's over the max value for the attribute. It just lets you know that the data is out of alignment. Also... The script did not previously account for non-numeric input... which would give a player the ability (as an example) to change from '8' to 'jumbawaffle' to '12' and the script wouldn't properly detect the changes. In this version, if the tracked attributes are changed from text to a number OR from a number to text, the message you receive will be slightly different -- acknowledging that the attribute changed from X to Y without trying to do math. Give this version a run and see what you think... on("change:attribute", function (obj, prev) { if (!['hp', 'ac', 'durability', 'class_resource', 'other_resource'].includes(obj.get('name').toLowerCase())) return; let delta = 0; let currentVal = 0; let oldVal = 0; let max = 0; let character = getObj("character", obj.get("_characterid")); let msg = ''; currentVal = obj.get("current"); currentMax = obj.get("max"); oldVal = prev["current"]; oldMax = prev["max"]; if (currentVal !== oldVal) { if ((isNaN(Number(currentVal)) || currentVal === '') || (isNaN(Number(oldVal)) || oldVal === '')) { msg = `/w gm has changed ${obj.get('name')} from ${oldVal === '' ? '(blank)' : oldVal} to ${currentVal === '' ? '(blank)' : currentVal}`; } else { delta = currentVal - oldVal; msg = `/w gm has ${delta < 0 ? 'lost' : 'gained'} ${(delta < 0 ? -1 : 1) * delta} ${obj.get('name')}.`; if (Number(currentVal) > Number(currentMax)) { msg = `${msg} The current value (${currentVal}) is over the max value (${currentMax})`; } else { msg = `${msg} (New : ${currentVal})`; } } } else if (currentMax !== oldMax) { if ((isNaN(Number(currentMax)) || currentMax === '') || (isNaN(Number(oldMax)) || oldMax === '')) { msg = `/w gm has changed ${obj.get('name')} (max) from ${oldMax === '' ? '(blank)' : oldMax} to ${currentMax === '' ? '(blank)' : currentMax}`; } else { delta = currentMax - oldMax; msg = `/w gm has ${delta < 0 ? 'lost' : 'gained'} ${(delta < 0 ? -1 : 1) * delta} to the max value for ${obj.get('name')}.`; if (Number(currentVal) > Number(currentMax)) { msg = `${msg} The current value (${currentVal}) is over the max value (${currentMax})`; } else { msg = `${msg} (New Max: ${currentMax})`; } } } sendChat(character.get("name"), msg); });