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

Move to Map Layer on Death with Health Tracker script

Hi all, I have what I hope is a simple question.  I'm using token mod and the following "health tracker" script to help with hp management.  I would like to add a line to move the token to the map layer when it dies.  I'm not sure what the syntax is for this.  Can someone assist?  I feel like it should go right after " changes.status_dead = true;" but I don't know what to put there. on('ready', () => {     const HPBarNum = 3;     const bar = `bar${HPBarNum}_value`;     const max = `bar${HPBarNum}_max`;     const constrainHPBarToMax = (obj) => {         const hpMax = parseInt(obj.get(max),10);         if(!isNaN(hpMax) && 'token' === obj.get('subtype') && !obj.get('isdrawing') ){             let hp = parseInt(obj.get(bar),10);             let changes = {};             if(hp > hpMax) {                 hp = hpMax;                 changes[bar] = hp;                 changes.status_dead = false;             } else if(hp <= 0) {                 hp=0;                 changes[bar] = hp;                 changes.status_dead = true;             } else {                 changes.status_dead = false;             }             obj.set(changes);         }     };     on("change:token", constrainHPBarToMax);     if('undefined' !== typeof TokenMod && TokenMod.ObserveTokenChange){         TokenMod.ObserveTokenChange(constrainHPBarToMax);     }     if('undefined' !== typeof ApplyDamage && ApplyDamage.registerObserver){         ApplyDamage.registerObserver('change',constrainHPBarToMax);     } }); Thanks much in advance!
Just following up on this.  Does anyone have any ideas?
1648880988

Edited 1648880999
Oosh
Sheet Author
API Scripter
I think you just want: changes.layer = "map";
That worked Oosh.  Thank you very much!
1649007233
The Aaron
Roll20 Production Team
API Scripter
You might also want to call toFront() on the token as well. Probably and edge case but if you've added map elements after having added that token on the objects layer, pushing it to the map layer may bury it under the added map elements.
Agreed.  I just solved for that!  It took me a while because I didn't realize at first that I had to bring it "to front" BEFORE I sent it to the map layer.  If I do it the other way it doesn't seem to work as intended.
Also the new script for anyone who would like to use. on('ready', () => {     const HPBarNum = 3;     const bar = `bar${HPBarNum}_value`;     const max = `bar${HPBarNum}_max`;     const constrainHPBarToMax = (obj) => {         const hpMax = parseInt(obj.get(max),10);         if(!isNaN(hpMax) && 'token' === obj.get('subtype') && !obj.get('isdrawing') ){             let hp = parseInt(obj.get(bar),10);             let changes = {};             if(hp > hpMax) {                 hp = hpMax;                 changes[bar] = hp;                 changes.status_dead = false;                 changes.layer = "objects";             } else if(hp <= 0) {                 hp=0;                 changes[bar] = hp;                 changes.status_dead = true;                 changes.order = "tofront"                 changes.layer = "map";             } else {                 changes.status_dead = false;                 changes.layer = "objects";             }             obj.set(changes);         }     };     on("change:token", constrainHPBarToMax);     if('undefined' !== typeof TokenMod && TokenMod.ObserveTokenChange){         TokenMod.ObserveTokenChange(constrainHPBarToMax);     }     if('undefined' !== typeof ApplyDamage && ApplyDamage.registerObserver){         ApplyDamage.registerObserver('change',constrainHPBarToMax);     } });
1649080551

Edited 1649080617
Andreas J.
Forum Champion
Sheet Author
Translator
Here is the above API better formatted. Also added it to <a href="https://wiki.roll20.net/API:Short_Community_Scripts" rel="nofollow">https://wiki.roll20.net/API:Short_Community_Scripts</a> so it's easier to find on ( 'ready' , () =&gt; { const HPBarNum = 3 ; const bar = `bar ${ HPBarNum } _value` ; const max = `bar ${ HPBarNum } _max` ; const constrainHPBarToMax = ( obj ) =&gt; { const hpMax = parseInt ( obj . get ( max ), 10 ); if (! isNaN ( hpMax ) &amp;&amp; 'token' === obj . get ( 'subtype' ) &amp;&amp; ! obj . get ( 'isdrawing' ) ){ let hp = parseInt ( obj . get ( bar ), 10 ); let changes = {}; if ( hp &gt; hpMax ) { hp = hpMax ; changes [ bar ] = hp ; changes . status_dead = false ; changes . layer = "objects" ; } else if ( hp &lt;= 0 ) { hp = 0 ; changes [ bar ] = hp ; changes . status_dead = true ; changes . order = "tofront" changes . layer = "map" ; } else { changes . status_dead = false ; changes . layer = "objects" ; } obj . set ( changes ); } }; on ( "change:token" , constrainHPBarToMax ); if ( 'undefined' !== typeof TokenMod &amp;&amp; TokenMod . ObserveTokenChange ){ TokenMod . ObserveTokenChange ( constrainHPBarToMax ); } if ( 'undefined' !== typeof ApplyDamage &amp;&amp; ApplyDamage . registerObserver ){ ApplyDamage . registerObserver ( 'change' , constrainHPBarToMax ); } });
Hi all, I realized after more testing that this script will not allow you to move a token to the GM layer when it is in play.&nbsp; I modified it to allow you to do so when the HP maximum of the token equals its hit points. (e.g. 25/25).&nbsp; I have updated the script in the link @Andreas J provided. <a href="https://wiki.roll20.net/API:Short_Community_Scripts" rel="nofollow">https://wiki.roll20.net/API:Short_Community_Scripts</a>