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

How to copy token with script?

January 17 (10 years ago)
Hi all, I'm brand new to the Roll20 API and javascript, so forgive me if this question seems a little thick.

I'm trying to create a function that will allow me to populate a number of tokens, adding an iteration number to each, with the option to roll new hp for each based on a hit_dice attribute. I've managed to create the new token and hand-copy over attributes from the base token, but I'd like to be able to automate the copying of attributes, so I don't have to hand enter every single possible token attribute. Here is my code so far:

function(msg, ARGV)
    {
        var objectCount = ARGV.shift();
        var baseObject = getObj('graphic', msg.selected[0]['_id']);
        var baseImage = baseObject.get('imgsrc');
        baseImage = baseImage.replace("/med.png", "/thumb.png");
        
        log(baseObject);
        
        var baseKeys = baseObject.keys();
        
        for(var n = 0; n < objectCount; n++)
        {
            var newObject = createObj("graphic", {
            subtype:'token',
            pageid:baseObject.get('_pageid'),
            imgsrc:baseImage,
            represents:baseObject.get('represents'),
            layer:baseObject.get('layer')
            });
            
            for(var i = 0; i < baseKeys.length; i++)
            {
                var key = baseKeys[i];
                if(key.indexOf("_") != 0 && key.indexOf("imgsrc") != 0)
                {
                    newObject.set(key, baseObject.get(key));
                }
            }
            
            newObject.set('left', baseObject.get('left')+70*(n+1));
	    // TO DO: assign new name, assign new hp
            
        }
    };

Unfortunately, "baseObject.keys()" is not a valid function call for the Roll20 token object. Is there another way to get an array with all the attributes of an object? Or is there an altogether better way to go about implementing this? Thanks so much!
January 17 (10 years ago)
Gen Kitty
Forum Champion
You might take a look at The Aaron's token numbering script for ideas: https://app.roll20.net/forum/post/1273423/script-t...

Good luck with your coding!
January 17 (10 years ago)
Oh man, that might eliminate the need for mine altogether...

Thanks a bunch!
January 17 (10 years ago)
The Aaron
Pro
API Scripter
You might also find this useful: https://gist.github.com/shdwjk/7377de58100f4e8134...

It's an older script, but probably still works. Let me know if you need help tweaking it. :)
January 17 (10 years ago)
Perfect. Changed the hit dice attribute and the bar and it seems to be doing exactly what I need.

I also just grabbed your charUtils script, which I'm using to create a new monster from a template with the click of a button. (I very clumsily added a copy attributes function to it so I can bring over the standard attribute names in addition to the macros. This saves me 3 or 4 steps vs opening up a template character, pressing edit, then duplicate, then finding "Copy of AA Monster Template". If only I could get the new character to pop open once I've created it.)

Do you have any other amazing magic scripts up your sleeve?
January 17 (10 years ago)
Gen Kitty
Forum Champion
The Aaron is our resident Scriptomancer. So yeah.

The Aaron, Arcane Scriptomancer
"While wizards may learn to bend magic to their will, the Scriptomancer alone understands the language of the gods, in which the very building blocks of the world are written. Wielding both his esoteric knowledge and the hammer of the Ay'p Eye, The Aaron reshapes the world at its most fundamental level, producing results that benefit the common man; though their workings may be beyond mortal comprehension."
How about a GroupInitiative script? https://app.roll20.net/forum/post/1066916/script-g...

How about editing token properties from macros/chat? https://app.roll20.net/forum/post/1257490/#post-12...

These, and many more are lurking in The Aaron's grimoire.
January 18 (10 years ago)
The Aaron
Pro
API Scripter
GenKitty, you're one of my favorite people. :)

Redrick, if you go to my Gist page you can look through all my scripts: https://gist.github.com/shdwjk
If you want to find the forum thread about one of them, just search for the full URL on the forum and it should be easy to spot. It will start with [Script].
January 18 (10 years ago)
Well, sir The Aaron, clearly I ain't the first to sing your praises, and I surely won't be the last. I'll be keeping an eye on your posts.
January 18 (10 years ago)
The Aaron
Pro
API Scripter
I aim to please! :)