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

[Script] TokenNameNumber -- Automatic Numbering of Qualifying Tokens

1413677976

Edited 1455065528
The Aaron
Pro
API Scripter
Update 0.5.5 &nbsp;-- Fixed copy/paste bug. (Thanks Three of Swords!) Update 0.5.3: Fixed a bug in 0.5.2 preventing naming. (Thanks Gozer, Vince, Laurent!) Update 0.5.2: Now much faster token renaming thanks to Stephen S. pointing out that although the on('add:graphic',...) event provides a malformed object, there is an immediately following on('change:graphic',...) which contains the fully formed object. I'm not queueing up the object id in the 'add', and when I get the next 'modify' for it, I do the rename. (Thanks Stephen S.!) Update 0.5.1: Added a help screen with the !tnn command, added GenConS configuration for Random Space, added multi-status setting for identification. Update v0.4 -- Added escaping of token names to prevent bugs if certain characters are present, such as parenthesis. (Thanks Kevin!) Update v0.3 -- Moved to Git, added a multi-stage renaming process to prevent missing a rename. Added Random Spacing (change randomSpace from 0 to some number to have the next tiles number be between 1 and randomSpace higher). (Thanks GenKitty!) Update v0.21 -- Fixed Gist Link in the top of the source. (Thanks Vince!) Update v0.2 -- Added in incrementing for Copy/Paste. I had to make a major change in the way tokens are recognized. I was using the token's imgsrc as a distinguishing factor, but as it turns out that can (and DOES) change when you copy/paste a token. That meant I couldn't detect existing tokens of the same "type" to set the number on the new one. Now, I'm basing identifying of existing tokens of the same "type" on the pattern of the name. This has 2 distinct benefits: 1. You can have tokens with different images that all represent the same type of creature, such as with a Rollable Table Token: 2. You can have whatever numbers you want in a name, and wherever you like, and it will still work: Instructions: Name your Token with %%NUMBERED%% where you want the number. You probably want to have Show nameplate? checked. Note that this the Token, not the Character. Only the Token's name is considered by this script.: Add that token to a character representing your monster as normal. You can use represents and all without issues. Note that the Character can be named anything you want. Drag in as many tokens as you like. Tokens will be numbered based on the highest numbered token with the same imgsrc on the same page. That means each page has its own monotonically increasing number sequence. I can see some great enhancements for this in the future... GIT: <a href="https://github.com/shdwjk/Roll20API/blob/master/To" rel="nofollow">https://github.com/shdwjk/Roll20API/blob/master/To</a>... Support my work on If you use my scripts, want to contribute, and have the spare bucks to do so , go right ahead. However, please don't feel like you must contribute just to use them! I'd much rather have happy Roll20 users armed with my scripts than people not using them out of some sense of shame. Use them and be happy, completely guilt-free! Disclaimer: This Patreon campaign is not affiliated with Roll20; as such, contributions are voluntary and Roll20 cannot provide support or refunds for contributions.
1413681578
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
That's handy!
1413682343
The Aaron
Roll20 Production Team
API Scripter
Yeah, I'm looking forward to hearing the suggestions others have for improvements. :)
Awesome.
1413685019
The Aaron
Pro
API Scripter
Thanks!
1413711449
Finderski
Pro
Sheet Author
Compendium Curator
Would that also work if I just copy/paste the token? I'm assuming not, because after I copy/paste, the %%NUMBERED%% would no longer be there. Is there a way to get it so it could still happen if I copy/pasted? Because I find that quicker and easier than dragging it all the time. But still very cool script.
I can see that working if the script searches out duplicate numbers on tokens as well as the %%NUMBERED%%, though I suspect it would be more robust to have it output duplicate numbers in some sort of bracket ( Kobold [ 1 ], Kobold [ 2 ], etc.) to prevent mistakes ( Don't want to break it with a character that has numbers in its name after all; 7 of 9, or r2d2, for some more famous examples). Of course, I don't know how much strain that puts on the api, though I would think that it was not terribly intensive, as it already searches the other tokens on the page on token creation? Not a very practiced coder here, so just guessing.
1413732534
The Aaron
Pro
API Scripter
I can make that work. =D I'll make a new version shortly.
1413776303
The Aaron
Pro
API Scripter
Update v0.2 -- Added in incrementing for Copy/Paste. I had to make a major change in the way tokens are recognized. I was using the token's imgsrc as a distinguishing factor, but as it turns out that can (and DOES) change when you copy/paste a token. That meant I couldn't detect existing tokens of the same "type" to set the number on the new one. Now, I'm basing identifying of existing tokens of the same "type" on the pattern of the name. This has 2 distinct benefits: 1. You can have tokens with different images that all represent the same type of creature, such as with a Rollable Table Token: 2. You can have whatever numbers you want in a name, and wherever you like, and it will still work:
Star Wars and Star Trek together!
1413776825
The Aaron
Pro
API Scripter
I was just speaking to Michael's point about numbers with that one (and I see that I polluted the reference with a bawdy substitution error... =D ).
1413793545
Finderski
Pro
Sheet Author
Compendium Curator
That is awesome. Now, I might have to migrate my game tot he Dev server so I can use this!
1413809180

Edited 1414183929
The Aaron
Pro
API Scripter
You can use this on the production servers. That's where I use it. :)
1414183274
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
Works like a charm.... very nice script and nice idea.
Weird... the imgsrc changes when you copy/paste? That seems wasteful programming to me. Have you brought this up with Riley?
1414183908
The Aaron
Pro
API Scripter
I haven't. It moved from being a ... max.jpg?##### to a ... thumb.jpg?##### I surmised it is an optimization based on the use case. I'm guessing that dragging from the journal it doesn't know the destination, but copy pasting the destination is explicit, and thus the size can be optimized. The way I was forced to change it was an improvement though, so I'm not complaining too much. (I was just being lazy! =D)
1414946204
The Aaron
Pro
API Scripter
Update v0.21 -- Fixed Gist Link in the top of the source. (Thanks Vince!)
Awesome Script....Great Job Aaron. I just tried this out and man will it save some time. Bobby
1419996035
The Aaron
Pro
API Scripter
Thanks! Glad to hear you'll be able to use it!
Aaron, Can you tell me why this script doesn't work every time? Here is what I am doing: I create a Character sheet and use the %%numbered%% and when i copy and then paste the tokens will work properly however. If I log out and go back in to the campaign and then drag a character / token onto the map, I cannot get the token name number to work at all. I named and renamed several times and cannot get it to work. I got it to work after logging out and re-saving the script. i don't know if this was coincidence or not because I tried this again after It wouldn't work again and it would not work. Is there a specific process that I must abide by? Bobby
1421292629
The Aaron
Pro
API Scripter
I believe it needs to be %%NUMBERED%% (uppercase). See if that fixes it. I can't see how it would actually work with it lowercase.
Aaron, I believe that was it. I did not know that these are case sensitive. Is there a way to make the &&NUMBERED%% just a letter such as %%N%%. The reason I ask is that I have found that I can create a monster character sheet and set the name as jondoe %%NUMBERED%% and it saves me the step of having to type %%NUMBERED%% each time I drag one onto the map. This would shorten the display name in the journal. Any suggestions is appreciated. Bobby
1421352612
The Aaron
Pro
API Scripter
Sure, you can just do a simple find and replace for NUMBERED with N and it will change the script correctly. I'll try to get a new version out soon that allows you to configure the name of the field. One thing to be aware of, it's the name of the token that is important, the name of the character can be anything. (See the 4th image in the OP)
The Aaron, First of all, Thanks! This is kick-ass and a tremendously helpful script. :D Now, the request, LOL! Would it be possible to have a script exactly like this one (or to modify this one) so that it will generate random, non-repeated numbers? Reasoning: I allow Players to see the names of the tokens on the board for communication and targeting purposes. However, I don't want them to automatically deduce the number of mobs on the map based upon the numbered token names and spoil the fun and suspense. I would like to be able to copy and paste a token and have a randomly generated number attached to the token's name. Cheers!
1421427232

Edited 1421427819
The Aaron
Pro
API Scripter
You're welcome! Re: your request -- Yes, it's possible, but a bit complicated. What might accomplish the same goal but would be easier would be to have a random increment and start number, instead of just using 1. So instead of having Orc 1, Orc 2, Orc 3, Orc 4, you'd have something like Orc 21, Orc 72, Orc 124, Orc 215, Orc 378... If that works for you, you can add this line: num += randomInteger(37); In the space on line 85: /* 84 */ .value() ); /* 85 */ num += randomInteger(37); /* 86 */ parts=renamer.exec(tokenName); You can change the 37 to be however large you want the potential gap, or even add another +randomInteger(37) or so if you want to adjust the distribution.
Dude, you're a rock star! Awesome. Many thanks!
1421427772
The Aaron
Pro
API Scripter
=D No worries.
1421428281
The Aaron
Pro
API Scripter
@Drohem -- If they start figuring that out, let me know. I just though of a reasonable way to do the original idea. =D
1421440325
Gen Kitty
Forum Champion
Oh man, this is evil for players who're used to GMs who don't randomly number their tokens. I'll be making this update before we resume play in February ^_^
1421441244
The Aaron
Pro
API Scripter
BWAHAHHAAH =D
1421445494
Gen Kitty
Forum Champion
Quick question: Would I be correct in guessing that randomInteger(37) means 'return a number between 1 and 37, inclusive'?
1421461032
The Aaron
Pro
API Scripter
Correct. You can think of it as taking the number of sides.
Just stumbled across this. This is a must-have. Thanks so much! M.
1423084295
The Aaron
Pro
API Scripter
Cheers! =D
1423918321

Edited 1423918725
Gen Kitty
Forum Champion
So I'm doing the scriptwork needed for resuming my Sunday campaign (yes, I procrastinate horribly) , and I made the random number tweak, using 3 as my random number. For all that is unholy, why would I get Skeleton 8 after Skeleton 4? :&gt; Ok, nevermind. I'll get a four because it's whatever-is-next PLUS 1-3. S'ok. Though, I'm not getting a very random distribution of numbers. But them's the breaks. I guess I'll just up the random ceiling and cross my fingers :&gt; (Also, I had a moment when the script didn't actually do anything, in the middle of testing the tweak, it just left a %%NUMBERED%% on the end. I paused and went to look at the troubleshooting guide in preparation for jumping through those hoops, but when I came back and cleared the skellies and tried again it worked fine. These ghost API issues are starting to get really stressful. :/ )
1423932232
The Aaron
Pro
API Scripter
That API issue has to do with timing. =D Notification of creation of objects to the API sometimes happens before the object is actually accessible. To get around that, I set a time for 500ms and do the manipulation then. Sometimes though, the object is STILL not created. You can safely delete it, and copy a new one it, or change the 500 on about line 97 to be a larger number. it will take longer to number things, but will be more likely to number them correctly. At some point, I should make the timeout... Ok, I just did it.. GIT: <a href="https://github.com/shdwjk/Roll20API/blob/master/To" rel="nofollow">https://github.com/shdwjk/Roll20API/blob/master/To</a>... This will now try multiple times to rename the token, starting at 200ms, then 400ms, 800ms, 1600ms, then give up. I haven't been able to make it fail to update at this point. I also added in the Random Spacing. It's not quite a first class citizen, but you can edit the randomSpace from 0 to some number. I also fixed the off by one error so a setting of 3 will mean the next token will be 1, 2, or 3 higher, not 4. Cheers!
Good to see that minor annoyance eliminated.
1424018068
The Aaron
Pro
API Scripter
See, if you'd just asked about it sooner.. =D
Aaron, I notice a bit of a bug that was easy to workaround, but thought you may want to know about it. I had a character journal named "Elemental, Air (Minor)", while named this way it would throw the following error when trying to add the token to the map: evalmachine.&lt;anonymous&gt;:691 name: parts[1]+(++num)+parts[3] ^ TypeError: Cannot read property '1' of null at setNumberFunction (evalmachine.&lt;anonymous&gt;:691:16) at null._onTimeout (/home/symbly/www/d20-api-server/sandcastle/node_modules/underscore/underscore.js:584:19) at Timer.listOnTimeout [as ontimeout] (timers.js:112:15) Simply renaming the character journal (and associated token) to "Elemental, Minor Air" did work around it. I assume that it didn't like the "(" or ")" in the name.
1424753598
The Aaron
Pro
API Scripter
Ah! Nice catch Kevin! It's because it's interpretting those as a capturing group when it builds the regular expression to extract current name numbers from the tokens on the page. I need to add escaping of the original name before inserting it into the RegExp. I'll do that and roll out a new version! Thanks!! :)
1424753680
vÍnce
Pro
Sheet Author
The Aaron said: Ah! Nice catch Kevin! It's because it's interpretting those as a capturing group when it builds the regular expression to extract current name numbers from the tokens on the page. I need to add escaping of the original name before inserting it into the RegExp. I'll do that and roll out a new version! Thanks!! :) Happens to me all the time. :-)
1424754091
The Aaron
Pro
API Scripter
Hahaha! Happens to the best of us!!
1424754187
vÍnce
Pro
Sheet Author
I meant needing Aaron's help. :-p
Hey The Aaron! Great script, makes things MUCH easier on me when populating my battlefields. But I have to wonder if it can be made even more functional: Names instead of numbers? Would it be possible to, for example, put a rollable table name on the token instead of %%NUMBERED%, such that it calls up an item from said table to name that set of tokens? Then all I"d have to do is make a table of %%GOBLIN-NAMES%%, or %%ORC-NAMES%%, or something like that, and get some randomly named critters? Just an idea I had. No nickels involved, I promise! q;} -Phnord
1424755526
The Aaron
Pro
API Scripter
That's a great idea!! I have plans to write a Marcov Chain based trigraph random name generator, and this would be a great way to seed it... In the interim, it would be pretty easy to have a syntax to put names in from a table. You'd need a pretty good sized table, and a plan for handling running out of names. You could repeat names, or append a count. (This problem would be solved by a generator. )
1424802724
The Aaron
Pro
API Scripter
Fixed! <a href="https://github.com/shdwjk/Roll20API/blob/master/To" rel="nofollow">https://github.com/shdwjk/Roll20API/blob/master/To</a>... Next for the tables and name generators...
1424802818
The Aaron
Pro
API Scripter
Update v0.4 -- Added escaping of token names to prevent bugs if certain characters are present, such as parenthesis. (Thanks Kevin!)
Great find, thanks The Aaron. I changed the num += ( randomSpace ? (randomInteger(randomSpace)-1) : 0); to num += ( randomSpace ? (randomInteger(randomSpace)-1) : 3); but its only increasing by 4 rather than 1, 2 or 3. Did I need to change the -1 to something else? I'm getting Orc 1, Orc 4, Orc 8, Orc 12 etc.
1427744246
The Aaron
Pro
API Scripter
You're looking for how to cause a random gap between names? line 12 has: randomSpace = 0, Change that 0 to some number (I like 37, but pic whatever you're comfortable with) and you will get a gap from 1 to 37 (or whatever number) in size. I'll see about adding that as a state based configuration value in a future deploy.
Perfect. Thank you.
How in the lord have I not been using this script for my games.