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

API that can output when it registers a change in a token's horizontal position?

January 08 (4 years ago)

Edited January 08 (4 years ago)

After searching the script index, I'm not sure if this exists, if it it's buried in the fine print of an API, or if I'm just blind, but I am looking for a script that registers when a token's position on the X-axis changes, and here's why: 


 


I have a party that plays using pixelated tokens (created in a third-party app called Reroll, for those interested), so I thought it would be really neat if there was a way to get the system to register when they move right or left via a positive or negative change on the X-axis, and to have a negative change set the flip to the left, and the positive change set the flip to the right, that way when their tokens are moved (or likely when they are positionally updated after the move) it will flip automatically and be like they are playing an old-school pixelated video game. 

Does anyone have any insight on this?

January 08 (4 years ago)
The Aaron
Roll20 Production Team
API Scripter

There's not a script for that that I'm aware of, so I wrote you one:

on('ready',()=>{
  on('change:graphic',(obj,prev)=>{
    if(['objects','gmlayer'].includes(obj.get('layer')) && '' !== obj.get('represents')){
      let changeX = parseInt(obj.get('left'))-parseInt(prev.left);
      if( changeX > 0 && obj.get('fliph')){
        obj.set('fliph',false);
      } else if( changeX < 0 && !obj.get('fliph')){
        obj.set('fliph',true);
      }
    }
  });
});

January 08 (4 years ago)

You sir, are a god-send. Seriously, I can't thank you enough for your help these past few days!

January 08 (4 years ago)
The Aaron
Roll20 Production Team
API Scripter

=D

January 08 (4 years ago)

Now I just have to remember ahead of time to modify all my tokens to be right-facing or else I'm gonna get moon-walking NPC's XD

January 08 (4 years ago)
The Aaron
Roll20 Production Team
API Scripter

BTW, I set this up so it only acts on graphics on the Objects and GM layers, and only ones that represent characters.

January 08 (4 years ago)

Edited January 08 (4 years ago)
The Aaron
Roll20 Production Team
API Scripter


Rocklobster said:

Now I just have to remember ahead of time to modify all my tokens to be right-facing or else I'm gonna get moon-walking NPC's XD


Try this version:

on('ready',()=>{
  on('change:graphic',(obj,prev)=>{
    if(['objects','gmlayer'].includes(obj.get('layer')) && '' !== obj.get('represents')){
      let changeX = parseInt(obj.get('left'))-parseInt(prev.left);
      if(0 !== changeX) {
        obj.set('fliph',!obj.get('fliph'));
      }
    }
  });
});
It will invert the horizontal flip on a direction change, so if it's flipping the wrong way, you'd just manually flip it once and it will start going the right way. =D

Never mind, that didn't work right...

January 08 (4 years ago)

Edited January 08 (4 years ago)
The Aaron
Roll20 Production Team
API Scripter

Here's a version that works correctly with dropping movement paths with 'Q' or right clicking the mouse while dragging:

on('ready',()=>{
  const getLastMoveDirHorizontal = (obj) => {
    let pts = obj.get('lastmove').split(/,/).map(n=>parseInt(n)).filter((p,i)=>i%2===0);
    pts.push(parseInt(obj.get('left')));
    let dir = 0;
    while(0 === dir && pts.length>=2){
      let pt = pts.slice(-2);
      dir = pt[1]-pt[0];
      pts.pop();
    }
    return dir;
  };

  on('change:graphic',(obj)=>{
    if(['objects','gmlayer'].includes(obj.get('layer')) && '' !== obj.get('represents')){
      let changeX = getLastMoveDirHorizontal(obj);
      if( changeX > 0 && obj.get('fliph')){
        obj.set('fliph',false);
      } else if( changeX < 0 && !obj.get('fliph')){
        obj.set('fliph',true);
      }
    }
  });
});

January 08 (4 years ago)
If I understand correctly, marking a path with Q while moving a token will now appropriate change the direction based on the horizontal movement of the path?
January 08 (4 years ago)
The Aaron
Roll20 Production Team
API Scripter

Right, so if your path moves was kind of a hook shape, it could end up facing the wrong way with the first script, because doubling back wasn't accounted for.

January 08 (4 years ago)

Edited January 08 (4 years ago)

Ah, gotcha that makes sense. Out of curiosity, is it possible to have this observable with Marching Order, so that tokens that are following another token would also flip the appropriate directions?

January 08 (4 years ago)
The Aaron
Roll20 Production Team
API Scripter

Possible: yes.

Lots of work: Probably. =D

Honestly, it would likely be easier to just hack this into Marching Order directly.  Link me to the version you're using?

January 08 (4 years ago)

Edited January 08 (4 years ago)

I can't find a direct link, but it is the latest version of the one in the one-click library. 

I did notice, however, that the waypoints weren't functioning as intended with the flipping script. For example, while facing left, if I move the token to the right, to the left, and back to the right, it remains facing to the left, even though the last movement was to the right. 

January 08 (4 years ago)
The Aaron
Roll20 Production Team
API Scripter

Ok, I'll look at that version.

With the path move, the last leg didn't happen to be straight down or straight up, did it? in that case, it wouldn't change the flip.

January 08 (4 years ago)

Edited January 08 (4 years ago)

No, it was not. I did plenty of moving the token around various path formations to test the issue, but it doesn't seem reliable at the moment. From what I can tell, it functions fine when the path is direct and without waypoints, whether using the ruler or not, but adding a waypoint causes the flip to not happen. 

January 08 (4 years ago)
The Aaron
Roll20 Production Team
API Scripter

Hmm..  If you wanna PM me an invite to your game and GM me, I can come take a look.

January 08 (4 years ago)

Edited January 08 (4 years ago)
keithcurtis
Forum Champion
Marketplace Creator
API Scripter

I would absolutely use Aaron's script, but there is an ancient one in the wiki that does this, for Iso games. It also flips directional lighting, I believe.

January 08 (4 years ago)

Invite has been sent!

January 08 (4 years ago)
The Aaron
Roll20 Production Team
API Scripter

Fixed in the above post.  The issue was a double waypoint at the end of the move causing a perceived change of 0.  Now it will look for the last change in horizontal position to set the facing, and will handle moving vertically in a final leg of the path. =D