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

Changing Token Face Based on Rotation

Hello, I have never posted on the forum before, so I hope this post doesn't break any rules. I am planning to set up a one-shot and want to use sprites for the characters. I had the idea of making them rotate and change direction, which led me to this other post that used a rollable table and an API script. <a href="https://app.roll20.net/forum/post/2778056/script-token-face-change-based-on-rotation" rel="nofollow">https://app.roll20.net/forum/post/2778056/script-token-face-change-based-on-rotation</a> I have never used the API before, and loading this script into the game only produced errors. Would anyone be able to help me make this work and explain the best way to set this up? I'm sorry if this is a big ask, but I'd really appreciate any guidance! I was also debating messaging the creator, but it's been years since they last interacted on here. Thank you!!
1765017184
Kurt J.
Pro
API Scripter
This isn't exactly what you are asking (it doesn't look at the rotation value on the token object) but this can be done with 1 line of ScriptCards code if you use that Mod script. (The second line just hides the banner output). You would select the token and run the macro (token action or general macro) and be prompted for a facing. The multisided token is assumed to be in the order Front, Back, Left, Right for the images - you can change the numbers below to correspond to what you have). !script {{ &nbsp; --#hidecard|1 &nbsp; --!t:@{selected|token_id}|currentside:?{Select Facing|Front,0|Back,1|Left,2|Right,3} }} If you really want to base it on the rotation of the token, you could make this a ScriptCards trigger that would watch for token rotation. It would take a few extra lines of code, but still be pretty simple.
1765031168
timmaugh
Forum Champion
API Scripter
What error did you get when you installed that script, Cina? I read through the code, and nothing stands out as should be causing an error... Another question ... did you add the "Rotatable?" attribute to the character and fill it with the value "yes"?
1765036930

Edited 1765036997
Hi Cina, I noticed that you don't have a "Pro" tag after your name. Do you have a Pro or Elite account? Only those types of accounts&nbsp; an use API scripts (MODs), so that might explain the error that you're seeing, Cina said: Hello, I have never posted on the forum before, so I hope this post doesn't break any rules. I am planning to set up a one-shot and want to use sprites for the characters. I had the idea of making them rotate and change direction, which led me to this other post that used a rollable table and an API script. <a href="https://app.roll20.net/forum/post/2778056/script-token-face-change-based-on-rotation" rel="nofollow">https://app.roll20.net/forum/post/2778056/script-token-face-change-based-on-rotation</a> I have never used the API before, and loading this script into the game only produced errors. Would anyone be able to help me make this work and explain the best way to set this up? I'm sorry if this is a big ask, but I'd really appreciate any guidance! I was also debating messaging the creator, but it's been years since they last interacted on here. Thank you!!
Ohhh, I see. But yes, this is exactly what I was looking for! And I would also prefer it if it were based on rotation so that they wouldn't have to issue commands. Overall, making it a smoother experience. What would those extra lines of code look like if you don't mind me asking? Kurt J. said: This isn't exactly what you are asking (it doesn't look at the rotation value on the token object) but this can be done with 1 line of ScriptCards code if you use that Mod script. (The second line just hides the banner output). You would select the token and run the macro (token action or general macro) and be prompted for a facing. The multisided token is assumed to be in the order Front, Back, Left, Right for the images - you can change the numbers below to correspond to what you have). !script {{ &nbsp; --#hidecard|1 &nbsp; --!t:@{selected|token_id}|currentside:?{Select Facing|Front,0|Back,1|Left,2|Right,3} }} If you really want to base it on the rotation of the token, you could make this a ScriptCards trigger that would watch for token rotation. It would take a few extra lines of code, but still be pretty simple.
Please see the screenshot below for the errors I received. As for your question, I am going to say no because I didn't realize there was an attribute for it. I will add that attribute if it is needed for the script to work, but I was just rotating the token manually after clicking it. timmaugh said: What error did you get when you installed that script, Cina? I read through the code, and nothing stands out as should be causing an error... Another question ... did you add the "Rotatable?" attribute to the character and fill it with the value "yes"?
Hello Rick, you are correct that I do not have a Pro or Elite account, but my DM has one. He set up a new campaign for me to use with me as a co-DM, which allows me to use those features within the campaign. Ideally, everything should work, but do you think that would still cause issues? Rick A. said: Hi Cina, I noticed that you don't have a "Pro" tag after your name. Do you have a Pro or Elite account? Only those types of accounts&nbsp; an use API scripts (MODs), so that might explain the error that you're seeing, Cina said: Hello, I have never posted on the forum before, so I hope this post doesn't break any rules. I am planning to set up a one-shot and want to use sprites for the characters. I had the idea of making them rotate and change direction, which led me to this other post that used a rollable table and an API script. <a href="https://app.roll20.net/forum/post/2778056/script-token-face-change-based-on-rotation" rel="nofollow">https://app.roll20.net/forum/post/2778056/script-token-face-change-based-on-rotation</a> I have never used the API before, and loading this script into the game only produced errors. Would anyone be able to help me make this work and explain the best way to set this up? I'm sorry if this is a big ask, but I'd really appreciate any guidance! I was also debating messaging the creator, but it's been years since they last interacted on here. Thank you!!
1765088474
timmaugh
Forum Champion
API Scripter
Hey, Cina... I figured out that error (and a couple others). Here is the reworked script from that link. This works for me in my game: on('change:token:rotation',(obj) =&gt; { &nbsp; &nbsp; let tok = obj; &nbsp; &nbsp; //Unless there is an attribute "Rotateable?" with the current value is set to "Yes" it will not attempt to change token image. &nbsp; &nbsp; if(tok.get('represents') == "" ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (findObjs({ type: 'attribute', characterid: tok.get('represents'), name: 'Rotateable?'})[0] || {get: () =&gt; ''}).get('current').toLowerCase() !== 'yes') { &nbsp; &nbsp; &nbsp; &nbsp; return; &nbsp; &nbsp; } &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; //Make sure token rotation does not exceed 360 for simplification reasons. &nbsp; &nbsp; let rot = ((tok.get('rotation') % 360) + 360) % 360; &nbsp; &nbsp; log(rot); &nbsp; &nbsp; if (rot !== tok.get('rotation')) { &nbsp; &nbsp; &nbsp; &nbsp; tok.set('rotation', rot); &nbsp; &nbsp; }; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; //Make an array of the sides from the token, making them usable by ".set()" so the api &nbsp; &nbsp; //wont complain it's not a thumb or in your library. &nbsp; &nbsp; //log(tok.get("sides")); &nbsp; &nbsp; let sides = tok.get('sides') &nbsp; &nbsp; &nbsp; &nbsp; .split(`|`) &nbsp; &nbsp; &nbsp; &nbsp; .map(s =&gt; s.replace(`%3A`, `:`) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .replace(`%3F`, `?`) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .replace(`med`, `thumb`) &nbsp; &nbsp; &nbsp; &nbsp; ); &nbsp; &nbsp; //Set the image based on 90 degree rotations.&nbsp; &nbsp; &nbsp; if (rot &lt; 90) { tok.set({imgsrc: sides[0], currentSide: 0}); }&nbsp; &nbsp; &nbsp;//Back &nbsp; &nbsp; else if (rot &lt; 180) { tok.set({imgsrc: sides[1], currentSide: 1}); }&nbsp; &nbsp; &nbsp;//Back &nbsp; &nbsp; else if (rot &lt; 270) { tok.set({imgsrc: sides[2], currentSide: 2}); }&nbsp; &nbsp; &nbsp;//Back &nbsp; &nbsp; else { tok.set({imgsrc: sides[3], currentSide: 3}); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Left });
This worked perfectly!!! I should have read through the comments a bit more for the attribute, so that was a good catch. I am a bit curious what caused those errors, though. Thank you so much for your help! timmaugh said: Hey, Cina... I figured out that error (and a couple others). Here is the reworked script from that link. This works for me in my game: on('change:token:rotation',(obj) =&gt; { &nbsp; &nbsp; let tok = obj; &nbsp; &nbsp; //Unless there is an attribute "Rotateable?" with the current value is set to "Yes" it will not attempt to change token image. &nbsp; &nbsp; if(tok.get('represents') == "" ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (findObjs({ type: 'attribute', characterid: tok.get('represents'), name: 'Rotateable?'})[0] || {get: () =&gt; ''}).get('current').toLowerCase() !== 'yes') { &nbsp; &nbsp; &nbsp; &nbsp; return; &nbsp; &nbsp; } &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; //Make sure token rotation does not exceed 360 for simplification reasons. &nbsp; &nbsp; let rot = ((tok.get('rotation') % 360) + 360) % 360; &nbsp; &nbsp; log(rot); &nbsp; &nbsp; if (rot !== tok.get('rotation')) { &nbsp; &nbsp; &nbsp; &nbsp; tok.set('rotation', rot); &nbsp; &nbsp; }; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; //Make an array of the sides from the token, making them usable by ".set()" so the api &nbsp; &nbsp; //wont complain it's not a thumb or in your library. &nbsp; &nbsp; //log(tok.get("sides")); &nbsp; &nbsp; let sides = tok.get('sides') &nbsp; &nbsp; &nbsp; &nbsp; .split(`|`) &nbsp; &nbsp; &nbsp; &nbsp; .map(s =&gt; s.replace(`%3A`, `:`) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .replace(`%3F`, `?`) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .replace(`med`, `thumb`) &nbsp; &nbsp; &nbsp; &nbsp; ); &nbsp; &nbsp; //Set the image based on 90 degree rotations.&nbsp; &nbsp; &nbsp; if (rot &lt; 90) { tok.set({imgsrc: sides[0], currentSide: 0}); }&nbsp; &nbsp; &nbsp;//Back &nbsp; &nbsp; else if (rot &lt; 180) { tok.set({imgsrc: sides[1], currentSide: 1}); }&nbsp; &nbsp; &nbsp;//Back &nbsp; &nbsp; else if (rot &lt; 270) { tok.set({imgsrc: sides[2], currentSide: 2}); }&nbsp; &nbsp; &nbsp;//Back &nbsp; &nbsp; else { tok.set({imgsrc: sides[3], currentSide: 3}); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Left });