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 .
×

Cannot access graphic properties without needless parsing

I have an errant question about my xp script that I wrote to help manage my game, here's a snippet of the bug in question: var players = findObjs({ _pageid: Campaign().get("playerpageid"), _type: "graphic", }); var characters = new Array(); _.each(players, function(obj){ log(obj); //Works fine, logs entire JavaScript object-type log(obj["represents"]); //Returns 'undefined; }); The loop properly cycles through all the graphics, and properly logs out the entire object with all the needed properties, but when I try and access the 'represents' property, it returns undefined, which I can then work around with this snippet of code: var players = findObjs({ _pageid: Campaign().get("playerpageid"), _type: "graphic", }); var characters = new Array(); _.each(players, function(obj){ log(obj); //Same as before log(JSON.parse(JSON.stringify(obj))["represents"]); //Finally returns property as string }); All I want to know is if anyone else has a clue as to why I have to write in this awkward work-around of turning an already working object into a JSON string, and then turning the JSON back into the same object so I can access it.
1559269852
The Aaron
Roll20 Production Team
API Scripter
Because the Roll20 objects in the API are wrappers around Firebase objects.  You must access the Firebase properties with the .get() and .set() functions.  These allow fetching the data from Firebase, and updating Firebase with the data when it changes. (When the API was implemented, properties that triggered a function were not as easy to deal with.) This should do what you're looking at: let players = findObjs({ pageid: Campaign().get("playerpageid"), type: "graphic", }); let characters = []; players.forEach(t=>{ let c = getObj('character',t.get('represents')); if(c){ characters.push(c); } });
Thank you so much for the quick and insightful reply! Also, I really appreciate the TokenMod script you made, cheers!
1559270698
The Aaron
Roll20 Production Team
API Scripter
No problem!  We love helping here on the forum, definitely bring on more questions if you have them. =D