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

Get tokens assigned to character

1491327923
DXWarlock
Sheet Author
API Scripter
I've dug around the API wiki, but cant see any 'clean' way to get the tokens assigned to a character. What Im essentially trying to do: On attribute change: find the character it was changed on, then find the tokens assigned to that character as an obj. I got the attrib trigger, and can find the character it happened on, but stumped how to find what token is for that character.
Do a findObjs and compare .get("represents") to character.id ?
1491328615

Edited 1491328639
DXWarlock
Sheet Author
API Scripter
Wouldn't I need to loop thru all the tokens on all the pages that way? What I thought of (sort of), but seemed overly 'messy'.
1491328623

Edited 1491328685
The Aaron
Pro
API Scripter
let characterid = /* wherever you get it */, tokens = _.chain(findObjs({type:graphic})) .filter((o)=>o.get('represents')===characterid) .value(); DXWarlock said: Wouldn't I need to loop thru all the tokens on all the pages that way? Yes, but it's actually quite fast at this point.
1491328899
DXWarlock
Sheet Author
API Scripter
Ah ok Ill do that. Since I had your on attrib change watch, going to add that to update tokens, way better than a !command to do it.
Really wish I could get my head wrapped around all the dot-functions like chain and filter. About the only one I can reliably remember is _.each
1491329808
DXWarlock
Sheet Author
API Scripter
Same here sky. The Aaron is always showing off taking 40 lines of code I have a question about and reducing it to 5 lines of dot and underscore code..lol
1491330353
Jakob
Sheet Author
API Scripter
Funny thing, in this case, chaining is superflous, probably born from copy-paste? findObjs({type:graphic}).filter((o)=>o.get('represents')===characterid); does the same thing.
1491331908
The Aaron
Pro
API Scripter
Jakob said: Funny thing, in this case, chaining is superflous, probably born from copy-paste? findObjs({type:graphic}).filter((o)=>o.get('represents')===characterid); does the same thing. Nah, just doing it a different way.  I often chain objects as well as arrays and _.filter() supports either, whereas Object doesn't have a native filter() function, so I'd have to think about the return type.  Of course, in this case It doesn't make any difference, as you pointed out. =D
1491332304
The Aaron
Pro
API Scripter
Sky said: Really wish I could get my head wrapped around all the dot-functions like chain and filter. About the only one I can reliably remember is _.each That chain syntax is just syntactic sugar around passing the output of the previous function as the input to the next. let tokensByFirstLetter = _.chain(msg.selected) .map((o)=>getObj('graphic',o._id)) .filter(_.isUndefined) .reduce((m,o)=>{ let l=(o.get('name')[0]||'').toLowerCase(); m[l]=m[l]||[]; m.push(o); return m; },{}) .value(); Is the same as: let tokens = _.map(msg.selected, (o)=>getObj('graphic',o._id)); let tokensNoUndefined = _.filter(tokens,_.isUndefined); let tokensByFirstLetter = _.reduce(tokensNoUndefined, (m,o)=>{ let l=(o.get('name')[0]||'').toLowerCase(); m[l]=m[l]||[]; m.push(o); return m; },{}); without all the intervening data structures.
1491338082
Lithl
Pro
Sheet Author
API Scripter
The Aaron said: let characterid = /* wherever you get it */, tokens = _.chain(findObjs({type:graphic})) .filter((o)=>o.get('represents')===characterid) .value(); You should also be able to do: tokens = findObjs({type: 'graphic', represents: characterid}); Although I admit I don't know how the performance compares these days.
1491388760
The Aaron
Pro
API Scripter
Good point Brian!  For some reason I kept thinking that would require filterObjs(). In my head, I guess I was thinking "multiple character ids". 
Brian said: You should also be able to do: tokens = findObjs({type: 'graphic', represents: characterid}); Although I admit I don't know how the performance compares these days. I use the same find except I add "subtype: 'token'". Not sure if that speeds up much though.
1491408785
The Aaron
Pro
API Scripter
Yeah, I considered subtype, but since you could have a graphic represent a character and not be a token, I didn't figure it was an important distinction to make.  I can't think of any example of wanting to do that (I guess if you had your monsters all set up as cards in a deck and did the represents with a script on add, you could end up in that situation), but didn't want to exclude the possibility.
is there a macro that could come out of this for the rest of us?
1494359583
The Aaron
Pro
API Scripter
GameT1me said: is there a macro that could come out of this for the rest of us? What do you want that macro to do?