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

API script that "chaches" each token hp independently from other tokens sharing the same character sheet.

I had a friend write a script for me so that tokens sharing the same npc sheet don't affect other tokens sharing the same sheet. It somewhat works however there are several issues that I can't seem to figure out with my sub-basic understanding of javascript. When placing two or more of the same token the initial deduction of hp from one token works flawlessly. Then if the same token takes damage or heals it sets every other token to the same hp as the damaged token was at when its hp changed, as if the other tokens had taken the initial damage numbers. If another token takes damage it resets the other tokens, including the one that initially took the damage to full HP, this also functions the same for adding health. I can't figure out exactly what the issue is but it seems like the script is on the right track. I'm also using Aura/Tint HealthColors and there seems to be some strange interactions there as the involving the auto death markers as if you remove the X token marker from a token that shouldn't be dead it will mark the other tokens as being 0 hp. Moving one token sets its marker and returns all the other tokens to full hp. Am I just banging my head against the wall for no reason or is there a way this could work? Thanks in advance for all you guys do here in the API community. on('ready', function() {     // Cache the HP of tokens independently     let tokenHP = {};     // When a graphic (token) changes     on('change:graphic', function(obj, prev) {         // Ensure the token represents a character and has bar1 (HP) value         if (obj.get('represents') && obj.get('bar1_value') !== undefined) {             let tokenID = obj.get('id');             let charID = obj.get('represents');             let currentHP = obj.get('bar1_value');             // Initialize or update the tokenHP cache             if (!tokenHP[tokenID]) {                 tokenHP[tokenID] = currentHP;             } else {                 let cachedHP = tokenHP[tokenID];                 if (cachedHP !== currentHP) {                     // HP has changed, update cache                     tokenHP[tokenID] = currentHP;                     // Apply the change only to this token and not others sharing the same character sheet                     let allTokens = findObjs({ type: 'graphic', represents: charID });                     allTokens.forEach(function(token) {                         if (token.id !== tokenID) {                             token.set('bar1_value', cachedHP);                         }                     });                 }             }         }     }); });
1720715854

