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] Range Finder

December 25 (11 years ago)
Just a script that finds the range between 2 tokens by name since the ruler is useless when you zoom out. Only supports squares and assumes 70 is the grid size (not sure how to get the grid size from the API). Defaults to names T1 and T2 if no names are provided.

Usage:

!range TokenName1,TokenName2

Range Finder Source

December 25 (11 years ago)
You should use @{target|1st Target|token_id} and @{target|2nd Target|token_id} instead. Also saves some code in the API since you don't have to use findObs to get the correct tokens based off of their name. Also means you can have tokens with the same name w/o confusing the API
December 25 (11 years ago)

Edited December 25 (11 years ago)
Actually I wanted to keep it by name since scrolling,panning and picking on map that is over 100x100 is a pain. It is much faster to just type out the names especially when you are checking every turn, but definitely worth adding another version to do one off picks.
December 25 (11 years ago)

Edited December 25 (11 years ago)
Different version that uses the UI picker to select the tokens.

Source
January 19 (11 years ago)
Peter, why do you use this line to calculate distance?

dist = lDist+(tDist-lDist)

wouldnt it be simpler to write:

dist = tDist ?
January 19 (11 years ago)
yep just got lazy and left some experimental code in there ( think I was seeing if there was a difference between squares and hexes but dont remember exacly_
This script is great as you're right the ruler is pap when zoomed out, I'm not the versed in APIs but was wandering if there was a way to change the output in a few ways:

(1) Round it to 2 decimal places
(2) Format it (i.e. as /desc)
(3) Put the unit after the number (i.e. CMs or Inches)

Thanks,

Craig
That is pretty easy, at lines 47/48 the code is

dist = dist;
sendChat("","Range between "+name1+" and "+name2+" = " + dist);

change it to

var distStr = dist.toFixed(2);;
sendChat("","/desc Range between "+name1+" and "+name2+" = " + distStr);


PeterW


Thanks Peter! That's brilliant!
March 20 (11 years ago)
Okay, terrible question but how do I use the !rangep api? I tried selecting tokens in the tabletop area and I scanned the code a little bit (I don't know very much about coding). I'm sorry but I just can't figure it out.
March 21 (11 years ago)
Lithl
Pro
Sheet Author
API Scripter

Peter W. said:

assumes 70 is the grid size (not sure how to get the grid size from the API).

70 is the grid size in pixels as far as the API is concerned.
If you use the !range you need to type the 2 token's name afterward for example ( if you don't supply names it assumes the names are T1 and T2 )

!range orc, goblin

If you use the !rangep just create your macro like so

!rangep @{target|1st Target|token_id} and @{target|2nd Target|token_id}

and the macro will prompt you to pick 2 targets by clicking on them.




March 21 (11 years ago)
Thank you so much, Peter. I never would have figured that out on my own.
March 25 (11 years ago)
Peter, is there a way to have the macro calculate diagonals? For example, if I have two tokens on the x axis and they are 20 ft from one another, the script calculates it properly and displays 4 as the result. When there are two tokens that are diagonal from one another (say 4 squares on the x and 4 squares on the y) it is showing up as 4 total. The ruler tool finds the distance as 30 ft (6 squares). I assume there would have to be some math in there that calculates the hypotenuse by using: x squared + y squared = distance squared and then square root the result. Is that even possible?
var gridSize = 70;

var lDist = Math.abs(token1.get("left")-token2.get("left"))/gridSize;
var tDist = Math.abs(token1.get("top")-token2.get("top"))/gridSize;

var dist = 0;

dist = Math.sqrt(lDist*lDist + tDist*tDist);
dist = Math.floor(dist + 0.5);

return dist;
What Callum posted will give you the real distance. The original script just counted boxes.
March 26 (11 years ago)
Thank you very much Callum & Peter. We have it up and running and it's working perfectly.
I have a question concerning this script... can I somehow get the unit size for the map grid and say something like :

"dist = 1/<grid unit size> * dist"

Any help would be appreciated
July 02 (11 years ago)
The Aaron
Roll20 Production Team
API Scripter
Malfane, Brian talks about getting units right in this post: https://app.roll20.net/forum/post/972389/#post-972547