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

Move and rotate tokens from char sheet

December 19 (7 years ago)
Sorry if this is a simple question, I am just learning how to develop custom character sheets, rolls, API, etc. My friends and I play war games and there is a need to make precise movements at weird angles. We made a template that we put under the token so we can see if the turn is 15 degrees or 45, etc. 

What would be ideal is if there was a way to have a drop down menu with all the common movement options (move 10', turn left 30 degrees, etc) and a button to execute the move and one to undo it in case they make a mistake. 

So I was wondering if there was a way to move and rotate tokens via API, or even better yet if someone made a script to do this. If you could point me in the right direction I'm happy to figure it out. 

If if there is any interest, when I'm done I'll post the Car Wars character sheets for the community. 
Thanks!
December 19 (7 years ago)
The Aaron
Pro
API Scripter
Ah! I love Car Wars!

You can definitely rotate and move a token using the API. Rotation is just a property of a token. Moving is a matter of changing the top and left property. To get the right distance, you’ll need to do a bit of trigonometry based on the rotation.  
December 19 (7 years ago)
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Sounds like a job for token-mod!
December 19 (7 years ago)
Thanks for the tips. I'll definitely check out token mod.
December 20 (7 years ago)
Just a quick note to say wow, nice job with tokenmod. Very well documented and easy. I got rotation working in 10 min :) If you ever add features, I would suggest a move forward and move back command. 

Since I need to do some math to calculate "move 10' forward" based on rotation angle, I think will need to write a small custom script to do the math/trig. Are all scripts activated by chat commands, and a macro just saves a set of commands? Or to put it another way, Can you directly call a script from a macro button without parsing the command (I.e. I don't need to pass any values other than selected token). It's not a big deal, just trying to get my head around how roll20 is structured. If you can recommend  a good script shell ("hello world" example) that would be awesome. 

Thanks for for helping out!
December 20 (7 years ago)
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
As I understand it, you can have it both ways. You could write a script called !movetenright that would move you ten feet to the right, or right a generic script called !move that can take arguments: "!move --10, right" or somesuch. The values passed can be math formulas, values pulled from a token or the character sheet it represents, or input by the user at execution time. A macro would determine how those numbers were pulled:

!move -- @{target), ?{distance|10}, ?{Direction|right,left,up,down}

Pardon any horrible syntax errors. This was for demo purposes off the top of my head. Please feel free to correct me, anyone who knows better. :)
December 20 (7 years ago)
The Aaron
Pro
API Scripter
Thanks!  TokenMod is one of my favorite scripts.  The biggest problem with "move forward" from TokenMod's perspective is "What is forward?"  Sometimes it will be in the "up" direction, sometimes it will be in the "down" direction, and it depends on the graphic, which can't be directly determined by TokenMod.  What I could do is take the "forward" direction as an angle offset from up, and assume a 0º offset if it's not specified.  Something like:
!token-mod --move 10
!token-mod --move 180|10
to move 10 units in the "up" direction for the first and 10 units in the "down" direction for the second.


Regarding API scripts, they are completely Event Driven.  Most scripts respond to the on('chat:message',...) event, which is then parsed for chat commands (where msg.type==='api' usually).  There are other events based on things happening on the tabletop, such as on('change:graphic',...) or on('change:graphic:rotation',...) and the like. 

Generally, a player (including the gm) will activate a script from the chat or via a macro/ability (which is identical from the script's perspective).  One common gotcha for new scripters is that the api receives the result of evaluating the chat message, not what was typed in.  For example:
!cmd @{selected|token_name} [[2d6]]
will send this as msg.content (for Beholder 1 being selected):
!cmd Beholder 1 $[[0]]
The rest of the data for the roll and such is on the msg object, but you won't know where some things come from (there's no way to know about @{selected|token_name} for example, just the expanded contents).  The full msg object for the above looks like this:
{
   "content": "!cmd Beholder 1 $[[0]]",
   "inlinerolls": [
      {
         "expression": "2d6",
         "results": {
            "resultType": "sum",
            "rolls": [
               {
                  "dice": 2,
                  "results": [
                     {
                        "v": 5
                     },
                     {
                        "v": 2
                     }
                  ],
                  "rollid": "-L0oqpuKnKqPi_mj_eKS",
                  "sides": 6,
                  "type": "R"
               }
            ],
            "total": 7,
            "type": "V"
         },
         "rollid": "-L0oqpuKnKqPi_mj_eKS",
         "signature": "641eb1ae7415cf2f954a09aa74108bd94b1ce8f2ff0def6926b7a5ae09a58947deac7395eb84e0dadd62abb2fed3af36316ab809732fa54ed8dd0f897bce5e1a",
         "tdseed": 8304631734064763000
      }
   ],
   "playerid": "-JS3qKxIUPHLzSbK24ve",
   "selected": [
      {
         "_id": "-L-WpTltXgC_N6RAtk_O",
         "_type": "graphic"
      }
   ],
   "type": "api",
   "who": "The Aaron (GM)"
}


Hope that helps?


December 22 (7 years ago)
Thanks again for the great response. Ill try to absorb and see what I can do over xmas break :) 

As for my suggestion of move forward / backwards, I would just assume the current token rotation value is the direction of forward movement. We can always adjust the icon to make sense if needed. Your idea of move 180|10 is great too, but it means that the macro would need to first read the current angle and then pass it back. 

I am sure I will have many more questions, but I am going to try and figure it out for a bit.
December 22 (7 years ago)
The Aaron
Pro
API Scripter
Actually, it wouldn't need to read the token.  I intended that as the offset of the Forward direction from up.  So if it was rotated at 23.5º, the move direction would be 203.5º.  =D
December 23 (7 years ago)
That does sound perfect Aaron. I was able to hack together an API to move, rotate and slide tokens heavily stealing from on some of your code and others. I hope you feel imitation is the sincerest form of flattery :) 

Is there a good place to review or at least see a list of all the built in stuff like sendchat? I was cruising the API library way too much looking for some similar code... It also seemed that a lot of items in the wiki were old too. Current scripts was really the only way to go. I also wish they had a wrapper script for new coders that shows how to intercept the chat commands and/or cycle through the selected tokens. 

Oh and of course one of my players looked at my spiffy new rotate and said "well its really suppose to turn left 15 degrees rotating around the back left corner like the car wars turn key, not rotating around the center. Lol. Wouldnt trade him for the world but I have some tweaking to do.

Thanks again!
December 23 (7 years ago)
The Aaron
Pro
API Scripter
Oh yes, imitate away!  Let me know if you run into any problems with it.

The wiki has some pages devoted to the built ins.  This is one: https://wiki.roll20.net/API:Function_documentation
Here's another: https://wiki.roll20.net/API:Chat

Probably looking at scripts is the best documentation.