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

[Help] Hex Grid Range Finder issue

This is based on Austin B.'s script here:  Hex-grid-range-finder Anyone have any idea why this script doesn't work for me?  All I get is: : Token not found Token not found -Jz765GG80sLPwrnydq2 -K0S4H9INeZlsF9J3Gml Distance between Token1 & Token2 is undefined It looks like it can't find the tokens based on token_id....but I have no idea why that would be.  They appear to have a token_id....but then can't be found using it.  Keep in mind that I basically have no idea what I'm doing...basically a monkey mashing the keys when it comes to the API.  :-)
1445052745
The Aaron
Pro
API Scripter
You probably have an extra space between the !range and first id. 
1445053130

Edited 1445225197
The Aaron
Pro
API Scripter
If you want to fix the code to be more forgiving, you can change this : msg.content.split(" ", 4) to this: msg.content.split(/\s+/, 4)
I still get 'Distance between Token1 & Token2 is undefined', but not the 'Token not found' portions.
1445110552
The Aaron
Pro
API Scripter
Looks like there has been some change to the way the grid type is specified since this script was created (or perhaps it was just never used with Hex (H) back then).  Anyway, here's a corrected script, along with a few error message improvements: // Computes the distances between 2 tokens by ID // Use the macro target picker to fill in the ID // Script command // !range @{target|1st Target|token_id} @{target|2nd Target|token_id}  on("chat:message", function(msg) {     if(msg.type == "api" && msg.content.indexOf("!range") != -1) {         var n = msg.content.split(/\s+/, 4)         var cmd = n[0];         var id1 = n[1];         var id2 = n[2];         var token1 =  getObj("graphic", id1);          var token2 =  getObj("graphic", id2);         if (token1 && token2) {             var distance = tokenDistance(token1, token2);             sendChat("", "/desc Distance between Token1 & Token2 is " + distance);         } else {             if (!token1)                 sendChat("Range Finder","/w " + msg.who + " Could not locate first token!");             if (!token2)                 sendChat("Range Finder","/w " + msg.who + " Could not locate second token!");         }             }      }); function tokenDistance(token1, token2) {     if (token1.get('pageid') != token2.get('pageid')) {         return 'Cannot measure distance between tokens on different pages';     }     var page = getObj('page', token1.get('pageid')); var gridType = page.get('grid_type'); switch(gridType) { case 'hex':         case 'hexv': return  hexVDistance([token1.get("left"), token1.get("top")], [token2.get("left"), token2.get("top")]);         case 'hexr': case 'hexh': return hexHDistance([token1.get("left"), token1.get("top")], [token2.get("left"), token2.get("top")]);                      default:              return '(square grid not supported)'; } } function hexHDistance(unit1, unit2) { var q1, q2, r1, r2;     q1 = Math.round((unit1[0] - 46.48512749037782) / 69.58512749037783);     r1 = Math.round((unit1[1] - 39.8443949917523) / 39.8443949917523);     r1 = Math.floor(r1 / 2);     q2 = Math.round((unit2[0] - 46.48512749037782) / 69.58512749037783);     r2 = Math.round((unit2[1] - 39.8443949917523) / 39.8443949917523);     r2 = Math.floor(r2 / 2); return cubeDistance(oddQToCube(q1, r1), oddQToCube(q2, r2)); } function hexVDistance(unit1, unit2) { var q1, q2, r1, r2;     q1 = Math.round((unit1[0] - 37.59928099223013) / 37.59928099223013);     r1 = Math.round((unit1[1] - 43.86582782426834) / 66.96582782426833);     q1 = Math.floor(q1 / 2);     q2 = Math.round((unit2[0] - 37.59928099223013) / 37.59928099223013);     r2 = Math.round((unit2[1] - 43.86582782426834) / 66.96582782426833);     q2 = Math.floor(q2 / 2); return cubeDistance(oddRToCube(q1, r1), oddRToCube(q2, r2)); } function oddRToCube(q, r) { var x, y, z; x = q - (r - (r & 1)) / 2; z = r; y = -x - z; return [x, y, z]; } function oddQToCube(q, r) { var x, y, z; x = q; z = r - (q - (q & 1)) / 2; y = -x - z; return [x, y, z]; } function cubeDistance(cube1, cube2) { return Math.max(Math.abs(cube1[0] - cube2[0]), Math.abs(cube1[1] - cube2[1]), Math.abs(cube1[2] - cube2[2])); }
Thanks, Aaron.  You are the greatest!  :-)
1445225158
The Aaron
Pro
API Scripter
No problem!  Happy rolling!