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

[Request] Change attributes based on token's position

So I was wondering if anyone could write a script for me that would change a character's attributes based on where its representative token is moved on the grid. For example: Say I had a character with two attributes named 'position-y' and 'position-x,' and the character token's current coords on the grid were 'top:2905, left:1575.' If I moved the character's token up two spaces and left one space on the grid, the script in question would automatically change the value of the 'position-y' attribute to '2765' (-140) and the 'position-x' attribute to '1505' (-70). I feel like this one might not even be too hard to make--one of my scripts already spits out the top and left position of a token to the api output console whenever a token moves, I just need a script that essentially does the same thing except with attributes. If anyone could help me with this it would be an extremely useful tool for me (and I'm for some other GMs too). Thanks. :)
The script you are asking for could be writen easy but I would like to know why you need the position in a attribute,  It is already stored on the graphic.  Why can't you use that?
1486589149
The Aaron
Pro
API Scripter
I'm going to guess it has to do with charting the location of a token on a star map or something. on('ready', function(){     "use strict";     on('change:graphic',function(obj,prev){         if( ( _.contains(['gmlayer','objects'],obj.get('layer')) ) &&             ( obj.get('left') !== prev.left || obj.get('top') !== prev.top) &&             obj.get('represents') ) {             let a=_.chain(findObjs({type: 'attribute',characterid: obj.get('represents')}))                     .filter(a=>a.get('name').match(/^position-[xy]$/))                     .each(a=>{                         switch(a.get('name')){                             case 'position-x':                                  a.set({current: obj.get('left')});                                 break;                             case 'position-y':                                  a.set({current: obj.get('top')});                                 break;                         }                     });         }     }); }); If the token represents a character, then it's attributes named 'position-x' and 'position-y' will be updated when it gets moved, if they exist.  Case sensitive. Happy Rolling!
1486595464
Lithl
Pro
Sheet Author
API Scripter
Feature creep: Now do a.set({ current: a.get('current') + obj.get('left') - prev.left })  (if a.current has a value) to add the distance moved. Then a user can manually modify the token's "origin" point to be somewhere other than the top-left corner of the map! =D
Neat! Thanks guys. Very much appreciated. :D