js noob here, having a little trouble understanding of how the callback function works for getting the JSON string of the default token for a given character sheet. I eventually want to be able to create an instance of the default token (which in this case will be a rollable table token) on the map. I imagine this last part should be pretty straightforward, assuming I can get the correct object information in the first place. In the code below, I've defined a variable defaultTokenString that I want to eventually contain the parsed JSON string. Since the _defaulttoken property must use a callback function rather than a simple Obj.get("defaultToken"), I am currently assigning the string value in the bolded code block below. I would expect my two log values below to contain the same information. However, only the log within the callback outputs the parsed JSON representing the default token object. My defaultTokenString remains undefined. This remains true if I don't declare it in advance, but wait until the line in which it is assigned. on("ready",function()
{
on("chat:message",function(msg){
if(msg.type=="api" && msg.content.indexOf("!Test")==0 && playerIsGM(msg.playerid))
{
var defaultTokenString = ""
let args = msg.content.split(/\s+--/);
args.shift();
if (args.length >= 1)
{
let inputName = args[0];
let check = findObjs({ _type: "character", name: inputName },{caseInsensitive: true})[0];
if (typeof check == 'undefined')
{
sendChat(msg.who, "Character named \"" + inputName + "\" not found.");
} else {
let chars = findObjs({ _type: "character", name: inputName },{caseInsensitive: true});
defaultTokenString = chars[0].get("_defaulttoken", function(defaultToken) {
JSON.parse(defaultToken);
log('defaultToken = ' + defaultToken);
});
}
log('defaultTokenString = ' + defaultTokenString);
}
}
});
}); I have also tried assigning the string from within the callback: chars[0].get("_defaulttoken", function(defaultToken) {
JSON.parse(defaultToken);
log('defaultToken = ' + defaultToken);
defaultTokenString = defaultToken
}); When I do this, defaultTokenString is no longer undefined, but it instead remains an empty string as if the assignment never took place. What is happening here?