
Hello, I've written the script you will find at the end of this post. It basically allows me to have automatically added and removed Status Markers depending on the HP change. It also gives some options for Temp-HP. And here comes the bug: if I have temp HP on a character and remove some hitpoints over the token, the script automatically calculates how many temp hp are left and resets the real HP to the right value. So far so good. The problem is, that all token on all other maps for this character will not change their HP properly. In the character, the HP are set right, but the token displays a wrong value in its HP-Bar. Does anyone know where the issue is? Here is the code of the script: on('ready',function(){ if ("undefined" !== typeof ApplyDamage) { ApplyDamage.registerObserver("change", updateStatus); } on('change:token', updateStatus); on("chat:message", function(msg) { if(msg.type == "api" && msg.content.indexOf("!tempHPStatus") == 0) { var args = msg.content.split(" "); var action = args[1]; var addingHP = Number(args[2]); var selected = msg.selected; if(selected===undefined){ sendChat("TempHP", "Please select a graphic."); return; } if(action === undefined) { sendChat("TempHP", "Please specify the action: -add or -reset."); return; } var tok = getObj("graphic",selected[0]._id); var character = getObj("character", tok.get("represents")); if(character === undefined) { sendChat("TempHP", "Selected Token does not represent a Character."); return; } var tmpAttr = findObjs({ name: "hp_temp", _type: "attribute", _characterid: character.id})[0]; if(tmpAttr===undefined) { sendChat("TempHP", "TempHP not supported for NPCs."); return; } var tmpHP; switch(action) { case "-add": if(addingHP === undefined || isNaN(addingHP)) { sendChat("TempHP", "Please specify a proper number of TempHP to add."); return; } var curTmpHP = parseInt(getAttrByName(character.id, "hp_temp"),10) || 0; tmpHP = curTmpHP + addingHP; break; case "-reset": tmpHP = 0; break; default: sendChat("TempHP", "Unknown action sent."); return; } tmpAttr.set("current", tmpHP); var allToken = findObjs({ _type: "graphic", represents: tok.get("represents") }); _.each(allToken, function(curTok) { updateTmpHPIcon(curTok, tmpAttr); }); } }); }); var getPageForPlayer = (function(playerid) { let player = getObj('player',playerid); if(playerIsGM(playerid)){ return player.get('lastpage'); } let psp = Campaign().get('playerspecificpages'); if(psp[playerid]){ return psp[playerid]; } return Campaign().get('playerpageid'); }); var updateStatus = (function(obj, prev) { if (obj.get("isdrawing")) { return; } var HP = { now: parseInt(obj.get("bar1_value"),10) || 0, old: parseInt(prev["bar1_value"],10) || 0, max: parseInt(obj.get("bar1_max"),10) || 0, tmp: 0, damage: 0 }; if (0 === HP.max || HP.now === HP.old) { return; } var character = getObj("character", obj.get("represents")); var tmpAttr = findObjs({ name: "hp_temp", _type: "attribute", _characterid: character.id})[0]; var hpAttr = findObjs({ name: "hp", _type: "attribute", _characterid: character.id})[0]; var playerID = character.get("controlledby").split(",")[0]; var playerPageID = getPageForPlayer(playerID); if(obj.get("pageid") === playerPageID && obj.get("layer") == "objects") { HP.tmp = parseInt(getAttrByName(character.id, "hp_temp"),10) || 0; HP.damage = HP.old - HP.now; if(HP.tmp > 0 && HP.damage > 0) { // handle temp hp if(HP.tmp >= HP.damage) { HP.tmp -= HP.damage; HP.damage = 0; } else { HP.damage -= HP.tmp; HP.tmp = 0; } } HP.new = HP.old - HP.damage; if(HP.new > HP.max) { HP.new = HP.max; } if(tmpAttr!==undefined) { tmpAttr.set("current", HP.tmp); } hpAttr.set("current", HP.new); } setTimeout(function() { var newHP = parseInt(obj.get("bar1_value"),10) || 0; var bloodied = Math.floor(HP.max/2) || 0; if(newHP <= 0) { var isNPC = findObjs({ name: "npc", _type: "attribute", _characterid: character.id})[0]; if(isNPC === undefined || !isNPC.get("current")) { // PC obj.set("status_Tod::1979519", true); obj.set("status_Bewusstlos-Unconcious::1788832", true); } else { // NPC obj.set("status_dead", true); // grab the current turnorder turnorder = Campaign().get('turnorder'); // if it's the empty string, then just treat it as an array, // otherwise decode it as a JSON string into the array of turns turnorder = ('' === turnorder) ? [] : JSON.parse(turnorder); // Get a version of it without the token we were activated for turnorder = _.reject(turnorder, function(i){ return obj.id === i.id; }); // encode the array as a JSON string and // stuff it back in the turnorder property of the Campaign Campaign().set('turnorder',JSON.stringify(turnorder)); } } else { obj.set("status_dead", false); obj.set("status_Tod::1979519", false); obj.set("status_Bewusstlos-Unconcious::1788832", false); } if(newHP > 0 && newHP <= bloodied) { obj.set("status_Verletzt-Bleeding::1788989", true); } else { obj.set("status_Verletzt-Bleeding::1788989", false); } updateTmpHPIcon(obj, tmpAttr); }, 500); }); var updateTmpHPIcon = (function(obj, tmpAttr) { if(tmpAttr!==undefined) { var newTmpHP = tmpAttr.get("current"); if(newTmpHP > 0 && newTmpHP < 10) { obj.set("status_Temp-HP::1789043", newTmpHP); } else if(newTmpHP >= 10) { obj.set("status_Temp-HP::1789043", 0); } else { obj.set("status_Temp-HP::1789043", false); } } });