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] modified "lightswitch" only affects one lightsource

Currently, I am trying to modify another script to set the light values of a token and it works great with the only exception being that it only works on the first token it encounters. I would like it to affect all tokens with the same name, though I cannot figure out how. Any help would be appreciated... var lights = function(radius,dim) { var light = findObjs({ _type: 'graphic', name: "Torch" })[0]; light.set("light_radius", radius); light.set("light_dimradius", dim); }; on("chat:message", function(msg) { if(msg.type == "api" && msg.content.indexOf("!lightson") !== -1) { lights(30,20);};}); on("chat:message", function(msg) { if(msg.type == "api" && msg.content.indexOf("!lightsoff") !== -1) { lights(0,0);};});
1404592948
The Aaron
Roll20 Production Team
API Scripter
Something like this should work: var lights = function(radius,dim) { _.each(findObjs({ _type: 'graphic', name: "Torch" }), function(light){ light.set("light_radius", radius); light.set("light_dimradius", dim); }); }; on("chat:message", function(msg) { if(msg.type == "api" && msg.content.indexOf("!lightson") !== -1) { lights(30,20);};}); on("chat:message", function(msg) { if(msg.type == "api" && msg.content.indexOf("!lightsoff") !== -1) { lights(0,0);};}); That [0] in your original code is taking the first of the matching objects, so you only get one. The Underscore library provides some very nice shorthand; I'm using the each() method in that sample to iterate over all the objects returned from findObjs() and call the callback function with them as an argument. Hope that helps!
Aaron said: Something like this should work: var lights = function(radius,dim) { _.each(findObjs({ _type: 'graphic', name: "Torch" }), function(light){ light.set("light_radius", radius); light.set("light_dimradius", dim); }); }; on("chat:message", function(msg) { if(msg.type == "api" && msg.content.indexOf("!lightson") !== -1) { lights(30,20);};}); on("chat:message", function(msg) { if(msg.type == "api" && msg.content.indexOf("!lightsoff") !== -1) { lights(0,0);};}); That [0] in your original code is taking the first of the matching objects, so you only get one. The Underscore library provides some very nice shorthand; I'm using the each() method in that sample to iterate over all the objects returned from findObjs() and call the callback function with them as an argument. Hope that helps! Thank you very much for that. That is exactly what I was looking for. I wasn't aware of the underscore library or the each() functionality. Glad to be learning more :)
1404608985
The Aaron
Roll20 Production Team
API Scripter
No problem! The underscore library is pretty useful/time saving.
Alright, I've got a similar script for one of my player's characters (he basically acts like the party's torch), but since he couldn't make it to this last session, the group was left with their own items to provide enough light for them to see by. Is there any way to do a script that puts a light radius and dim radius on a selected token with a command? And then does the same but removing it to the default I have setup (1 light radius, 0 dim radius) with a different command? I basically want to be able to select a token, and give them a torch's brightness, and then be able to turn it off after the torch has been used up.
1405747554
The Aaron
Roll20 Production Team
API Scripter
Yeah, I could do that... To give the currently selected tokens a light radius of 40, with dim starting at 20: !torch or light radius of 30 with a dim starting at 15: !torch 30 or light radius of 60 with a dim starting at 10: !torch 60 10 Snuff the light with: !snuff Enjoy! <a href="https://gist.github.com/shdwjk/342cb67457936702fd8" rel="nofollow">https://gist.github.com/shdwjk/342cb67457936702fd8</a>...
Aaron, you are awesome. :)
1405779422
The Aaron
Roll20 Production Team
API Scripter
I have to say, I think I like writing scripts more than I like playing... Hope that doesn't reflect badly on the groups I play with! :)
Oh man, thanks Aaron, that is amazing!
1405780074
The Aaron
Roll20 Production Team
API Scripter
/em beams! :)