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

Any way to make a rollable token inherit the name from the table?

February 21 (8 years ago)

Edited February 21 (8 years ago)
Jen
Pro
I'm assuming this would require a script -- let's say I have a rollable table of goblins with three token images, and I name the images in the table Bitey, Giggles, and Pokey.

Is there an API-based way to set it so when I roll/reroll the token and the "Bitey" image comes up, the name of the token is "Bitey" even though it's tied to my generic Goblin sheet?
February 21 (8 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Certainly possible with an API, I'm not aware of a script that does it currently. Shouldn't be too hard, unfortunately I don't have time to work on it between RL and my scripts that I'm already working on updating.
February 21 (8 years ago)
The Aaron
Pro
API Scripter
Here you go:
on('ready',function(){
    "use strict";

    let nameFromSide = function(token,prev){
        let imgs=_.map(token.get('sides').split(/\|/),(i)=>{
                return i.replace(/%([a-zA-Z0-9]{2})/g,(m,n)=>String.fromCharCode(parseInt(`0x${n}`)));
            }),
            img=token.get('imgsrc'),
            tableItem=_.chain(findObjs({type:'tableitem'}))
            .reduce( (m,i)=>{
                m[i.get('rollabletableid')] = m[i.get('rollabletableid')]||[];
                m[i.get('rollabletableid')].push(i);
                return m;
            },{})
            .filter( (t)=>{ return _.keys(t).length===imgs.length;})
            .reduce( (m,t)=>{
                let count=_.reduce(t,(cnt,ti)=>{
                    if(_.contains(imgs,ti.get('avatar'))){
                        cnt++;
                    }
                    return cnt;
                },0);
                if(count===imgs.length){
                    m.push(t);
                }
                return m;
            },[])
            .first()
            .filter(t=>t.get('avatar')===img)
            .value()[0];
        if(tableItem && tableItem.get){
            token.set({
                name: tableItem.get('name'),
                showname: true
            });
        }
    };

    on('change:token:currentSide',nameFromSide);
    on('add:token',function(obj,prev){
        if(obj.get('sides').length){
            nameFromSide(obj,prev);
        }
    });
});

This will just work, provided you don't change the original table by adding or removing entries.  It will find all the rollable table with the same number of sides and the same images.  If it finds more than one, it will just take the first one (which is generally the oldest one). You can change the names in the table with impunity, but if you ever need to add images, you'll have to recreate the token in order for it to work with the new expanded table.  Doing it this way means you can reuse the same images in multiple tables without getting the wrong names.  Let me know if you have any issues.

Happy Rolling!
February 22 (8 years ago)
Jen
Pro
Oh wow!! How exciting. Thank you so much! I can't test it out right now but I am very much looking forward to trying it tomorrow. And yeah, I'm used to having to recreate the token to include whatever was in the expanded table, so that's all good.
February 22 (8 years ago)

Edited February 22 (8 years ago)
Jen
Pro
Okay, I do have one suggestion/request -- if the table item has the generic name "New Item", in that case can it not change the token name? I have a lot of preexisting tables without names, see. (I do plan to go through and name things, but haven't yet.)
February 22 (8 years ago)
The Aaron
Pro
API Scripter
Change this line:
     if(tableItem && tableItem.get){
to
     if(tableItem && tableItem.get && "New Item" !== tableItem.get('name')){
and it will skip "New Item" entries. 
February 22 (8 years ago)
Jen
Pro
That worked! 

You didn't mention your Patreon, but I will -- just signed up. You do such excellent work for the roll20 community... So often I run into a script I need here or in the github that has your name on it. Thank you again.
February 22 (8 years ago)
Jen
Pro
Also -- in combination with your Table Export Script, this will be super handy. So for example, let's say I have already made four tables of named elves for tokens:
  • Elf commoners male
  • Elf commoners female
  • Elf nobles male
  • Elf nobles female
Then if I wanted to make a separate token for use in a mixed crowd, I could easily use the output from Table Export and splice together to make these tokens:
  • Elf commoners 
  • Elf nobles
  • Elf males
  • Elf females
  • Elf everybody
Same thing if I have a list of people wearing armor vs. not and want to combine them, etc.
February 22 (8 years ago)
The Aaron
Pro
API Scripter
Ha!  Thanks, I appreciate it! =D  

I generally only mention it on full scripts (I call these little scripts "Snippets").  I don't want people to feel compelled as I'd much rather people asked me for help and let me help them, than have them shy away out of a sense that they shouldn't ask because they can't fund me.  Hope that makes since and doesn't make me sound like a wacko. =D
February 22 (8 years ago)
Jen
Pro
Well I mentioned it anyway, heh heh.

I do suggest for all your github scripts, adding some // comment up front so people remember how to use them.... listing the command words, etc. Or just link back to the thread -- it's usually mentioned in the thread but not the script, and you can't see the associated thread when you're at the github.

Usually I just add the URL like this when the script doesn't have it already:
// From https://app.roll20.net/forum/post/4669527/any-way-to-make-a-rollable-token-inherit-the-name-from-the-table/
February 22 (8 years ago)
The Aaron
Pro
API Scripter
That's an excellent suggestion and one I will place in my list of todos.  I'm working on rewriting the way I handle commands and help for all my scripts right now and this will definitely fit into that really well.
February 22 (8 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator

The Aaron said:

...I'm working on rewriting the way I handle commands ...

Looking forward to seeing what you come up with so I can shamelessly steal it ;)
February 22 (8 years ago)
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Clever idea, Jen!
sweet!