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

Snap token rotation to the grid with a script?

Let's say I have a map with snap-to-grid turned on. Now let's say that a script rotates a token, but the new rotation is not in the grid increment. For example, the token is rotated to 37 degrees on a square map. Now let's say that as a next step, I want the script to snap the token's rotation to the nearest snap angle (in the case of 37 degrees, it would snap to 45). Is it possible to do this with a simple script? I recognize that I could do this with some math scripting that finds the nearest snap angle and then rotates the token to that angle. But I'm wondering if there is a script that just snaps the selected token to the grid, kind of like what happens when I rotate the token manually. Thanks in advance.
1617915281
The Aaron
Roll20 Production Team
API Scripter
The biggest issue is getting the script to know when to do that.  What script are you rotating with?   (Great Avatar BTW. I love "The Devil is a Part-Timer!" and was literally re-watching it last night! Season 2 is supposed to come out this year, super excited! =D )
1617927528

Edited 1617927589
Actually, this is going to be my first Roll20 scripting project, and right now I'm trying to understand what's easy to do, what's not easy, and what can't be done. Right now I'm looking at TokenMod to rotate the token, but there may be a better way, or one that makes more sense for what I'm trying to do. Stepping up a level, here's what I want to do: I've got two tokens on a hex grid - an active token and a target. I want to have the API rotate the active token to face the target, but then have the active token snap to the nearest hex angle (either side or vertex). Put another way, I want the active token to rotate to the "snap angle" that is closest to pointing directly at the target.  This is one piece of a game's movement system. I'm hoping to create some scripts that handle the tedious bits for the players. (And OMG! There's going to be a second season of "The Devil is a Part-Timer"? That's awesome!)
1617939904
The Aaron
Roll20 Production Team
API Scripter
That sounds like a great first scripting project! If you were going to rotate with TokenMod (and I don't think you should in this case), you could observe TokenMod changes and get notified when it gets rotated. However, for your situation, I'd write a custom script to do the rotation.&nbsp; You'll probably have a command like: !face-token-hex --source @{selected|token_id} --target @{target|token_id} Then your script would watch for that that command and rotate the source token to face the target token.&nbsp; You must pass both IDs in this case because there is a long standing bug where @{target} will prevent the API from getting a msg.selected array.&nbsp; You get around that by specifying the @{selected} argument. After parsing out the token ids, you'd grab both Graphic objects for the tokens, then calculate the angle between them.&nbsp; You can then get the rotation angle with some pretty simple math: const SNAP = 360 / ( 6 + 6 ); // (hex sides + hex points) const HALF_SNAP = SNAP / 2 ; const getAngle = ( angle ) =&gt; Math . floor ( ( angle + HALF_SNAP ) / SNAP ) * SNAP ; Then set the rotation and your golden.&nbsp; You might use this function for the angle if you want to keep it bounded between 0 and 360: const getAngle = ( angle ) =&gt; (( Math . floor ( ( angle + HALF_SNAP ) / SNAP ) * SNAP ) % 360 + 360 ) % 360 ; (Yup!!!&nbsp;&nbsp; <a href="https://www.cbr.com/the-devil-is-a-part-timer-season-2-announce/" rel="nofollow">https://www.cbr.com/the-devil-is-a-part-timer-season-2-announce/</a> &nbsp;)
Thanks for the help. I've started messing around with the script. I'm sure I'll be back to the forum again with another question soon!
1617997613
The Aaron
Roll20 Production Team
API Scripter
Great!&nbsp; We love questions!&nbsp; =D