We use Cookies to help personalize and improve Roll20. For more information on our use of non-essential Cookies, visit our Privacy Policy here.
Accept
Advertisement Create a free account

Get tokens assigned to character

1491327923
DXWarlock
Pro
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.
1491328294
Do a findObjs and compare .get("represents") to character.id ?
1491328615

Edited 1491328639
DXWarlock
Pro
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
Forum Champion
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
Pro
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.
1491328933
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
Pro
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
Pro
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
Forum Champion
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
Forum Champion
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
Brian
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
Forum Champion
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
Forum Champion
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
Forum Champion
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?