I'm running into a strange issue with a very simple script, when I launch it runs fine for a bit but then all APIs stop working but with no error. It's almost like it's got itself in a loop and grinds everything to a halt. I notice even restarting the API Sandbox takes a bit afterword. All this script does is watch for a token on the objects layer that has a max_hp set. If it changes it calculates the % of hp remaining and tags the token with a status marker of a color that represents the health of the token. Any help is appreciated /**
* healthstate.js
*
* * Copyright 2020: Ben F.
* Licensed under the GPL Version 3 license.
* <a href="http://www.gnu.org/licenses/gpl.html" rel="nofollow">http://www.gnu.org/licenses/gpl.html</a>
*
* This script is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* To use type !hs to enable/disable the script, when enabled...
* - A token on the object layer with a health and max health
* will be flagged with a color status indicating health state,
* using green,yellow,brown,red and finally X (dead).
* - Tokens with visibility for health bar to players will not
* trigger this effect.
*
*/
var healthstat = healthstat || (function(){
'use strict';
var handleInput = function(msg) {
if ( "api" !== msg.type ) {
return;
}
// Disable/Enable the script activity with !hs
if(msg.content.indexOf("!hs") !== -1 ) {
if(state.healthstat.run_state) {
state.healthstat.run_state = false;
log(">> Health Status: OFF");
sendChat('HS','/w gm OFF');
} else {
state.healthstat.run_state = true;
log(">> Health Status: ON");
sendChat('HS','/w gm ON');
}
}
},
handleToken = function(obj) {
// Process token changes if it's a token on the objects layer with a max hp set
if(
'graphic' == obj.get('type')
&& 'token' == obj.get('subtype')
&& 'objects' == obj.get('layer')
&& '' != obj.get('bar1_max')
)
{
// To move forward the token the state 'run_state' must be true and
// the show player health for token must be disabled
if(state.healthstat.run_state && !(obj.get('showplayers_bar1'))) {
var token_name = obj.get('name') || '';
var token_hp = (obj.get('bar1_value')) || 0;
var token_hp_max = (obj.get('bar1_max')) || 0;
// Exit if one of the variables is missing
if(!(token_hp) || !(token_hp_max)) { return; }
// Calculate the % of health remaining
var token_hp_percent = ((token_hp/token_hp_max)*100);
log(">> Health Status: "+token_name+" - at "+token_hp_percent+"%");
// Clear all token status
resetStatus(obj);
if(token_hp_percent >= 75) {
obj.set("status_green",true);
} else if (token_hp_percent >= 50 && token_hp_percent <= 74) {
obj.set("status_yellow",true);
} else if (token_hp_percent >= 25 && token_hp_percent <= 49) {
obj.set("status_brown",true);
} else if (token_hp_percent >= 1 && token_hp_percent <= 24) {
obj.set("status_red",true);
} else if (token_hp_percent <= 0 ) {
obj.set("status_dead",true);
}
} else {
resetStatus(obj);
}
}
},
resetStatus = function(obj) {
obj.set("status_dead",false);
obj.set("status_red",false);
obj.set("status_brown",false);
obj.set("status_yellow",false);
obj.set("status_green",false);
},
checkInstall = function()
{
var script_version = "0.4.0";
if( ! state.healthstat ) {
state.healthstat = {
version: script_version,
run_state: true,
};
}
if (state.healthstat.version != script_version)
state.healthstat.version = script_version;
log("-=> Health State Script v"+state.healthstat.version+" Initialized <=-")
},
registerEventHandlers = function() {
on('chat:message', handleInput);
on('change:graphic', handleToken);
};
return {
CheckInstall: checkInstall,
RegisterEventHandlers: registerEventHandlers
};
}());
on("ready", function() {
'use strict';
healthstat.CheckInstall();
healthstat.RegisterEventHandlers();
});