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

Grabbing an array of all characters for control of a token

I am a newbie to the roll20 API and I am trying to learn more. I have a little programing experience but would not consider myself to be an expert. To teach myself what's going on in the API I have been building a Torch handling script to allow me to light, dim, snuff and drop torches and have this automatically adjust the lighting on the player characters. When I run the "Drop torch" script the code creates a new token and gives control as follows    var player = character.get("controlledby"); createObj("graphic",{....,  controlledby:player }); What I want to do is create an array of all of the characters in the game and pass it to the controlled by. I thought this would work var playerChars = findObjs({type: 'character'}); but the API doesn't like it when I pass it to the controlledby property. I feel like this must be trivial but I cannot find the answer on the forums (I am probably not putting in the right search terms) or google.
1713592185
The Aaron
Roll20 Production Team
API Scripter
You're on the right track, there's just a few things to refine. First, controlledby is a list of player ids, not character ids, so you'd want to search for all the players and get their ids. However, you actually don't need them in this case, you can just use the special value "all". createObj("graphic",{....,  controlledby:"all" }); Second, findObjs() returns an array of objects, but what you need is an a comma delimited list of player ids stored in a string. To get that, you could do something akin to: let playerlist = findObjs({type:"player"}).map(p=>p.id).join(","); The .map() function on Array will call a given function once for each item in the array, and return an array of the results. The function here: p=>p.id Takes a player object as p, and returns its id. It's shorthand for: function(p){return p.id;} Then the .join() function on Array just makes a string of all the values with the argument (",") between each element.