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

Adding Tint to my AutoDeath API

What I am trying to achieve.... When a token is reduced to Zero HP, change Tint to White and disable Nameplate. I currently do this with !Tokenmod by using the following Macro: !token-mod --off showname --set tint_color|#ffffff But what I'm trying to achieve is to integrate these changes into the existing API script I use which automatically applies Death status when HP <= 0.   I know about 0.001% on how to script and was hoping someone could point me in the right direction on how that change script should look.   Here's the current script... on('ready', () => {     const HPBarNum = 1;     const bar = `bar${HPBarNum}_value`;     const max = `bar${HPBarNum}_max`;     const constrainHPBarToMax = (obj) => {         const hpMax = parseInt(obj.get(max),10);         if(!isNaN(hpMax) && 'token' === obj.get('subtype') && !obj.get('isdrawing') ){             let hp = parseInt(obj.get(bar),10);             let changes = {};             if(hp > hpMax) {                 hp = hpMax;                 changes[bar] = hp;                 changes.status_dead = false;             } else if(hp <= 0) {                 hp=0;                 changes[bar] = hp;                 changes.status_dead = true; // THIS IS WHERE THE NEW CODE GOES BUT WHAT DO I PUT HERE ?             } else {                 changes.status_dead = false;             }             obj.set(changes);         }     };
Check out the code for the deathtracker API it may have something you can use in there.
1603822697
Pat
Pro
API Scripter
You can fire off a message to chat with your tokenmod changes - macro-like - it may not work if the command expects a GM parameter and you pass "system" for the "speakingAs" (the first parameter), but you might give it a shot.  sendChat('System', '!token-mod --off showname --set tint_color|#ffffff"');
1603823959
David M.
Pro
API Scripter
You might have to add the tokenid of the selected token to the token-mod call via sendChat from within your script, as I think the vtt might "forget" which token was selected prior to running your master script. Something like this (untested): let selectedID = msg.selected[0]._id; sendChat('System', `!token-mod --off showname --set tint_color|#ffffff"' --ids ${selectedID})`; Note this uses the backtick notation for concatenating text with the selectedID variable.  Pat said: You can fire off a message to chat with your tokenmod changes - macro-like - it may not work if the command expects a GM parameter and you pass "system" for the "speakingAs" (the first parameter), but you might give it a shot.  sendChat('System', '!token-mod --off showname --set tint_color|#ffffff"');
1603823962
The Aaron
Roll20 Production Team
API Scripter
This should do it: on('ready', () => { const HPBarNum = 1; const bar = `bar${HPBarNum}_value`; const max = `bar${HPBarNum}_max`; const constrainHPBarToMax = (obj) => { const hpMax = parseInt(obj.get(max),10); if(!isNaN(hpMax) && 'token' === obj.get('subtype') && !obj.get('isdrawing') ){ let hp = parseInt(obj.get(bar),10); let changes = {}; if(hp > hpMax) { hp = hpMax; changes[bar] = hp; changes.status_dead = false; } else if(hp <= 0) { hp=0; changes[bar] = hp; changes.status_dead = true; changes.showname = false; changes.tint_color = '#ffffff'; } else { changes.status_dead = false; } obj.set(changes); } }; If you also want to remove the tint and show the name plate, you'd need to add similar lines in the else block and initial if block.
1603824013
David M.
Pro
API Scripter
NM, TheAaron is on the case :)
1603824150
The Aaron
Roll20 Production Team
API Scripter
Generally, it's better to make changes directly to the token, than to call out to TokenMod.  TokenMod is really just a human interface to what you can do in the API to a Token, so there's more effort involved in calling it, than there is in just doing the operations directly in code.  This is especially true in TokenMod as it very literally uses the properties and values you would be setting anyway, for the most part.  The only case where you might want to call out to a script is when that script has a large amount of functionality in it that you don't want to duplicate, and where there isn't a programatic interface you can use instead.  In those cases, it is almost always better to get a programmatic interface added than it is to call it via a sendChat() call.
The Aaron, thank you thank you thank you. This was exactly what I was looking for, just couldn't get the syntax right.
Final code that I'm using which turns nameplates back on and tint back to transparent for anyone finding this thread in the future. on('ready', () => {     const HPBarNum = 1;     const bar = `bar${HPBarNum}_value`;     const max = `bar${HPBarNum}_max`;     const constrainHPBarToMax = (obj) => {         const hpMax = parseInt(obj.get(max),10);         if(!isNaN(hpMax) && 'token' === obj.get('subtype') && !obj.get('isdrawing') ){             let hp = parseInt(obj.get(bar),10);             let changes = {};             if(hp > hpMax) {                 hp = hpMax;                 changes[bar] = hp;                 changes.status_dead = false;                 changes.showname = true;                 changes.tint_color = 'transparent';                 } else if(hp <= 0) {                 hp=0;                 changes[bar] = hp;                 changes.status_dead = true;                 changes.showname = false;                 changes.tint_color = '#ffffff';             } else {                 changes.status_dead = false;                 changes.showname = true;                 changes.tint_color = 'transparent';             }             obj.set(changes);         }     };     on("change:token", constrainHPBarToMax);     if('undefined' !== typeof TokenMod && TokenMod.ObserveTokenChange){         TokenMod.ObserveTokenChange(constrainHPBarToMax);     } });