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

Rotate Token "!rotate" Script

December 04 (11 years ago)
Hey all, new to this scripting so still trying to wrap my mind around it. my attempt is to make a command that will swivel a token 90'. I know it's a basic script but just can't nail it.any help would be awesome!


/**
* If a player or GM uses the `!rotate' command, all graphics they have selected
* will flip horizontally by 90 degrees.
*/
on('chat:message', function(msg) {
if(msg.type == 'api' && msg.selected && msg.content.indexOf('!rotate') == 0)
{
var selectedObjs = msg.selected;
_.each(selectedObjs, function(obj) {
if(obj._type == 'graphic')
{
var token = getObj('graphic', obj._id);
token.set('rotation 90', !token.get('rotation 90'));
}
});
}
})
December 04 (11 years ago)

Edited December 04 (11 years ago)
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
/*
* If a player or GM uses the `!rotate' command, all graphics they have selected
* will flip horizontally by 90 degrees.
*/
on('chat:message', function(msg) {
    if(msg.type == 'api' && msg.selected && msg.content.indexOf('!rotate') == 0){
		var selectedObjs = msg.selected;
		_.each(selectedObjs, function(obj) {
			if(obj._type == 'graphic'){
				var token = getObj('graphic', obj._id);
                
                		var rotation = token.get("rotation");   //Find the current rotation value
                		rotation = rotation + 45;               //add 45 degrees, this could also be 90. 
                		token.set({rotation: rotation});        //set the rotation
			};
		});
	};
});
December 04 (11 years ago)
Stephen! Thanks a bunch man works beautiful! i am plugging away at the codecademy course trying to get my head wrapped around all this so i can pass out some contributions as well! :-) cheers!
December 04 (11 years ago)

Edited December 04 (11 years ago)
So the only thing i think i need to adjust on your script is how to turn back on the "auto-lock" at the pre-set degrees. meaning, once you've rotated a token, the automatic lock at certain angles that the tabletop has built in, no longer functions. But i'll work it! more to follow :-)


December 05 (11 years ago)
How about using a variant of the "can't rotate" event API.

on("change:graphic:rotation", function(obj, prev) {    
  //Always set rotation back to 0, so no one can rotate objects.    
  obj.set("rotation", 0);
});

Except that, instead of limiting it to a rotation of zero degrees, have it use a State variable on the token which your token rotation script (above) sets.

December 05 (11 years ago)
Sweet, Thanks lifer! So, say my scripint skills were craptastic, i wouold just add that in on the bottom of the previous script?
December 05 (11 years ago)

Edited December 05 (11 years ago)
Yes, I have not used a State variable yet, so I'm not 100% certain on the syntax or limitations, but the script that Stephen S posted would simply need a line right next to
token.set({rotation: rotation});
that also stuck the value of the rotation into a State variable on the token.

Then change the script for preventing rotation to read that State variable and use its value instead of 0 for the obj.set line.

DISCLAIMER: Keep in mind, this is all just theory. I don't know if this would even work. It seems like it might, though, and I like the concept for something like a board game where turning costs movement points, etc.


January 24 (11 years ago)
Another cool idea, and thanks to all that contributed to it. I currently make use of this script, and am quite happy. I myself am not very good at API yet, but I am trying to get better by doing simple tasks, mainly playing with API and dungeon trap situations. I have found some success, but now I have run into an obstacle. I was wondering if it would be possible to combine the "Patrolling Tokens" script with what you were doing here? And if not, then maybe with the "fliph" command? The idea is to simulate a moving trap, and have it travel along a repetitive path but to turn at a set rate with each direction change so as to create the imagery of a swinging trap (like a pendulum moving back and forth, per say). Could also use this to make a dart wall trap that is constantly firing, and having the same token (the darts themselves) simply "turn around" within the well concealed dungeon wall on either side only to come back again. Think it would be very cool myself, and I have tried a few different combinations, but cannot get it to fully function. The best I can come up with is "it makes the set amount of moves, turns once with [fliph], then repeats the move set but never turns again" kinda deal. Advice? Help?
January 25 (11 years ago)
Ryan, yeah i wish i could help you but my scripting is rather subpar at the moment. still talking the scripting classes at codacedemy ;-)