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,Update] A modified version of Kerberos's "Token face change based on rotation." script.

1508515993
The Aaron
Pro
API Scripter
Original Script:&nbsp; <a href="https://app.roll20.net/forum/post/2778056/slug%7D" rel="nofollow">https://app.roll20.net/forum/post/2778056/slug%7D</a> I revised this script to support an arbitrary number of faces, arbitrary rotation, and to handle non-thumb and non-user library images gracefully.&nbsp; Also case-insensitive for attributes and values when configuring it. The user I revised this for is using it to have portrait tokens that can be rotated for light direction.&nbsp; They've got 8 images setup with each successive image rotated counter-clockwise 45 degrees, the net effect being that the token when rotated clockwise, is always right side up.&nbsp; Hopefully they'll post some pictures. =D Here's the revised script: on('ready',()=&gt;{ &nbsp; &nbsp; const getCleanImgsrc = function (imgsrc) { &nbsp; &nbsp; &nbsp; &nbsp; let parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)([^?]*)(\?[^?]+)?$/); &nbsp; &nbsp; &nbsp; &nbsp; if(parts) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return parts[1]+'thumb'+parts[3]+(parts[4]?parts[4]:`?${Math.round(Math.random()*9999999)}`); &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; return; &nbsp; &nbsp; }; &nbsp; &nbsp; on('change:token:rotation', (obj) =&gt; { &nbsp; &nbsp; &nbsp; &nbsp; //Unless there is an attribute "Rotateable?" with the current value is &nbsp; &nbsp; &nbsp; &nbsp; //set to on of 'yes','y','1',1,'on' (case insensitive), it will not &nbsp; &nbsp; &nbsp; &nbsp; //attempt to change token image. &nbsp; &nbsp; &nbsp; &nbsp; if(obj.get("represents") == "") { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return; &nbsp; &nbsp; &nbsp; &nbsp; } else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let rotable = (findObjs({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _type: "attribute", &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _characterid: obj.get("represents"), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: "Rotateable?" &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },{caseInsensitive: true})[0]||{get:()=&gt;''}).get("current"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!['yes','y','1','on'].includes(`${rotable}`.trim().toLowerCase())){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; let rot = parseInt(obj.get("rotation"),10)||0; &nbsp; &nbsp; &nbsp; &nbsp; let sides = obj.get('sides'); &nbsp; &nbsp; &nbsp; &nbsp; if(sides.length){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let faces = sides.split(/\|/).map(decodeURIComponent).map(getCleanImgsrc); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let slice = 360/faces.length; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let bias = (slice/2); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let side = Math.floor(((((rot+bias)%360)+360)%360)/slice); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(faces[side]){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.set({imgsrc: faces[side]}); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; }); });
Nice! I liked that cool little script, glad to see it getting some attention!
1508536780
Finderski
Plus
Sheet Author
Compendium Curator
The game hasn't started yet, but here are some pics with Dynamic lighting to show this macro in action... And an animated (click the pic) one to show in a more dynamic way...