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

How to synchronize Token mod and Bar Value?

 Hey are there any method to flip token to next side once PC change the attribute?  Currently I was using macro !token-mod --set currentside|+1 --set bar1_value|+1 but wish to change the token image to next side by just changing bar value stated. (ex. if I change 0 to 1, token will flip to next side by 1 time. if I change 0 to 2, token will flip to next side by 2 times. if I change  3 to 0, token will flip to previous side by 3 times.) Thanks in advance!
1619801691
The Aaron
Roll20 Production Team
API Scripter
That could be done with a custom script pretty easily, just not a part of TokenMod.
1619802358

Edited 1619802544
HG
Pro
The Aaron said: That could be done with a custom script pretty easily, just not a part of TokenMod. Sounds amazing! One thing is, I am not really close to those custom scripts and APIs... Any kind advice how to start with it? Thank you very much! I really really appreciate it!
1619812761

Edited 1620050533
The Aaron
Roll20 Production Team
API Scripter
Here ya go: /* global TokenMod */ on('ready',()=>{ const BAR = 1; const BIAS = 1; ///////////////////////////////////////////// const BarProp = `bar${BAR}_value`; const getCleanImgsrc = (imgsrc) => { let parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)([^?]*)(\?[^?]+)?$/); if(parts) { return parts[1]+'thumb'+parts[3]+(parts[4]?parts[4]:`?${Math.round(Math.random()*9999999)}`); } return; }; const handleGraphicChange = (obj,prev) => { if(parseInt(prev[BarProp]) !== parseInt(obj.get(BarProp))){ let sides = decodeURIComponent(obj.get('sides')).split(/\|/); let maxSides = sides.length||0; if(maxSides){ let currSide = parseInt(obj.get('currentSide'))||0; let newSide = Math.max(BIAS,Math.min(maxSides,obj.get(`bar${BAR}_value`)||BIAS)); if((currSide + BIAS) != newSide){ let imgsrc = getCleanImgsrc(sides[newSide-BIAS]); if(imgsrc) { obj.set({ currentSide: newSide, imgsrc }); } } } } }; on('change:graphic', handleGraphicChange); if('undefined' !== typeof TokenMod && TokenMod.ObserveTokenChange){ TokenMod.ObserveTokenChange(handleGraphicChange); } });
The Aaron said: Here ya go: /* global TokenMod */ on('ready',()=>{ const BAR = 1; const BIAS = 1; ///////////////////////////////////////////// const BarProp = `bar${BAR}_value`; const handleGraphicChange = (obj,prev) => { if(parseInt(prev[BarProp]) !== parseInt(obj.get(BarProp))){ let sides = decodeURIComponent(obj.get('sides')).split(/\|/); let maxSides = sides.length||0; if(maxSides){ let currSide = parseInt(obj.get('currentSide'))||0; let newSide = Math.max(BIAS,Math.min(maxSides,obj.get(`bar${BAR}_value`)||BIAS)); if((currSide + BIAS) != newSide){ obj.set({ currentSide: newSide, imgsrc: sides[newSide-BIAS] }); } } } }; on('change:graphic', handleGraphicChange); if('undefined' !== typeof TokenMod && TokenMod.ObserveTokenChange){ TokenMod.ObserveTokenChange(handleGraphicChange); } }); Wow, Thank you very much!!  However I have this error in the sand box, "ERROR: Please use the 'thumb' size for imgsrc properties." how may I fix this? Sorry for bothering you ;D
1620050550
The Aaron
Roll20 Production Team
API Scripter
Ah!  Sorry about that, I've fixed the code above.
Amazing! Thank you very much for your help! Will check with the API soon!!