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

Getting A Card's Name From It's ID

1586817485

Edited 1586817513
Spenser S.
Sheet Author
Hey there! Doing some coding for a custom API project and ran into an issue getting a card's name when I have it's ID. I'll detail what I've attempted below- any guidance you have would be much appreciated! So I have a function called findTheirNames that is running a 'for' loop in an attempt to get the name of all of the "Suspect" cards that have been drawn from the deck throughout the game. The cardID's of all of these cards are stored in an array called peopleDrawn. I'm using findObjs to get those IDs from the array and return the card data to a variable called "Card". That part is working-- if I log 'card', it displays the info about the card that's in that position in the array (name, id, photo, etc). When I attempt to extract the name out of the 'card' object using card.name however, it doesn't work. If I log "card.name" to see what the value is, it says it's unidentified. Any idea how to grab the name out of that data?  function findTheirNames(){    for (i = 0; i < (suspectsAmount); i++) {    card = findObjs({ type: 'card', id: peopleDrawn[i] });    leftoverNames [i] = card.name;    log(leftoverNames);    }    };    findTheirNames();
1586820661
The Aaron
Roll20 Production Team
API Scripter
Roll20 Objects are a Proxy to an object in a (near) realtime database called Firebase.  Because of that, Roll20 needs to know when things are accessed or changed so they can keep the database in sync.  The method they used for that is to have accessor and setter functions wrapping private data within the object.  To get a property, you must use the .get() method, to set it, you must use the .set() method.  Here's your code adjusted to use those: function findTheirNames(){ for (i = 0; i < (suspectsAmount); i++) { card = findObjs({ type: 'card', id: peopleDrawn[i] }); leftoverNames [i] = card.get('name'); log(leftoverNames); } }; findTheirNames();
Hey Aaron! Thanks so much for all you do around here, I've literally been learning to code Javascript using your examples from these forums, so it's much appreciated! Unfortunately this is now throwing me an error- I'll post it below, any insight you have would be much appreciated!
1586821188
The Aaron
Roll20 Production Team
API Scripter
Ah, that's because findObjs() returns an array.  I should have read the rest of the code!  Give this a try: function findTheirNames(){ let leftoverNames = []; for (i = 0; i < (suspectsAmount); i++) { let card = findObjs({ type: 'card', id: peopleDrawn[i] })[0]; if(card) { leftoverNames.push(card.get('name')); } log(leftoverNames); } }; findTheirNames();
Thanks so much Aaron! That worked perfectly. Really appreciate it!