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

Cycling through selected tokens?

Hi all (Aaron XD) I'm looking at the GroupInitiative script to try and understand how to cycle through each selected token doing stuff. But not quite there yet. Gonna do a tuturial on underscore.js before continuing
1446379499
The Aaron
Pro
API Scripter
Torch is probably easier to follow. Look for msg.selected. 
If you do a log (msg.selected); and then copy paste that into a javascript beautifier or something... it will expand it into a much more readable format.
1446383881
The Aaron
Pro
API Scripter
Here's an annotated way of doing it: _.chain(msg.selected) // Start a chain of the selected objects .map(function(s){ return getObj('graphic',o._id); // try to get each as a graphic (will be undefined for drawings, text, etc }) .reject(_.isUndefined) // Remove those selected objects that were not graphics (drawings, text, etc) .each(function(t){ // Do something with the token t log( 'name: '+t.get('name'); });
1446400622

Edited 1446404465
WurmD
KS Backer
Okaaay :D, wizards! Wizards all! -- Ok, almost. Not there yet   case '!selected-money': m_obj = _.chain(msg.selected) // Start a chain of the selected objects     .map(function(s){         return getObj('graphic',s._id); // try to get each as a graphic (will be undefined for drawings, text, etc     })     .reject(_.isUndefined) // Remove those selected objects that were not graphics (drawings, text, etc)     .each(function(t,m){         m.maintenance += t.get('bar1_value');         log(' ' + t + ' ' + m + ' ' + t.get('bar1_value') + ' ' + m.maintenance);         return m;     },{ maintenance: 0}); sendChat('','Maintenance total of selected units: ' + m_obj.maintenance); break; gives For reference, the error message generated was: /home/symbly/www/d20-api-server/node_modules/firebase/lib/firebase-node.js:1 orts, require, module, __filename, __dirname) { function g(a){throw a;}var j=v ^ ReferenceError: m_obj is not defined at handleInput (evalmachine.<anonymous>:195:23) at eval (
1446409109
The Aaron
Pro
API Scripter
With "use strict", you must declare variables before using them: var m_obj = ... Also, for what you want, you'll need to switch the .each() to a .reduce(), put the arguments as (m,t), then add a .value() after the .reduce(). 
1446715210

Edited 1446715278
WurmD
KS Backer
yeah, this could be done with .each(), but .reduce() is perfect for this Now, the need for forcing the variables to "be" numbers (with parseFloat() or by multiplying each with *1) so that they are summed instead of concatenated was quite a bitch to figure out case '!selected-money': var m_obj = _.chain(msg.selected) // Start a chain of the selected objects .map(function(s){ return getObj('graphic',s._id); // try to get each as a graphic (will be undefined for drawings, text, etc }) .reject(_.isUndefined) // Remove those selected objects that were not graphics (drawings, text, etc) .reduce(function(m,t){ return parseFloat(m) + parseFloat(t.get('bar1_value')); },0) // initializing the reduce's value to 0 .value(); sendChat('','Maintenance total of selected units: ' + ' ' + m_obj); break;