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

Tally tokens on the map

Hello everyone, Is there a macro out there that could count up and provide total numbers of tokens by name? This is for a board game that uses multiple tokens that need to be tallied each turn for a census, which is a bit of a pain.  Thanks for any suggestions!
1595429416
The Aaron
Roll20 Production Team
API Scripter
It would be trivial with an API script. Would that be an option?
It would be trivial for a person smarter than me! :) I haven't coded a macro before. If you have any tips or examples, I'm all ears!
1595434568

Edited 1595442609
The Aaron
Roll20 Production Team
API Scripter
Hahaha, sorry, I didn't mean to imply you'd need to write it.  How's this: Command: !tally-tokens Script: on('ready',()=>{ const getPageForPlayer = (playerid) => { let player = getObj('player',playerid); if(playerIsGM(playerid)){ return player.get('lastpage') || Campaign().get('playerpageid'); } let psp = Campaign().get('playerspecificpages'); if(psp[playerid]){ return psp[playerid]; } return Campaign().get('playerpageid'); }; const css = (o)=>Object.keys(o).reduce((m,k)=>`${m}${k}:${o[k]};`,''); const s = { box: css({ ["font-weight"] : "bold", ["border-bottom"] : "2px solid #0F3DA0", ["border-top"] : "4px solid #0F3DA0", ["background-color"] : "#AEB6C6" }), heading: css({ ["font-weight"] : "bold", ["font-size"] : "1.3em" }), title: css({ ["font-weight"] : "bold" }), row: css({ ["margin"] : ".1em", ["border-bottom"] : "1px solid #0F3DA0" }), num: css({ ["display"] : "inline-block", ["float"] : "right", ["font-size"] : "1.3em", ["padding"] : ".1em" }), img: css({ ["max-width"] : "2em", ["max-height"] : "2em", ["float"] : "left", ["border"] : "1px solid #999999", ["border-radius"] : ".25em", ["background-color"] : "white" }), clear: css({ ["clear"] : "both" }) }; const f = { content: (t,c) => `<div style="${s.content}">${f.heading(t)}${c}</div>`, heading: (t) => `<div style="${s.heading}">${t}</div>`, box: (t) => `<div style="${s.box}">${t}</div>`, img: (url) => `<img style="${s.img}" src="${url}">`, title: (t) => `<div style="${s.title}">${t}</div>`, clear: () => `<div style="${s.clear}"></div>`, num: (n) => `<div style="${s.num}">${n}</div>`, row: (...o) => `<div style="${s.row}">${o.join(' ')}${f.clear()}</div>` }; on('chat:message',msg=>{ if('api'===msg.type && /^!tally-tokens(\b\s|$)/i.test(msg.content)){ let pageid = getPageForPlayer(msg.playerid); let data =findObjs({ type: "graphic", pageid: pageid, layer: "objects" }).reduce( (m,t)=>{ let n = t.get('name'); if(!m.hasOwnProperty(n)){ m[n]={ name: n, img: t.get('imgsrc'), count: 0 }; } m[n].count++; return m; },{}); sendChat('',f.content("Token Tallies",f.box( Object.values(data).map(d=>f.row(f.img(d.img),f.num(d.count),f.title(d.name))).join('') ))); } }); });
I am in awe of this community. thank you so much! I am going to give this a whirl!
1595438811

Edited 1595438849
Thank you again! I am having a slight problem with the code. Just in case you have time to help troubleshoot: I was able to add the script as a new script, save it, and it registered. The output console says it started up and is running properly. In the game I created a macro that calls for  !tally-tokens .  When I run it, the text "Token Tallies" appears in the chat but there aren't any values showing up under it. (I do have tokens on the map.) I'm happy to add you to the game if you have interest to look from the inside. Either way, thanks for your help so far!
Oddly, it works in my other games. This one isn't using a predefined character sheet. I wonder if that is the issue...
1595441234
The Aaron
Roll20 Production Team
API Scripter
Sure, PM me an invite!
1595441342
The Aaron
Roll20 Production Team
API Scripter
It looks for graphic objects on the page the player that ran the command is on, and which are on the objects layer. If there aren't any, it would just be the title. Are they on the wrong layer?
Just PMed the invite link :) I have a few tokens on the object layer, and only one page at the moment (just getting started). Thanks again!
The Aaron is Da Boss. Thanks so much for straightening this out! This is extremely helpful.
1595442673
The Aaron
Roll20 Production Team
API Scripter
No problem!  I updated the code above with the correction (For posterity: lastpage is only set for GMs when they change pages, so if you're in a new game with only one map, it won't be set.). Happy Rolling!
Again, thanks so much for this! I completed that game conversion and it works great. The tally was a huge help. I’m onto my next conversion project and was wondering if you might consider adapting this tally for a different kind of token count. No pressure on this, I was just wondering! In this game the players need to add up the income values of all the provinces they control. I have created a draft where each province is token on the map. These are a kind of character and have attributes the GM can change the values of. One of these is PLAYER (the player color) and another is ECONOMIC (the income). If it wasn’t too much trouble, I was wondering if you could adapt the script to calculate the total economic value by player. In the example below, the red player controls the bottom four provinces (worth 8 economic value, the one on the right of each pair) and the green player controls 0. If these were the only provinces they controlled, the income tally would look like: Red 8 Green 0 Etc. No pressure of course, I just thought I would ask! Thanks either way 8)
1596649953
The Aaron
Roll20 Production Team
API Scripter
That should be pretty easy.