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

Making an object with character ids in it?

June 09 (8 years ago)

Edited June 09 (8 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Is there a way to use the current value of a variable in the name index part of an object definition? I'd like to make an object that is indexed by character Id and has either 'active' or 'mia' as the value for each character id index.

So Ideally the end result object would look something like this:
PCs = {
-KCJ9856WK: 'active'
-KJ89567UI: 'mia'
...(continuing for however many characters meet the conditions for being put in the object)
}

Thanks,

Scott C.
June 09 (8 years ago)
The Aaron
Pro
API Scripter
Definitely.  You're using the javascript object like a hash.
var PCs = {};

/* gets an active character from somewhere */
PCs[character.id] = 'active';
/* etc */

You can also check if the character is already in the hash:
/* gets PCs from somewhere */
if(_.has(PCs,character.id)){
  /* do stuff with character */
}
You could do if(PCs[character.id]), but that relies on the truthy nature of the value, rather than the existance of the id.
June 09 (8 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Doh, I've got code like that elsewhere in the script that you helped me with a while ago. Apparently I just tried to replicate it incorrectly.
June 09 (8 years ago)
The Aaron
Pro
API Scripter
Happens to the best of us. =D