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

[Help] [API] Script to 'transfer' Bar 1 values to Bar 3 and vice versa

1658067036

Edited 1658067244
Hi! I'm pretty new to these API mods and have 0 experience with JavaScript but I'm sure this must be possible...I just can't figure out how to do it!&nbsp; I'd like a script that allows me to 'transfer' the bar values of selected tokens from the red bar3 to the green bar1. This is to simulate the ally or foe health bar. I would like to also be able to swap back from the green bar to the red bar. I only ever want one bar visible and I don't want the number value to be present in either.&nbsp; Example use - My players tame a once enemy wolf. The wolf token is currenly 7/7 health in bar3 (red) and I would like to press a macro button that will set the bar1 (green) value to 7/7 and remove the bar3 values completly. Another player hits the wolf and the wolf takes 4 damage and also turns foe again. I press the macro again and bar3 values become 3/7 while bar1 values are removed completly. I found this post&nbsp; <a href="https://app.roll20.net/forum/post/6613528/switching-bar-1-and-bar-3-make-red-be-hp-green-ac-and-blue-be-nothing" rel="nofollow">https://app.roll20.net/forum/post/6613528/switching-bar-1-and-bar-3-make-red-be-hp-green-ac-and-blue-be-nothing</a> &nbsp;which is almost exactly what I want only I want to be able to swap back and forth freely only on selected tokens (mass change would be ideal but not sure this would be possible). Would anyone be able to help out with this? Thanks!!
1658072788
The Aaron
Roll20 Production Team
API Scripter
You could do that on a 1-off basis with TokenMod (because you'd have to specify the values with @{selected|...} references, so couldn't do more than one token at a time), but easier to just write a custom Mod Script for that. Command: !swap-bars 1 3 Code: on('ready',()=&gt;{ on('chat:message',msg=&gt;{ if('api'===msg.type &amp;&amp; /^!swap-bars(\b\s|$)/i.test(msg.content) &amp;&amp; playerIsGM(msg.playerid)){ let who = (getObj('player',msg.playerid)||{get:()=&gt;'API'}).get('_displayname'); let [first,second] = msg.content.split(/\s+/).slice(1,3).map(n=&gt;parseInt(n)); if([1,2,3].includes(first) &amp;&amp; [1,2,3].includes(second)){ (msg.selected || []) .map(o=&gt;getObj('graphic',o._id)) .filter(g=&gt;undefined !== g) .forEach(t=&gt; t.set({ [`bar${second}_value`]: t.get(`bar${first}_value`), [`bar${second}_max`]: t.get(`bar${first}_max`), [`bar${second}_link`]: t.get(`bar${first}_link`), [`bar${first}_value`]: t.get(`bar${second}_value`), [`bar${first}_max`]: t.get(`bar${second}_max`), [`bar${first}_link`]: t.get(`bar${second}_link`) })) ; } else { sendChat('',`/w "${who}" &lt;div&gt;Please use the format &lt;code&gt;!swap-bars NUM NUM&lt;/code&gt; where &lt;code&gt;NUM&lt;/code&gt; is &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt;, or &lt;code&gt;3&lt;/code&gt;.&lt;/div&gt;`); } } }); }); This will swap the values, max values, and the link to an attribute. Hope that helps!
1658085750
timmaugh
Pro
API Scripter
Adding SelectManager and Fetch to TokenMod would give the differentiated results for multiple tokens (or one) at once. FETCH LIVES! lol
The Aaron said: You could do that on a 1-off basis with TokenMod (because you'd have to specify the values with @{selected|...} references, so couldn't do more than one token at a time), but easier to just write a custom Mod Script for that. Command: !swap-bars 1 3 Code: on('ready',()=&gt;{ on('chat:message',msg=&gt;{ if('api'===msg.type &amp;&amp; /^!swap-bars(\b\s|$)/i.test(msg.content) &amp;&amp; playerIsGM(msg.playerid)){ let who = (getObj('player',msg.playerid)||{get:()=&gt;'API'}).get('_displayname'); let [first,second] = msg.content.split(/\s+/).slice(1,3).map(n=&gt;parseInt(n)); if([1,2,3].includes(first) &amp;&amp; [1,2,3].includes(second)){ (msg.selected || []) .map(o=&gt;getObj('graphic',o._id)) .filter(g=&gt;undefined !== g) .forEach(t=&gt; t.set({ [`bar${second}_value`]: t.get(`bar${first}_value`), [`bar${second}_max`]: t.get(`bar${first}_max`), [`bar${second}_link`]: t.get(`bar${first}_link`), [`bar${first}_value`]: t.get(`bar${second}_value`), [`bar${first}_max`]: t.get(`bar${second}_max`), [`bar${first}_link`]: t.get(`bar${second}_link`) })) ; } else { sendChat('',`/w "${who}" &lt;div&gt;Please use the format &lt;code&gt;!swap-bars NUM NUM&lt;/code&gt; where &lt;code&gt;NUM&lt;/code&gt; is &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt;, or &lt;code&gt;3&lt;/code&gt;.&lt;/div&gt;`); } } }); }); This will swap the values, max values, and the link to an attribute. Hope that helps! Works perfectly, thanks!!