Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Change a Character's Attribute

Is there a way to set a Character's Attribute to a specific value with a macro? 
Hey Jon. This is a question asked frequently and to be honest there is no easy answer! If your looking for something where you input the value then I use this. Stick the following in your API scripts. (Copy everything in bold from the word function until you see the line that says "dont copy this") ___________________________________________ function getAttributeObjects(characterObj,attributeArray) { // can pass array of attribute strings or a single attribute stringalong 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); }); }; }); _____________________Dont Copy this!!___________ After you've activated that, go back into your game and you can do this a few ways. You need to name your attribute, then name your new value. Stick the following in your chat box. !attrib DEX|4  So this will change your DEX attribute to 4 - Then if you want to go further to make it easier for you create a macro like this: !attrib ?{Attribute}|?{New Value} Add it to your button bar at the bottom, and when you click it - it will ask for your attribute you want to change; and then what you want your new value to be. Hope this helps.
Enzo S. said: Hey Jon. This is a question asked frequently and to be honest there is no easy answer! If your looking for something where you input the value then I use this. Stick the following in your API scripts. (Copy everything in bold from the word function until you see the line that says "dont copy this") ___________________________________________ function getAttributeObjects(characterObj,attributeArray) { // can pass array of attribute strings or a single attribute stringalong 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); }); }; }); _____________________Dont Copy this!!___________ After you've activated that, go back into your game and you can do this a few ways. You need to name your attribute, then name your new value. Stick the following in your chat box. !attrib DEX|4  So this will change your DEX attribute to 4 - Then if you want to go further to make it easier for you create a macro like this: !attrib ?{Attribute}|?{New Value} Add it to your button bar at the bottom, and when you click it - it will ask for your attribute you want to change; and then what you want your new value to be. Hope this helps. Enzo,    Thank you. That looks a bit more complicated then I hoped, was hoping for something non-API, but I'll take it :)   Thanks alot.
It's a Script and forget, no maintenance required - You can't do it without going into the API, but this will get you off the ground! It also announces as a Desc, so no one boost their HP without you knowing. (that is if you allow players to use it)