
Bit of a newb here, but been fiddling with an api script to update status markers on a token if the linked character's attributes change. With the sheet I've been creating the attributes in question are all checkboxes which return "0" if unchecked or "on" or a value (like "4") if checked. Basic code has been working in many cases and runs something like this: on("change:attribute", function(attr) { var aName = attr.get("name"); var aCurrent = attr.get("current"); var charid = attr.get("_characterid"); // get MATCHING token(s) on current map var tokens = findObjs({ _pageid: Campaign().get("playerpageid"), _type: "graphic", represents: charid, }); // for each token for (i = 0; i < tokens.length; i++) { // case switch based on which attr was updated switch (aName) { // examples -- sometimes using extended if / else for certain attribs, but most work with the ternary operator case "wounded": if (aCurrent == "0") { tokens[i].set("status_red", false); } else { tokens[i].set("status_red", true); } break; case "shaken": aCurrent == "on" ? tokens[i].set("status_green", true) : tokens[i].set("status_green", false); break; default : break; } } }); This all seems to work fine if I've opened that character and toggled through the respective attribute buttons. But if I add a brand new token (linked to a character sheet) onto the map and go toggle an attribute I get nothing until I toggle that attribute again. I tried writing some kind of "initialize" function to see if setting all the attributes to non-zero and then setting them back to zero would do something, but it hasn't changed the behavior. I'm aware that this may already be done in another script, but (a) I like learning by doing and (b) all the _chains and _maps ya'll seem to like make my head spin, so trying to do it the simple way for now. Not sure what I'm missing, any tips appreciated. Thank you!