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

Token movement and more

Hello all, I would think that this would be basic functionality, but I can't seem to find it.  I am wondering if there is something I'm missing, or if I have to do it manually. I would like to move tokens around the map from cell to cell with the API. I have seen where I can use obj.set() with the "top" or "left" properties, but that's in pixels, not cell units (hexes or squares) I have also seen the Distance Functions, which say, "So it's important to use conversion functions (provided below) to make sure that you're respecting the game's settings." But each function listed is not yet implemented. The API script "Slide Tokens" seemed promising, but it appears to be limited to squares only, not hexes. When I use the arrow keys to move a token, it works properly when moving "forward" or "backward" but there doesn't seem to be a way to move "left-forward", "left-backward", "right-forward", or "right-backward"  It just moves straight left or right, and sits halfway between hexes.  If I then press forward or backward it moves the half distance and drops into a cell properly.  This is close, and I would love to use the API for this, but again seems to be designed for squares only, and does not work properly for all unit shapes. The UI works well with hexes.  I can drop "close" to a cell, and it snaps to it.  This is what I expect in the API. I am also wondering why the documentation says that 1 unit is always equal to 70 pixels when, for me, it's 79.68878998 in two of the directions, and 80.18519676 in the other. Sorry for so many questions, but I would hate to invest a lot of time here if the work has already been done and I'm just missing it! Thanks in advance! I would also love to be able to check if there are other tokens in the hex before I move, but I'll just settle for movement for now.
Oh, one more thing... When I create a map that's 27x27 units and turn on the grid (1 hex = 1 unit) the map is 23x26 hexes.  Is there any real coorelation between requested map size, and actual map size?
1448303822
The Aaron
Pro
API Scripter
The basic answer to your questions is you have to do the math your self currently.  There aren't any functions to support the positioning units rather than by pixels.  70 pixels is the size of the default square grid, the hex sizes are a bit different, as you've noted.  It goes back to before my time on Roll20, but I imagine the hex grid was added later, and most of the defaults rely on the square grid.  (certainly, that's true of the snap to grid when drawing)  Probably the size of the hexes was picked to fit onto the square grid in a particular way (possibly every other column of H-hexes has a square centered in it?).  The size of the map is likely fixed at the point where you set it, so switching grid type won't prompt a change in the size unless you also change the size when you change the grid.  Experimentation should help determine what is really going on. All that said, it shouldn't be too difficult to solve both of your issues.  At a minimum, you should be able to snap some tokens into each of some hexes and figure out the relative offsets in each direction, then write a function that calculates the position for centering in each of the hexes.  Once you've figured that out, it should be reasonably straight forward to work it backwards to figure out the hex coordinates of each object on the token layer and determine if there is something in a given hex.  Bonus points for building and caching the position map at startup, update on token move. =D I'd be happy to help with those functions later tonight, if you'd like to detail out some ways you plan to use them so I can build the right interface. =D
1448304428

Edited 1448304461
I was hoping the work was already done!  8) I noticed that I can turn on the Labels for hex maps.  It appears that each hes has been assigned a letter-number address.  I wonder if it would be possible to retrieve from a token its current hex, or at the very least, the x,y pixel coordinates of the hexes to compare to the tokens location.
1448306684
The Aaron
Pro
API Scripter
Totally possible.  =D
I examined every object returned by getAllObjs(), and haven't found anything that looks like I would have the information on the cell coordinates. Specifically, the Token and the Page object.   I see the Token knows its x,y position in pixels. I see the Page knows which tokens are on it, what kind of grid, and whether labels are on or off. I cannot find where those cell labels are stored - are they stored? Are they computed on-the-fly by the UI?  How can one access the cell information?
1448311808
The Aaron
Pro
API Scripter
Sorry, I should have been clearer.  They are calculated on the fly.  I thought you were proposing an interface where you'd call a function (that I would write for you) which would return those Hex coordinates.   They aren't available on the objects and would need to be calculated.
Hmmm, that's a shame, really. Not your offer, no certainly, thank you for offering your time! It's the fact that those are already being calculated, but they're not available, so they must be re-calculated.  Not only is it inefficient, it could be prone to error. I'll have to re-think if this will be worth investing my time.  Or if I just continue to push things around manually.  8) Thanks again!
1448316452
Lithl
Pro
Sheet Author
API Scripter
The Aaron said: It goes back to before my time on Roll20, but I imagine the hex grid was added later, and most of the defaults rely on the square grid. Considering the app was originally designed to facilitate playing D&D 4e? Yeah =)
The Aaron or anyone that might be of help. I am also trying to use the patrol API script. I however am a newb. I am very good at following directions, however most posters don't give very detailed instruction or assume some knowledge of the API. In the Patrol setting example they mention the name ID for the graphic. When I looked it up what this refers to the guide says the name of the token. However I'm unclear what name they mean. The Token name that appears when you click the checkbox for show name or the actual name of the token graphic. The latter of the two I have no idea how to find.  I've tried the Script with the name of the token "Drow Warrior" in the script replacing all instances of Guard A, but nothing happens when I load my campaign. Also I tried !Walk which is mentioned vaguely in the notation on the code but that does nothing as well. Any advice? Here is the script: // Guards on patrol... on("ready", function() { //Wait until the ready event fires so we know the campaign is completely loaded. //Get a reference to our patrolling token. log("Going Drow Warrior..."); var patroltoken = findObjs({_type: "graphic", name: "Drow Warrior"})[0]; //We know there is a token in the Campaign called "Guard A". if(!patroltoken) { log("NO Drow Warrior"); return; } var direction = -70; //Walk left 70 pixels. var stepstaken = 0; //How many steps have we walked in the current direction? setInterval(function() { if(patroltoken.get("status_greenmarker") === false) return; if(stepstaken > 9) { //Switch directions! direction = direction * -1; //will "flip" the direction we're walking stepstaken = 0; //reset steps back to 0. } patroltoken.set("left", patroltoken.get("left") + direction); //walk! stepstaken++; }, 3000); //take an action every 5 seconds });
1449387874
The Aaron
Pro
API Scripter
Based on your other post, I'll assume you got the working. 
1449413593

Edited 1449413741
var patroltoken = findObjs({_type: "graphic", name: "Drow Warrior"})[0]; //We know there is a token in the Campaign called "Guard A". John, you somewhat answered your own question with this line. When you do a find on "_type: graphic" it is looking for the token and therefore, will be looking for the token name (whatever is currently entered in the "Name" field on the "Edit Token" screen).  It will not be the name that shows in the "Represents Character" field on the same screen. In the case above, you won't find what you're looking for unless there is a token actually named "Drow Warrior" in the game.  Put "Guard A" in there instead and it would probably start working.
Personally, I would create a waypoint/pathing system that utilizes semi-invisible graphics on the GM Layer that you can use to make paths for patrols. Since you can't add attributes directly to tokens, you either need to make all tokens tied to a character sheet, use one of the bars, or use a system that looks for status icons on the token and moves them down towards the nearest waypoint with that icon. You could also extend this system to look for the waypoints in sequence by using the numbers on the status effects. For example... you could use Red on a Drow token and then Red 1, Red 2, Red 3, Red 4 to make the token patrol in that order.