Skip to content

Instantly share code, notes, and snippets.

@sturtus
Last active September 14, 2018 08:38
Show Gist options
  • Save sturtus/8352577 to your computer and use it in GitHub Desktop.
Save sturtus/8352577 to your computer and use it in GitHub Desktop.
Roll20 Library
/*
This is my collection of useful functions to use in other commands/functions. I will keep this top post updated with the list of included functions as I add more.
Library includes:
- removePlus. Takes string or array of strings and returns the integer after the symbol "+". Useful for getting an integer from a string that represents a bonus like +2. Will also convert any numbers in the string/array of strings to integers.
- getAttributeObjects: takes a character object and an array of attribute names, checks the names against the character for existing attributes, error checks them for empty values or existence at all, and returns an array of the attributes' objects.
- getCharacterObj: takes a graphic, attribute, ability, or character object and returns the associated character object.
*/
function removePlus(array) {
// takes any array of strings or string and removes the + to return the integer after it
// useful when attribute values +2 and you only need the integer associated.
if (!(array instanceof Array)) {
array = array.split();
};
for (var i = 0; i < array.length; i++) {
if (array[i].indexOf("+") !== -1) {
var p = array[i].indexOf("+")+1;
array[i] = parseInt(array[i].slice(p));
};
};
return array;
};
//---------------------------------------------------------------------------------------------------------------------------------------------
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("", "/desc 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("", "/desc " + 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("", "/desc cannot be associated with a character.");
log("getCharacterObj: object 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("", "/desc Selected token does not represent a character.");
return false;
};
};
if (objType == "character") {
var characterObj = getObj("character", obj._id);
}
return characterObj;
};
//---------------------------------------------------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment