Update: i figured out you need to use .get for attributes. So, it is working, almooooost. In the blow code, I first create a HPbar attribute and set its current and max values to 24, then verify they exist. Then in the token creation code, I set the bar1_link to that stat. When I do that, the HPbar stat is erased, and so the token has the link set properly, but the stats are empty. If I run the code without all the bar1 statements (bar1_link, bar1_value, bar1)max), the token is created, and on the character sheet, the HPbar stat is set properly. So according to the documentation, setting bar1_link is supposed to link the token, and update the attributes properly. the bar1_value and max lines are not supposed to be necessary. But if I run the code without them, the stat the bar is linked to gets emptied. Am I still doing somethign wrong? function assignAttribute(stat,value, character, max) {
//return; // temp
if (value === undefined )
{
log(stat + " has returned an undefined value.");
sendChat("Error on " + stat + " attribute", "This attribute has been ignored.");
} else if (max) {
createObj("attribute", {
name: stat,
current: value,
max: value,
characterid: character
});
} else {
createObj("attribute", {
name: stat,
current: value,
characterid: character
});
//use the line below for diagnostics!
//log(stat + ", " + value);
}
};
on("ready", function() {
on("chat:message", function (msg) {
if (msg.type === "api" && msg.content.split(" ")[0] === "!token-test")
{
log("=====================================");
var gridSize = 70;
var currentPageID = Campaign().get('playerpageid'),
currentPage = getObj('page', currentPageID);
var charName = "NewNPC Test";
let imgs=[
'<a href="https://s3.amazonaws.com/files.d20.io/images/3237299/dT7cOYw8rJMU0cXVQFqJwA/thumb.gif?1393455438" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237299/dT7cOYw8rJMU0cXVQFqJwA/thumb.gif?1393455438</a>',
'<a href="https://s3.amazonaws.com/files.d20.io/images/3237296/rP0Dso1PAU1DjpL3uAI7Mg/thumb.gif?1393455436" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237296/rP0Dso1PAU1DjpL3uAI7Mg/thumb.gif?1393455436</a>',
'<a href="https://s3.amazonaws.com/files.d20.io/images/3237293/Xk2rXRp50UEXfSf9FW_gLQ/thumb.gif?1393455435" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/3237293/Xk2rXRp50UEXfSf9FW_gLQ/thumb.gif?1393455435</a>'
];
let character = createObj('character', {
name: charName,
avatar: imgs[0],
}
);
/* ======================================================================
The following lines initialise the attribute to use in healthbar.
*/
assignAttribute('HPbar',24,character.id,true);
var HPbar = parseInt(getAttrByName(character.id, 'HPbar', 'max'))||0;
log("HPbar: " + HPbar);
var barHealth = findObjs({ type: 'attribute', name: 'HPbar', characterid: character.id})[0];
log("barHealth: " + barHealth.get('name'));
log("barHealth: " + barHealth.id);
// At this point, we have an attribute, with a current and max value of 24.
let token = createObj('graphic',{
sides: imgs.map(encodeURIComponent).join('|'),
imgsrc: imgs[0],
currentSide: 0,
/* other initialization properties here */
_pageid: currentPageID,
left: gridSize + gridSize/2,
top: gridSize + gridSize/2,
rotation: 0,
width: gridSize,
height: gridSize,
layer: "objects",
// character stuff
represents:character.id,
name: charName,
showname:true,
bar1_link: barHealth.id,
/* ===============================================================
The above line erases the barHealth attributes values, setting them to undefined.
Following two lines should not be necessary according to documentation
But without them, the above line erases the HPbar stat.
================================================================ */
bar1_value: HPbar,
bar1_max: HPbar,
});
if(token) {
/* Token is valid, do something with it */
setDefaultTokenForCharacter(character,token);
sendChat("Token Test", "/w gm Token Created");
} else {
sendChat("Token Test", "/w gm Token Is Invalid");
log("Token is Invalid");
}
}
});
});