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

[Help] Add Attribute to all Characters in a Folder

1442673898
Pat
Pro
API Scripter
Does anyone have a code block that will loop through all the characters in a folder, add an attribute, and set it to a value? Now that I have 100+ monsters set up in the journal, I find that I want to add a new attribute to all of them to improve my macros or fix an oversight.  Today, I have to drag a token onto the map and run the "!setattr" script.  That means I wind up with a population of tokens that are up to date an others that I will stumble onto during a game and have to upgrade when my macros suddenly fail. Here's a visual example of what I'm trying to do.  
1442680306

Edited 1442680400
Lithl
Pro
Sheet Author
API Scripter
Here's what I threw together in a few minutes: on('chat:message', function(msg) {     var folderdata = JSON.parse(Campaign().get('journalfolder')),         charactersInFolder = _.chain(findNestedFolder(folderdata, 'Characters').i)             .filter(function(obj) { return _.isString(obj); })             .map(function(id) { return getObj('character', id); })             .reject(function(char) { return !char; })             .value();     log(charactersInFolder); }); function findNestedFolder(folderData, name) {     var targetFolder;          _.each(folderData, function(obj) {         if (!_.isObject(obj) || targetFolder) return;         if (obj.n.toLowerCase() === name.toLowerCase()) {             targetFolder = obj;             return;         }         targetFolder = findNestedFolder(obj.i, name);     });     return targetFolder; } From this folder structure: The result is: [ {"name":"Drulyut","bio":"","gmnotes":"","_defaulttoken":"","archived":false,"inplayerjournals":"","controlledby":"","_id":"-JTJFofS76c1Xx5ZMHK9","_type":"character","avatar":""}, {"name":"Panther","bio":"","gmnotes":"","_defaulttoken":"","archived":false,"inplayerjournals":"","controlledby":"-J19JXG9_NPQb1y0UIhP","_id":"-J5EQW26czvL3bdTKCWp","_type":"character","avatar":""} ] As the call to findNestedFolder specified the 'Characters' folder. If you want to include characters in subfolders (eg, ask for the 'Characters' folder and get everything in 'Test Nested' as well), that's just a change to the value of charactersInFolder. Let me know if you need that. In order to create an attribute on each of those characters, simply iterate over the charactersInFolder array and create an attribute for each: _.each(charactersInFolder, function(char) { createObj("attribute", { name: "Strength", current: 0, max: 30, characterid: char.id }); });
1442783360
Pat
Pro
API Scripter
Thanks Brian!