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

Temp Hp Script taking twice as much temp hp as it should

Hello was wondering if anyone could help with this script. It seems to br triggering twice and pulling double of the temp HP as it should. /* global TokenMod, ChatSetAttr */ on('ready', () => { // Configuration parameters const HPBarNum = 1; const TempHPMarker = 'chained-heart'; const DeadMarker = 'dead'; const TempHPAttributeName = 'hp_temp'; ///////////////////////////////////////////// const bar = `bar${HPBarNum}_value`; const lnk = `bar${HPBarNum}_link`; const max = `bar${HPBarNum}_max`; const mrk = `status_${TempHPMarker}`; const ded = `status_${DeadMarker}`; const checkTempHP = (obj) => { let v = parseFloat(obj.get('current')); findObjs({ type: 'graphic', represents: obj.get('characterid') }) .filter( (t) => t.get(lnk) !== '') .forEach((g)=>{ g.set(mrk,(v>0 ? (v>9 ? true : v) : false) ); }); }; const assureTempHPMarkers = () => { let queue = findObjs({ type: 'attribute', name: TempHPAttributeName }); const burndownQueue = ()=>{ if(queue.length){ let attr = queue.shift(); checkTempHP(attr); setTimeout(burndownQueue,0); } }; burndownQueue(); }; const temporalTempHPCache = {}; const accountForHPBarChange = (obj,prev) => { // 1. did hp change and is it a scale const hpMax = parseInt(obj.get(max),10); let hp = parseInt(obj.get(bar),10); const diff = hp-parseFloat(prev[bar]); if( !isNaN(hpMax) && diff !== 0 ) { let changes = {}; // 2. does it represent a character // 3. does the hp bar represent an attribute const character = getObj('character',obj.get('represents')); if( diff < 0 && character && obj.get(lnk)!=='' ){ // 4. is there temp hp const temp_hp = findObjs({ type: 'attribute', characterid: character.id, name: TempHPAttributeName })[0]; if( temp_hp ) { const now = Date.now(); // 5. have we accounted for it. if( !temporalTempHPCache.hasOwnProperty(character.id) || (now-temporalTempHPCache[character.id].when)>300 ) { // calculate necessary change const tempHP = parseFloat(temp_hp.get('current'))||0; const newTmpHP = Math.max((tempHP+diff),0); const toHeal = tempHP - newTmpHP; temporalTempHPCache[character.id]={ when: now, toHeal: toHeal }; temp_hp.set('current', newTmpHP); checkTempHP(temp_hp); } hp += temporalTempHPCache[character.id].toHeal; changes[bar] = hp; } } if(hp > hpMax) { hp = hpMax; changes[bar] = hp; changes[ded] = false; } else if(hp <= 0) { hp=0; changes[bar] = hp; changes[ded] = true; } else { changes[ded] = false; } obj.set(changes); } }; const onAttributeChange = (obj) => { if(obj.get('name') === TempHPAttributeName){ checkTempHP(obj); } }; on("change:attribute", onAttributeChange); on("change:token", accountForHPBarChange); if('undefined' !== typeof TokenMod && TokenMod.ObserveTokenChange){ TokenMod.ObserveTokenChange(accountForHPBarChange); } if('undefined' !== typeof ChatSetAttr && ChatSetAttr.registerObserver){ ChatSetAttr.registerObserver('change',onAttributeChange); } assureTempHPMarkers(); });
1541688488
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Are you changing temp hp via the token bubbles? That will count as an attribute change and as a token change; which if I'm reading your code right would both potentially cause temp hp to be deducted. Also, if you'll take some code critique, your named functions should really be true functions I stead of fat arrow functions.
1541689460

Edited 1541689538
Interesting, yes I am using the token bubbles, and The Aaron made up this script so I have no idea how to go about changing the fat arrow functions to true functions. Is there a way to fix this script to allow token bubble editing? Or is there another script that does this better? Scott C. said: Are you changing temp hp via the token bubbles? That will count as an attribute change and as a token change; which if I'm reading your code right would both potentially cause temp hp to be deducted. Also, if you'll take some code critique, your named functions should really be true functions I stead of fat arrow functions.
It also seems to be taking 2 temp hit points off even if i edit the sheet and not the token bubbles.
I once found a similar issue when using chatsetattr to remove monster HP.... turns out I'd installed the API twice! So the code was ruining twice and taking the HP twice! Probs not related, but worth considering.