Edited 1720716039
Gauss
Forum Champion
Hi Beaded Pancake,  This should not be necessary as the problem you are describing (tokens affecting other token's HP) is the result of incorrectly setting up the token.  When setting up a token that is not unique make sure you do not link the HP bar to the Character. Only set the values.  Alternately, link, then immediately unlink.  Here is an image I made for correctly assigning HPs to a non-unique Character.  1. Edit the token 2. Assign the HP, then set it to None 3. Click Open Character Sheet 4. Save Settings 5. Edit Character 6. With the token selected, click Use Selected Token  Finally, Save Changes
I know that's one way to do it but I was hoping to be able to not mess with token settings each time I added a different kind creature into the game. I'm guessing it's just not really possible.
1720716563

Edited 1720716640
Gauss
Forum Champion
Bearded Pancake said: I know that's one way to do it but I was hoping to be able to not mess with token settings each time I added a different kind creature into the game. I'm guessing it's just not really possible. Could you elaborate on how you wouldn't be messing with token settings when you add new creatures into the game? That is a much broader issue than the token HP issue.  Setting up the token is a basic step to adding a creature to the Journal tab. (Unless dragging/dropping from the Compendium)
1720716863

Edited 1720717018
I usually just drag and drop from the compendium. When I create a homebrew creature I do like you showed above and unlink the token from the character sheet. I guess my desire was to not have to mess with compendium creatures when dragging and dropping them into a game because sometimes I don't know ahead of time that I'm going to have to utilize them and then end up having to pause the session for a moment while I set them up.
Hey Bearded Pancake, Which game are you playing where dragging from the compendium has the token's linked. When I drag from the compendium I get an unlinked token setup with the values I set in my game settings. For example: So when I dragged the Orc from the compendium the token had the max HP values in the bar1 but bar1 was not linked to an attribute. This matches my Game Settings for default tokens.
1720718094
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
compendium creatures shouldn't be coming in with their hp linked to the character sheet. If some are those are bugs and should be reported to Roll20.
1720718383

Edited 1720718700
Huh.....well I just tried dragging a new creature to a map and they aren't linked. I have no idea what happened now because I was dragging stuff from the 5e compendium and it was linking but I never changed any game settings to stop that. Guess I need to double check my creatures so I can report those issues. Now none of the ones I've had issues with in the past are coming up linked. I guess I was trying to fix a problem that's already been fixed. Sorry for the trouble guys.
1720721373
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Bearded Pancake said: Huh.....well I just tried dragging a new creature to a map and they aren't linked. I have no idea what happened now because I was dragging stuff from the 5e compendium and it was linking but I never changed any game settings to stop that. Guess I need to double check my creatures so I can report those issues. Now none of the ones I've had issues with in the past are coming up linked. I guess I was trying to fix a problem that's already been fixed. Sorry for the trouble guys. It's entirely possible that when you dragged in a creature from the compendium, you were dragging in a creature that already exists in your game. Compendium drags will not overwrite local copies (to preserve homebrew modifications). If you existing creatures were already linked, a "compendium drag" would place an instance of one of your local linked token/creature combos.
Bearded Pancake said: Huh.....well I just tried dragging a new creature to a map and they aren't linked. I have no idea what happened now because I was dragging stuff from the 5e compendium and it was linking but I never changed any game settings to stop that. Guess I need to double check my creatures so I can report those issues. Since you have a Pro subscription, you may want to create a TokenMod macro for default token settings. Here's an example of one to set up 'mook' NPCs: !?{Token Name?|@{selected|character_name}} !{Add Number?|Yes, %%NUMBERED%%|No,""} !?{Darkvision?|Yes,has_night_vision|No,} !?{Darkvision Distance?|60} !?{Passive Perception?|@{selected|passive_wisdom}} !?{Size?|Medium/Small,1|Large,2|Tiny,0.5|Huge,3|Gargantuan,4} !setattr {{   --silent   --sel   --replace   --hp|''   --passive_wisdom|?{Passive Perception?}   --advantagetoggle|'{{query=1}} {{normal=1}} {{r2=\lbrak\lbrak0d20'   --rtype|'\at{advantagetoggle}'   --wtype|\ques{Whisper\ques\|Public Roll,\|Whisper Roll,/w gm }|    --npc_name|\at{selected\|token_name} }} !delattr {{   --silent   --sel    --npc_name_flag    --dtype }} !token-mod {{ --on showname showplayers_name showplayers_aura1 ?{Darkvision?} --off playersedit_name playersedit_bar1 playersedit_bar2 playersedit_bar3 playersedit_aura1 playersedit_aura2 showplayers_bar1 showplayers_bar2 showplayers_bar3 showplayers_aura2 --set name|"?{Token Name?} %%NUMBERED%%" controlledby|= bar1_link|  bar2_link|npc_ac bar2_max|  bar3_link|passive_wisdom bar3_max|  bar3_current|?{Passive Perception?} aura2_color|#0000ff aura2_radius|  night_vision_distance|?{Darkvision Distance?} scale|?{Size?}u bar_location|overlap_bottom compact_bar|on defaulttoken }} /w gm Mook Token Full Setup for @{selected|character_name} !token-mod --set name|"?{Token Name?}" bar1|@{selected|hp|max} I'm using several Mod scripts, including  TokenMod ,   ChatSetAttr ,   MonsterHitDice , and   TokenNameNumber .
keithcurtis said: Bearded Pancake said: Huh.....well I just tried dragging a new creature to a map and they aren't linked. I have no idea what happened now because I was dragging stuff from the 5e compendium and it was linking but I never changed any game settings to stop that. Guess I need to double check my creatures so I can report those issues. Now none of the ones I've had issues with in the past are coming up linked. I guess I was trying to fix a problem that's already been fixed. Sorry for the trouble guys. It's entirely possible that when you dragged in a creature from the compendium, you were dragging in a creature that already exists in your game. Compendium drags will not overwrite local copies (to preserve homebrew modifications). If you existing creatures were already linked, a "compendium drag" would place an instance of one of your local linked token/creature combos. That's entirely possible. I have no idea what I did different. I tried pulling creatures I had issues with in a past game and they worked perfectly fine in this one. I've had issues with creatures that I've never used before and dragged straight from the compendium but it appears I'm no longer having issues. The last time was about 2 months ago that I used a compendium creature that I hadn't already used and corrected the issue manually. But as long as it working now I won't stress about it too much. It very well could have somehow been something I was doing. Jarren said: Bearded Pancake said: Huh.....well I just tried dragging a new creature to a map and they aren't linked. I have no idea what happened now because I was dragging stuff from the 5e compendium and it was linking but I never changed any game settings to stop that. Guess I need to double check my creatures so I can report those issues. Since you have a Pro subscription, you may want to create a TokenMod macro for default token settings. Here's an example of one to set up 'mook' NPCs: !?{Token Name?|@{selected|character_name}} !{Add Number?|Yes, %%NUMBERED%%|No,""} !?{Darkvision?|Yes,has_night_vision|No,} !?{Darkvision Distance?|60} !?{Passive Perception?|@{selected|passive_wisdom}} !?{Size?|Medium/Small,1|Large,2|Tiny,0.5|Huge,3|Gargantuan,4} !setattr {{   --silent   --sel   --replace   --hp|''   --passive_wisdom|?{Passive Perception?}   --advantagetoggle|'{{query=1}} {{normal=1}} {{r2=\lbrak\lbrak0d20'   --rtype|'\at{advantagetoggle}'   --wtype|\ques{Whisper\ques\|Public Roll,\|Whisper Roll,/w gm }|    --npc_name|\at{selected\|token_name} }} !delattr {{   --silent   --sel    --npc_name_flag    --dtype }} !token-mod {{ --on showname showplayers_name showplayers_aura1 ?{Darkvision?} --off playersedit_name playersedit_bar1 playersedit_bar2 playersedit_bar3 playersedit_aura1 playersedit_aura2 showplayers_bar1 showplayers_bar2 showplayers_bar3 showplayers_aura2 --set name|"?{Token Name?} %%NUMBERED%%" controlledby|= bar1_link|  bar2_link|npc_ac bar2_max|  bar3_link|passive_wisdom bar3_max|  bar3_current|?{Passive Perception?} aura2_color|#0000ff aura2_radius|  night_vision_distance|?{Darkvision Distance?} scale|?{Size?}u bar_location|overlap_bottom compact_bar|on defaulttoken }} /w gm Mook Token Full Setup for @{selected|character_name} !token-mod --set name|"?{Token Name?}" bar1|@{selected|hp|max} I'm using several Mod scripts, including  TokenMod ,   ChatSetAttr ,   MonsterHitDice , and   TokenNameNumber . I'll have to look into those and see about possibly setting them up.