Skip to content

Instantly share code, notes, and snippets.

@bplus
Forked from sturtus/setAttribute
Created March 31, 2020 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bplus/4d4482f6df997d20a941b2bde4749b8e to your computer and use it in GitHub Desktop.
Save bplus/4d4482f6df997d20a941b2bde4749b8e to your computer and use it in GitHub Desktop.
Roll20 API script to change an attribute's value to the defined string.
function getAttributeObjects(characterObj,attributeArray) {
// can pass array of attribute strings or a single attribute string along with an associated character
// returns those attributes as an object array or returns false if they do not exist on the passed character.
// get the passed attribute name array from the character object and test if they are defined
if (characterObj != undefined ) {
var attributeObjArray = new Array();
if (!(attributeArray instanceof Array)) {
attributeArray = attributeArray.split();
};
for (var i = 0; i < attributeArray.length; i++) {
attributeObjArray[i] = findObjs({_type: "attribute", name: attributeArray[i], _characterid: characterObj.id})[0];
if (attributeObjArray[i] === undefined) {
sendChat("API","Selected character requires attribute: " + attributeArray[i] + " ");
};
};
};
if (attributeObjArray.indexOf(undefined) !== -1) return false;
//loop through attributeArray and names of attributes to make sure they all match and get their values if they are valid.
//make sure none of the values are empty
var attributeValue = new Array();
var j = 0;
for (var i = 0; i < attributeArray.length; i++) {
attributeValue[i] = attributeObjArray[i].get("current");
if (attributeValue[i] === "") {
sendChat("API"," " + attributeArray[i] + " is empty.");
j++;
};
};
if (j !== 0) return false;
return attributeObjArray;
};
//---------------------------------------------------------------------------------------------------------------------------------------------
function getCharacterObj(obj) {
//send any object and returns the associated character object
//returns character object for attribute, token/graphic, and ability, and... character
var objType = obj._type;
if ((objType != "attribute") && (objType != "graphic") && (objType != "character")) {
sendChat("API"," cannot be associated with a character.");
return false;
}
if ((objType === "attribute") || (objType === "ability")) {
var att = getObj(objType, obj._id);
if (att.get("_characterid") != "") {
var characterObj = getObj("character", att.get("_characterid"));
};
};
if (objType === "graphic") {
var tok = getObj("graphic", obj._id);
if (tok.get("represents") != "") {
var characterObj = getObj("character", tok.get("represents"));
} else {
sendChat("API"," Selected token does not represent a character.");
return false;
};
};
if (objType === "character") {
var characterObj = getObj("character", obj._id);
}
return characterObj;
};
//---------------------------------------------------------------------------------------------------------------------------------------------
function attrib(characterObj,attributeObjArray,newValue) {
var attributeName = attributeObjArray[0].get("name");
var attributeValue = attributeObjArray[0].get("current");
var characterName = characterObj.get("name");
// change character attribute
attributeObjArray[0].set("current", newValue);
//output
sendChat("", "/desc " + characterName + " has changed " + attributeName + " from " + attributeValue + " to " + newValue + ".");
};
on("chat:message", function(msg) {
if (msg.type == "api" && msg.content.indexOf("!attrib ") !== -1) {
//parse the input into two variables, attribute and newValue
var selected = msg.selected;
var Parameters = msg.content.split("!attrib ")[1];
var attributeName = Parameters.split("|")[0];
var newValue = Parameters.split("|")[1];
if(!selected) {
sendChat("", "/desc Select token and try again.");
return; //quit if nothing selected
};
//loop through selected tokens
_.each(selected, function(obj) {
var characterObj = getCharacterObj(obj);
if (characterObj == false) return;
var attributeObjArray = getAttributeObjects(characterObj, attributeName);
if (attributeObjArray == false) return;
attrib(characterObj,attributeObjArray,newValue);
});
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment