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] Check if Token is Player

1445525916

Edited 1445547271
Hello Everyone! Kinda new here (especially to the forums!), so if this question has already been asked my apologies. Onto the question! Is there a simple way to tell if a token is controlled by a player? A use case example would be the " Bloodied and Dead Status Markers ". In it, a token get a red X if their health (bar) reaches zero or lower. In my game though, I'd rather make it only effect the monster token's, as the players don't die but instead go "unconscious". Afterwords, I could also add an "If Else" rule that adds a different marker on my player tokens instead of the red X. Any Idea how to write this one out? Thanks! TLDR: Check if token is a player before adding markers. -------------- Update! After a little bit of help from DXWarlock & The Aaron , I was able to find my answer! Below you'll see the finished code. It's just a simple script based off " Bloodied and Dead Status Markers ", but now it has a nested IF statement that quickly checks if the selected token is a "Player" or "Monster"! It should be easy to copy and change things around enough now to change it more to your personal preference, but for now I'm happy with it! You can find the different names for the status icons here ! (Thanks Aaron!) Let us know if you find any bugs or have any idea's to make it even better! Thanks again for everyone's help! // Original: Bloodied and Dead Status Markers.js // Github:&nbsp;&nbsp; <a href="https://github.com/Roll20/roll20-api-scripts/tree" rel="nofollow">https://github.com/Roll20/roll20-api-scripts/tree</a>... // by:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Ken Bauer // Contact:&nbsp; <a href="https://github.com/Roll20/roll20-api-scripts/tree" rel="nofollow">https://github.com/Roll20/roll20-api-scripts/tree</a>... // Special thanks too The Aaron & DXWarlock for changes on("change:graphic", function(obj) { &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; //if bar is empty, exit script &nbsp;&nbsp;&nbsp; if(obj.get("bar3_max") === "") return; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; //if bar is 0 or less &nbsp;&nbsp;&nbsp; if(obj.get("bar3_value") &lt;= 0) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Set Token data onto oCharacter &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Set Monster or Player onto Type &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var oCharacter = getObj('character', obj.get("_represents")); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var type = (oCharacter.get("controlledby") === "") ? 'Monster' : 'Player'; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Check if Token is a Monster &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(type == 'Monster') { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //add dead status onto token &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj.set("status_dead", true); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Check if Token is a Player &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(type == 'Player') { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //add sleepy status onto token &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj.set("status_sleepy", true); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; //if anything else &nbsp;&nbsp;&nbsp; else { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //remove sleepy status onto token &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj.set("status_sleepy", false); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //remove dead status onto token &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj.set("status_dead", false); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp; });
1445526613
The Aaron
Pro
API Scripter
Sure! &nbsp;you just need to check the contents of controlledby &nbsp;(See:&nbsp; <a href="https://wiki.roll20.net/API:Objects#Graphic_.28Tok" rel="nofollow">https://wiki.roll20.net/API:Objects#Graphic_.28Tok</a>... Something like this should be completely thorough: var players=token.get('controlledby').split(/,/); if( _.contains('all',players) || _.reject(players,playerIsGM).length&nbsp;) { &nbsp; // Either everyone can control the token ('all') or at least one player that is not a GM can control the token } but you could just check if the field is not empty. ( Warning: I'm typing this from memory, so the code might have a bug in it, but the idea is sound.)
1445526754

Edited 1445526939
DXWarlock
Sheet Author
API Scripter
edit: Aaron beat me to it..was typing this as he replied...Aaron strikes again! You can check if its controlled by someone. That usually means its a player. (unless you set all monsters to be controlled by GM, its not needed but Ive seen it done, then you will need to check if its a GM) var oCharacter = getObj('character', obj.get("_represents")); var type = (oCharacter.get("controlledby") === "") ? 'Monster' : 'Player'; if(type == 'Monster') { do stuff } if(type == 'Player') { do other stuff }
Thanks for the help! ^_^ I had a feeling it had something to do with "controlledby", but I've always been bad at finding exact syn-texts when it comes to coding. I'll try these out real quick and let you know If I get them working! Thanks again!
1445540079
Gold
Forum Champion
I have the same use-case in my game, so I would like to try your upgrade version of this bloodied-dead API, if you can make & release a working version. &nbsp;I would like the red-dot on the Player's Token if they are low on HP, but not the Red X on Player's Token if they reach 0HP because it should just be unconscious not dead. &nbsp;I want the red X to continue working for non-Player (monster) tokens when they reach zero. Marked & following this thread
Took me a bit longer then I thought, but I think I got it! Ended up using more of DXWarlock code, but many thanks to The Aaron as well for the help! I'll be updating my original post with the code so that it's easy to find for new people!
1445570739
Lithl
Pro
Sheet Author
API Scripter
The Aaron said: Sure! &nbsp;you just need to check the contents of controlledby &nbsp;(See:&nbsp; <a href="https://wiki.roll20.net/API:Objects#Graphic_.28Tok" rel="nofollow">https://wiki.roll20.net/API:Objects#Graphic_.28Tok</a>... Something like this should be completely thorough: var players=token.get('controlledby').split(/,/); if( _.contains('all',players) || _.reject(players,playerIsGM).length&nbsp;) { &nbsp; // Either everyone can control the token ('all') or at least one player that is not a GM can control the token } but you could just check if the field is not empty. ( Warning: I'm typing this from memory, so the code might have a bug in it, but the idea is sound.) IIRC, if a token is set to representing a character and the player has control of that character, the player can control the token even if controlledby is empty. The controlledby property doesn't cascade from characters to graphics, so to be sure, you'd need to check represents on the graphic and the controlledby of the character represented, as well.
1445571490
The Aaron
Pro
API Scripter
Ah, good point!