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 gmnotes via API returns undefined?

I was attempting to adjust a script that parses characters to use the linked character GM notes instead of the token GM notes (has to do with how they're being exported/imported) and ran into an issue accessing a character's gmnotes attribute. When I attempt to reference it via a callback as described in the API reference docs, I get back "undefined". It looks like it's going after a unique ID, but I can't figure out what to use to parse it. Can anyone figure out what I'm doing wrong here? I saw something about a year ago saying there were issues with this being worked on but nothing since... Example code: getStatblock = function(token){ var character = getObj("character", token.get("represents")); log(character) //this logs the character object as expected, and what looks like an ID int for gmnotes var statblock = character.get("gmnotes", function(gmnotes){ log(gmnotes) //this logs undefined }); }
1448786469
The Aaron
Pro
API Scripter
There is some sort of a race condition with gmnotes. I've dug into it in the past, but not found the reason that I remember. I'll try your code tomorrow and see if I can make it work. 
Riley recently changed the bio and gmnotes stuff to be asynchronous, meaning you have to make it a function inside the get or something like that. I have something somewhere for that...
Was eventually able to figure it out, there was a race condition going on unrelated to this function (which does work correctly now that the other issue has been fixed). Essentially another part of the code was wiping the contents of the character so it could be rebuilt by the import function. By the time the async call succeeded the character was empty and waiting to be filled up with the results of the async call... just had to reshuffle things a bit and it works like a charm!
1448894388
The Aaron
Pro
API Scripter
Ah, interesting!  You can use a closure to avoid that problem if you run into a valid case where you need to preserve a variable's state for some async operation (Often you see this inside a loop where the variable in question is the one being looped across): getStatblock = function(token){ var character = getObj("character", token.get("represents")); log(character) //this logs the character object as expected, and what looks like an ID int for gmnotes // A closure (sometimes called an Immediately Invoked Function Expression (IIFE) ) creates a private scope. Here the argument c is in the closure, so won't be changed. (function(c){ c.get("gmnotes", function(gmnotes){ log(gmnotes) //this logs undefined })(character); // The closure is invoked with character setting the initial value of c. character = undefined; // Character changes, but c will still be what it was inside the closure above. }
Ah, interesting! I handled it by removing the wipe and build function calls from their current locations into the success callback of .get(), which ensured that they weren't attempting to process the character at all until the character's data had been successfully obtained.