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 .
×

The new D&D 2024 sheet is now available!

Create a free account

Any scripts that number already placed tokens?

Running a module, and most of the tokens already placed aren't numbered. I just got pro so i can use api scripts, and i know there is a script that allows you to place tokens and have them be numbered, but i was wondering if i could number already placed tokens in.
1592794669

Edited 1593273183
The Aaron
Roll20 Production Team
API Scripter
Here's a script that will do it.  Just run: !number-npcs It will append a number to the end of each NPC on a page where there are more than one representing a given character.  NPC is determined as: A Token Represents a Character The Character is not controlled by anyone. Code: on('ready',() => { const getPageForPlayer = (playerid) => { let player = getObj('player',playerid); if(playerIsGM(playerid)){ return player.get('lastpage'); } let psp = Campaign().get('playerspecificpages'); if(psp[playerid]){ return psp[playerid]; } return Campaign().get('playerpageid'); }; on('chat:message', msg=>{ if('api'===msg.type && /^!number-npcs(\b\s|$)/i.test(msg.content) && playerIsGM(msg.playerid)){ let who = (getObj('player',msg.playerid)||{get:()=>'API'}).get('_displayname'); sendChat('',`/w "${who}" Renumbering all NPCs...`); let count = 0; let args = msg.content.split(/\s+--/).map(s=>s.toLowerCase()); let props = { type:'graphic' }; if(args.includes('current-page')){ props.pageid = getPageForPlayer(msg.playerid); } let tokenData = findObjs(props) .map((o)=>({ token:o, character:getObj('character',o.get('represents')) })) .filter(o=>undefined!==o.character) .filter(o=>''===o.character.get('controlledby')) .reduce((m,o)=>{ let pageid=o.token.get('pageid'); m[pageid]=m[pageid]||{}; m[pageid][o.character.id]=m[pageid][o.character.id]||[]; m[pageid][o.character.id].push(o); return m; },{}) ; let pageIDs = Object.keys(tokenData); const burndown1 = () =>{ let pid = pageIDs.shift(); if(pid){ let pageData = tokenData[pid]; let charIDs = Object.keys(pageData); const burndown2 = ()=>{ let charID = charIDs.shift(); if(charID){ let n = 0; let charData = pageData[charID]; if(charData.length>1){ charData.forEach(o=>o.token.set({ name: `${o.character.get('name')} ${++n}`})); count += charData.length; } setTimeout(burndown2,0); } else { setTimeout(burndown1,0); } }; setTimeout(burndown2,0); } else { sendChat('',`/w "${who}" Renumbered <code>${count}</code> NPCs.`); } }; setTimeout(burndown1,0); } }); }); Edit: Updated with an option to only renumber the current page: !number-npcs --current-page
1592801207
vÍnce
Pro
Sheet Author
Cool idea. I think this might be easier to use than TokenNameNumber ...
Awesome! Thanks
1592804293
The Aaron
Roll20 Production Team
API Scripter
Vince: It's definitely easier from the existing content standpoint. If you're dragging things in, TokenNameNumber is probably better. I keep meaning to roll this functionality into it.  No prob, Dylan! =D
How would I need to edit this script so that it only applies to the current page?
1593240689
The Aaron
Roll20 Production Team
API Scripter
You could grab the page ID for the caller and pass it to the findObjs() call. I can make that change for you tomorrow if you want to send me a PM reminder. 
1593273218
The Aaron
Roll20 Production Team
API Scripter
Ok, I updated the above script to take an argument to only renumber entries on the current page: !number-npcs --current-page
How about to only number selected tokens?
1593434060
The Aaron
Roll20 Production Team
API Scripter
So, do you mean to number the selected token in the same sequence (goblin 1, kobold 2, horse 3), or number each of the different character types (goblin 1, goblin 2, kobold 1, horse 1)? And what if there are already numbered tokens on the board?
Hmm good point. I guess the second would be better, and it'd probably not know anything about the other tokens on the board (in which case i could have another room with goblin 1, 2, 3, but thats a different room and the dungeon im running is kinda massive, so they wouldn't end up in the same encounter so it wouldn't cause any issues if there were a second "goblin 1" somewhere else. Optionally it could also check if a token has already been numbered or has a number at the end of its name but thats not necessary for my needs.
1593439234
The Aaron
Roll20 Production Team
API Scripter
Would it be sufficient if it reordered all creatures of the types selected, or are you trying to get ones that are close together to have consecutive names?   I guess I should say "what problem are you trying to solve?
I guess my worry was that certain named NPC's or the PC's would get numbers added to their names, or if i ever mention the names of NPC's, id need to make sure not to mention the number else it would give away how many NPC's there actually are, but i guess its not too big an issue.
1593474863
The Aaron
Roll20 Production Team
API Scripter
If there is only one of an NPC, the above script will not number them.  That should account for unique NPCs, unless they have multiple copies for some reason.
1593476637

Edited 1593476688
vÍnce
Pro
Sheet Author
The Aaron said: Vince: It's definitely easier from the existing content standpoint. If you're dragging things in, TokenNameNumber is probably better. I keep meaning to roll this functionality into it.  No prob, Dylan! =D It would be nice to see this script's "auto-numbering" of existing tokens implemented into TokenNameNumber.  I like this script since I don't have to rename any of my monster/npc tokens to include "%%NUMBERD%%" because I'm lazy. ;-P  Maybe TNN could be modified so that as you paste a 2nd copy and/or additional copies of a token, the script auto numbers them without requiring "%%NUMBERD%%". Optional, of course. ;-)
1593477118
The Aaron
Roll20 Production Team
API Scripter
Vince, so pushy!!!  =D  Yeah, I should do some updates there...
1593477513
vÍnce
Pro
Sheet Author
The Aaron said: Vince, so pushy!!!  =D  Yeah, I should do some updates there... Pushy?  Me?   Looking forward to the update. ;-P
1593696910

Edited 1593697067
Just noticed a problem, when there are two tokens representing the same token, it can overwrite the name of some tokens with unique names. I ran it in the arcane chambers using  !number-npcs --current-page and i noticed that the beholder zombie had its name changed to "Nerozar the Defeated 2" because a second Beholder Zombie was named "Nerozar the Defeated" (now "Nerozar the Defeated 1"). This isnt as much of an issue, but then i noticed it also renamed Karn Kassifax "Wererat 9" because he was also a wererat. This is problematic, because if i ran it on level 3, the drow strongholds have a different name for every drow token, so it might overwrite all their names. Ideally if two tokens are the same character but also have the same name it would number them, but if their name is different and/or their character is different then you probably shouldn't number them. Or you could check if the token name is the same as the character the token represents, and then only number those tokens.
1593698507
The Aaron
Roll20 Production Team
API Scripter
That last option would probably be the easiest.  I can just strip the number off the end and do the check before renumbering.  I'll see about adding that in tonight and then go test it on that map.