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 to swap position of two tokens on the same page?

Just curious if anyone might know if such a script exists. Basically what I'm wanting is something that would allow a player to have a token selected, be prompted to target another token (ala how TokenMod does it) and then whatever second token they select has their position on the current page swapped with the selected token. From what I can tell it's possible but I've not seen any 'teleportation' or swapping scripts floating around or if there was a movement-related script that could perform such a trick.
1528302932
The Aaron
Pro
API Scripter
So, would you need it to work across layers, or would being on the same layer as a requirement be fine?
Same layer is 100% fine, being able to do so across layers would be a lovely perk but not essential.
1528303353

Edited 1528303780
The Aaron
Pro
API Scripter
TokenSwap: !token-swap ID1 ID2 Example: !token-swap @{selected|token_id} @{target|Swap with|token_id} Currently, this would only work on the same layer (I don't think you can change layers while using @{target}) but that shouldn't be a problem for players.  Note that it doesn't check ownership of any tokens, so it could be used to swap any tokens on the tabletop.  Let me know if that's a problem. Code: on('ready',function(){     "use strict";          on('chat:message',function(msg){         var args, who;         if (msg.type !== "api") {             return;         }         who = getObj('player',msg.playerid).get('_displayname');         args = msg.content.split(/\s+/);         switch(args.shift()) {             case '!token-swap': {                 let t1 = getObj('graphic',args[0]);                 let t2 = getObj('graphic',args[1]);                 if(!t1 || !t2) {                     sendChat('TokenSwap', `/w "${who}" Please provide 2 token ids to swap: <code>!token-swap ID1 ID2</code>`);                     return;                 }                 let p1={                     top: t1.get('top'),                     left: t1.get('left')                 };                 let p2={                     top: t2.get('top'),                     left: t2.get('left')                 };                 t1.set(p2);                 t2.set(p1);                 if(!playerIsGM(msg.playerid)){                     sendChat('TokenSwap', `/w gm <code>${who}</code> just swapped <b>${t1.get('name')}</b> and <b>${t2.get('name')}</b>. <a href="!token-swap ${t1.id} ${t2.id}">Swap Back</a>`);                 }                 }                 break;         }     }); });
That's perfect for my needs and should be fine as-is. You sir, as always, are simply amazing.
1528303606

Edited 1528303794
The Aaron
Pro
API Scripter
=D  No worries!  Let me know if you want any enhancements. Would be trivial to whisper a notice the GM that it happened with a "swap back" button, etc. Edit: Added that notice... =D
Will do! I think this should cover me for what I needed.  Long story short, have a player in my game playing a 5e subclass that lets her create duplicates that she can swap places with and just wanted an easy way for her to do that without having to drag both tokens around trying to keep straight where they originally were, etc. So this handles that excellently.  Also it reminded me to go support you on Patreon like I've been meaning to do as I'm a huge fan of your api work. :)
1528304159
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Ah, Benign Transposition!
1528304378
The Aaron
Pro
API Scripter
Hey, thanks!  I'm planning to use Patreon to finance a new laptop, so every bit helps. =D Yeah, I was thinking about how this script will help me with my Nomad Mystic who is always using Benign and Baleful Transposition. =D  I think there are some other class abilities that let you swap positions with others, thinking about some fighter defender-y type ability. =D