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

October 19 (10 years ago)

Edited February 10 (9 years ago)
The Aaron
Pro
API Scripter
Update 0.5.5  -- 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: https://github.com/shdwjk/Roll20API/blob/master/To...
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.
October 19 (10 years ago)
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
That's handy!
October 19 (10 years ago)
The Aaron
Roll20 Production Team
API Scripter
Yeah, I'm looking forward to hearing the suggestions others have for improvements. :)
October 19 (10 years ago)
Awesome.
October 19 (10 years ago)
The Aaron
Pro
API Scripter
Thanks!
October 19 (10 years ago)
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.
October 19 (10 years ago)
The Aaron
Pro
API Scripter
I can make that work. =D I'll make a new version shortly.
October 20 (10 years ago)
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:

October 20 (10 years ago)
Star Wars and Star Trek together!
October 20 (10 years ago)
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 ).
October 20 (10 years ago)
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!
October 20 (10 years ago)

Edited October 24 (10 years ago)
The Aaron
Pro
API Scripter
You can use this on the production servers. That's where I use it. :)
October 24 (10 years ago)
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
Works like a charm.... very nice script and nice idea.
October 24 (10 years ago)
Weird... the imgsrc changes when you copy/paste? That seems wasteful programming to me. Have you brought this up with Riley?
October 24 (10 years ago)
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)
November 02 (10 years ago)
The Aaron
Pro
API Scripter
Update v0.21 -- Fixed Gist Link in the top of the source. (Thanks Vince!)
December 31 (10 years ago)
Awesome Script....Great Job Aaron. I just tried this out and man will it save some time.

Bobby
December 31 (10 years ago)
The Aaron
Pro
API Scripter
Thanks! Glad to hear you'll be able to use it!
January 15 (10 years ago)
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
January 15 (10 years ago)
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.
January 15 (10 years ago)
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
January 15 (10 years ago)
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)
January 16 (10 years ago)
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!
January 16 (10 years ago)

Edited January 16 (10 years ago)
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.
January 16 (10 years ago)
Dude, you're a rock star! Awesome. Many thanks!
January 16 (10 years ago)
The Aaron
Pro
API Scripter
=D No worries.
January 16 (10 years ago)
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
January 16 (10 years ago)
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 ^_^
January 16 (10 years ago)
The Aaron
Pro
API Scripter
BWAHAHHAAH =D

January 16 (10 years ago)
Gen Kitty
Forum Champion
Quick question: Would I be correct in guessing that randomInteger(37) means 'return a number between 1 and 37, inclusive'?
January 17 (10 years ago)
The Aaron
Pro
API Scripter
Correct. You can think of it as taking the number of sides.
February 04 (10 years ago)
Just stumbled across this. This is a must-have. Thanks so much!

M.
February 04 (10 years ago)
The Aaron
Pro
API Scripter
Cheers! =D
February 14 (10 years ago)

Edited February 14 (10 years ago)
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? :> 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 :>

(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. :/ )
February 14 (10 years ago)
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: https://github.com/shdwjk/Roll20API/blob/master/To...

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!
February 15 (9 years ago)
Good to see that minor annoyance eliminated.
February 15 (9 years ago)
The Aaron
Pro
API Scripter
See, if you'd just asked about it sooner.. =D
February 24 (9 years ago)
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.<anonymous>:691
name: parts[1]+(++num)+parts[3]
^
TypeError: Cannot read property '1' of null
at setNumberFunction (evalmachine.<anonymous>: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.



February 24 (9 years ago)
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!! :)
February 24 (9 years ago)
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. :-)
February 24 (9 years ago)
The Aaron
Pro
API Scripter
Hahaha! Happens to the best of us!!
February 24 (9 years ago)
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
February 24 (9 years ago)
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. )
February 24 (9 years ago)
The Aaron
Pro
API Scripter
Fixed! https://github.com/shdwjk/Roll20API/blob/master/To...

Next for the tables and name generators...
February 24 (9 years ago)
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.
March 30 (9 years ago)
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.
April 01 (9 years ago)
How in the lord have I not been using this script for my games.