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

API getObj doesn't have the correct attribute info?

1594954864

Edited 1595001855
Angelo
API Scripter
EDIT: looks like there is something going on with the createObj("graphic",...) call, as soon as I call it the values on one of the original character's attribute linked to a bar (npc_ac) is cleared out&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _.each(findObjs({type:'attribute', characterid: targetCharId}), (attr) =&gt; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(attr.get("name") == "npc_ac") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("found ac = " + JSON.stringify(attr)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; // create a copy of the character token &nbsp; &nbsp; &nbsp; &nbsp; let tokenLinks = [null, null, null]; &nbsp; &nbsp; &nbsp; &nbsp; let newCharacterToken = newCharacterTokenData ? createObj("graphic", newCharacterTokenData) : null; &nbsp; &nbsp; &nbsp; &nbsp; _.each(findObjs({type:'attribute', characterid: targetCharId}), (attr) =&gt; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(attr.get("name") == "npc_ac") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("found ac again = " + JSON.stringify(attr)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; }); outputs: "found ac = {\"name\":\"npc_ac\",\"current\":\"11\",\"max\":\"\",\"_id\":\"-MCQdfD2mNThEAdkbqc5\",\"_type\":\"attribute\",\"_characterid\":\"-MCQdevSr10_7N3vSvE6\"}" "found ac again = {\"name\":\"npc_ac\",\"current\":\"\",\"max\":\"\",\"_id\":\"-MCQdfD2mNThEAdkbqc5\",\"_type\":\"attribute\",\"_characterid\":\"-MCQdevSr10_7N3vSvE6\"}" ORIGINAL POST: I have a snipped of code with getObj that is returning some weird data. In the test code i am searching for the npc_ac attribute with a findObj and i log it, i can see that the current value is 12. then i try to find it with a getObj, and when i log it i can see that the current is now empty for some reason. this is my output&nbsp; "start search" "target token = {\"width\":70,\"height\":70,\"bar1_value\":\"19\",\"bar1_max\":\"19\",\"bar2_link\":\"-MCQbXRAUJ-5YHhRb-wZ\",\"represents\":\"-MCQbX-y_U8D5UJnKBm5\",\"name\":\"Ape\",\"imgsrc\":\"<a href="https://s3.amazonaws.com/files.d20.io/images/28449849/MXql7HghIay1nFPZU_PJSg/thumb.png\" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/28449849/MXql7HghIay1nFPZU_PJSg/thumb.png\</a>"}" "new token = {\"width\":70,\"height\":70,\"bar1_value\":\"19\",\"bar1_max\":\"19\",\"bar2_link\":\"-MCQbXRAUJ-5YHhRb-wZ\",\"represents\":\"-MCQbX-y_U8D5UJnKBm5\",\"name\":\"Ape\",\"imgsrc\":\"<a href="https://s3.amazonaws.com/files.d20.io/images/28449849/MXql7HghIay1nFPZU_PJSg/thumb.png?9043476\&quot;,\&quot;_pageid\&quot;:\&quot;-JvyFmkcZSMhT1TAQGx_\&quot;,\&quot;layer\&quot;:\&quot;gmlayer\&quot;,\&quot;left\&quot;:-500,\&quot;top\&quot;:-500" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/28449849/MXql7HghIay1nFPZU_PJSg/thumb.png?9043476\",\"_pageid\":\"-JvyFmkcZSMhT1TAQGx_\",\"layer\":\"gmlayer\",\"left\":-500,\"top\":-500</a>}" "targetCharacter = {\"name\":\"Ape\",\"bio\":1594969956477,\"gmnotes\":\"\",\"_defaulttoken\":1594969960551,\"archived\":false,\"inplayerjournals\":\"\",\"controlledby\":\"\",\"_id\":\"-MCQbX-y_U8D5UJnKBm5\",\"_type\":\"character\",\"avatar\":\"<a href="https://s3.amazonaws.com/files.d20.io/images/28449849/MXql7HghIay1nFPZU_PJSg/original.png\" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/28449849/MXql7HghIay1nFPZU_PJSg/original.png\</a>"}" "newCharacterData = {\"name\":\"Lavinia - Ape\",\"bio\":1594969956477,\"gmnotes\":\"\",\"archived\":false,\"inplayerjournals\":\"\",\"controlledby\":\"\",\"_type\":\"character\",\"avatar\":\"<a href="https://s3.amazonaws.com/files.d20.io/images/28449849/MXql7HghIay1nFPZU_PJSg/thumb.png?9471735\" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/28449849/MXql7HghIay1nFPZU_PJSg/thumb.png?9471735\</a>"}" "newCharacter = {\"name\":\"Lavinia - Ape\",\"bio\":1594969956477,\"gmnotes\":\"\",\"_defaulttoken\":\"\",\"archived\":false,\"inplayerjournals\":\"\",\"controlledby\":\"\",\"_id\":\"-MCQb_BOW5Qz88PNw7of\",\"_type\":\"character\",\"avatar\":\"<a href="https://s3.amazonaws.com/files.d20.io/images/28449849/MXql7HghIay1nFPZU_PJSg/thumb.png?9471735\" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/28449849/MXql7HghIay1nFPZU_PJSg/thumb.png?9471735\</a>"}" "found ac = {\"name\":\"npc_ac\",\"current\":\"12\",\"max\":\"\",\"_id\":\"-MCQbXRAUJ-5YHhRb-wZ\",\"_type\":\"attribute\",\"_characterid\":\"-MCQbX-y_U8D5UJnKBm5\"}" "get linkData bar1_link" "linkData = " "get linkData bar2_link" "linkData = -MCQbXRAUJ-5YHhRb-wZ" "found link attr = {\"name\":\"npc_ac\",\"current\":\"\",\"max\":\"\",\"_id\":\"-MCQbXRAUJ-5YHhRb-wZ\",\"_type\":\"attribute\",\"_characterid\":\"-MCQbX-y_U8D5UJnKBm5\"}" "saved link data = {\"data\":\"-MCQbXRAUJ-5YHhRb-wZ\",\"name\":\"npc_ac\",\"val\":\"\",\"max\":\"\"}" "get linkData bar3_link" "linkData = " and this is the snippet async duplicateCharacter(targetCharacter, newCharacterName) { if (!targetCharacter) { this.chatError("WildUtils::duplicateCharacter: trying to duplicate an invalid character"); return null; } let targetCharacterName = targetCharacter.get("name"); let targetCharId = targetCharacter.get("_id"); let errorMsgHeader = "WildUtils::duplicateCharacter (" + targetCharacterName + " -&gt; " + newCharacterName + "): "; const jsonObj = (o) =&gt; JSON.parse(JSON.stringify(o)); // find character token let targetCharacterToken = null; let newCharacterTokenData = null; await this.getDefaultToken(targetCharacter).then( (token) =&gt; { targetCharacterToken = token; }); if (targetCharacterToken) { // make a copy of the data and create a new off screen temporary graphic token newCharacterTokenData = jsonObj(targetCharacterToken); newCharacterTokenData.imgsrc = this.getCleanImgsrc(newCharacterTokenData.imgsrc); if(newCharacterTokenData.imgsrc == "") { newCharacterTokenData.imgsrc = this.getCleanImgsrc(targetCharacter.get("avatar")); if(newCharacterTokenData.imgsrc == "") { this.chatError(errorMsgHeader + "cannot find image on either token or avatar; if it's using a marketplace link the image needs to be re-uploaded into the library and set on the target character as either token or avatar image"); newCharacterTokenData = null; return null; } } // graphic token data setup newCharacterTokenData._pageid = Campaign().get("playerpageid"); newCharacterTokenData.layer = "gmlayer"; newCharacterTokenData.left = -500; newCharacterTokenData.top = -500; } // test log("start search"); log("target token = " + JSON.stringify(targetCharacterToken)); log("new token = " + JSON.stringify(newCharacterTokenData)); // create new character log("targetCharacter = " + JSON.stringify(targetCharacter)); let newCharacterData = jsonObj(targetCharacter); delete newCharacterData._defaulttoken; delete newCharacterData._id; newCharacterData.name = newCharacterName; newCharacterData.avatar = this.getCleanImgsrc(targetCharacter.get("avatar")); log("newCharacterData = " + JSON.stringify(newCharacterData)); let newCharacter = createObj('character', newCharacterData); let newCharacterId = newCharacter.get("_id"); log("newCharacter = " + JSON.stringify(newCharacter)); _.each(findObjs({type:'attribute', characterid: targetCharId}), (attr) =&gt; { if(attr.get("name") == "npc_ac") { log("found ac = " + JSON.stringify(attr)); } }); // create a copy of the character token let tokenLinks = [null, null, null]; let newCharacterToken = newCharacterTokenData ? createObj("graphic", newCharacterTokenData) : null; if (newCharacterToken) { newCharacterToken.set("represents", newCharacterId); // cache link info for (let i = 0; i &lt; 3; ++i) { log("get linkData " + "bar" + (i + 1).toString() + "_link"); let linkData = newCharacterToken.get("bar" + (i + 1).toString() + "_link"); log("linkData = " + linkData); if (linkData &amp;&amp; linkData !== "") { //let linkAttrs = findObjs({type:'attribute', _id: linkData, _characterid: targetCharId}); //_.each(linkAttrs, (linkAttr) =&gt; { let linkAttr = getObj("attribute", linkData); if (linkAttr) { let tmpTokenData = {}; tmpTokenData.data = linkData; tmpTokenData.name = linkAttr.get("name"); let val = newCharacterToken.get("bar" + (i + 1).toString() + "_value"); tmpTokenData.val = !_.isUndefined(val) ? val : null; val = newCharacterToken.get("bar" + (i + 1).toString() + "_max"); tmpTokenData.max = !_.isUndefined(val) ? val : null; tokenLinks[i] = tmpTokenData; log("found link attr = " + JSON.stringify(linkAttr)); log("saved link data = " + JSON.stringify(tmpTokenData)); } //}); } } newCharacterToken.remove(); newCharacter.remove(); return null; }
Which part of your code is producing this log? "found link attr {\"name\":\"npc_ac\",\"current\":\"\",\"max\":\"\",\"_id\":\"-MCPgG_P49ngqK20jKYd\",\"_type\":\"attribute\",\"_characterid\":\"-MCPgGImSTY8m4smwyHq\"}" I don't see anything that would produce this.
1594967492

Edited 1594972126
Angelo
API Scripter
sorry i must have removed it by mistake, i have edited the original post with the full example, it's right at the bottom. it's in a function that is duplicating a character (for now returning in the middle of it since some stuff it's broken). Another weird thing is that after the code runs the npc_ac attribute on the original character is modified (cleared up) as well; it doesn't look like other attributes are being affected EDIT: i have actually tried to replace the getObj let linkAttr = getObj("attribute", linkData); with a findObjs let linkAttrs = findObjs({type:'attribute', _id: linkData, _characterid: targetCharId}); _.each(linkAttrs, (linkAttr) =&gt; { }); and i have the same issue, exactly the same output with an empty current
1594972137

Edited 1595002111
Angelo
API Scripter
looks like is something to do with the createObj call, as soon as that is called the value is cleared out &nbsp; &nbsp; &nbsp; &nbsp; _.each(findObjs({type:'attribute', characterid: targetCharId}), (attr) =&gt; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(attr.get("name") == "npc_ac") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("found ac = " + JSON.stringify(attr)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; // create a copy of the character token &nbsp; &nbsp; &nbsp; &nbsp; let tokenLinks = [null, null, null]; &nbsp; &nbsp; &nbsp; &nbsp; let newCharacterToken = newCharacterTokenData ? createObj("graphic", newCharacterTokenData) : null; &nbsp; &nbsp; &nbsp; &nbsp; _.each(findObjs({type:'attribute', characterid: targetCharId}), (attr) =&gt; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(attr.get("name") == "npc_ac") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("found ac again = " + JSON.stringify(attr)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; }); outputs: "found ac = {\"name\":\"npc_ac\",\"current\":\"11\",\"max\":\"\",\"_id\":\"-MCQdfD2mNThEAdkbqc5\",\"_type\":\"attribute\",\"_characterid\":\"-MCQdevSr10_7N3vSvE6\"}" "found ac again = {\"name\":\"npc_ac\",\"current\":\"\",\"max\":\"\",\"_id\":\"-MCQdfD2mNThEAdkbqc5\",\"_type\":\"attribute\",\"_characterid\":\"-MCQdevSr10_7N3vSvE6\"}" full function &nbsp; &nbsp; async duplicateCharacter(targetCharacter, newCharacterName) { &nbsp; &nbsp; &nbsp; &nbsp; if (!targetCharacter) &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.chatError("WildUtils::duplicateCharacter: trying to duplicate an invalid character"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; let targetCharacterName = targetCharacter.get("name"); &nbsp; &nbsp; &nbsp; &nbsp; let targetCharId = targetCharacter.get("_id"); &nbsp; &nbsp; &nbsp; &nbsp; let errorMsgHeader = "WildUtils::duplicateCharacter (" + targetCharacterName + " -&gt; " + newCharacterName + "): "; &nbsp; &nbsp; &nbsp; &nbsp; const jsonObj = (o) =&gt; JSON.parse(JSON.stringify(o)); &nbsp; &nbsp; &nbsp; &nbsp; // find character token &nbsp; &nbsp; &nbsp; &nbsp; let targetCharacterToken = null; &nbsp; &nbsp; &nbsp; &nbsp; let newCharacterTokenData = null; &nbsp; &nbsp; &nbsp; &nbsp; await this.getDefaultToken(targetCharacter).then( (token) =&gt; { targetCharacterToken = token; }); &nbsp; &nbsp; &nbsp; &nbsp; if (targetCharacterToken) &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // make a copy of the data and create a new off screen temporary graphic token &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newCharacterTokenData = jsonObj(targetCharacterToken); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newCharacterTokenData.imgsrc = this.getCleanImgsrc(newCharacterTokenData.imgsrc); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(newCharacterTokenData.imgsrc == "") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newCharacterTokenData.imgsrc = this.getCleanImgsrc(targetCharacter.get("avatar")); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(newCharacterTokenData.imgsrc == "") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.chatError(errorMsgHeader + "cannot find image on either token or avatar; if it's using a marketplace link the image needs to be re-uploaded into the library and set on the target character as either token or avatar image"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newCharacterTokenData = null; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // graphic token data setup &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete newCharacterTokenData._id; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newCharacterTokenData._pageid = Campaign().get("playerpageid"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newCharacterTokenData.layer = "gmlayer"; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newCharacterTokenData.left = -500; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newCharacterTokenData.top = -500; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; // test &nbsp; &nbsp; &nbsp; &nbsp; log("start search"); &nbsp; &nbsp; &nbsp; &nbsp; log("target token = " + JSON.stringify(targetCharacterToken)); &nbsp; &nbsp; &nbsp; &nbsp; log("new token = " + JSON.stringify(newCharacterTokenData)); &nbsp; &nbsp; &nbsp; &nbsp; // create new character &nbsp; &nbsp; &nbsp; &nbsp; log("targetCharacter = " + JSON.stringify(targetCharacter)); &nbsp; &nbsp; &nbsp; &nbsp; let newCharacterData = jsonObj(targetCharacter); &nbsp; &nbsp; &nbsp; &nbsp; delete newCharacterData._defaulttoken; &nbsp; &nbsp; &nbsp; &nbsp; delete newCharacterData._id; &nbsp; &nbsp; &nbsp; &nbsp; newCharacterData.name = newCharacterName; &nbsp; &nbsp; &nbsp; &nbsp; newCharacterData.avatar = this.getCleanImgsrc(targetCharacter.get("avatar")); &nbsp; &nbsp; &nbsp; &nbsp; log("newCharacterData = " + JSON.stringify(newCharacterData)); &nbsp; &nbsp; &nbsp; &nbsp; let newCharacter = createObj('character', newCharacterData); &nbsp; &nbsp; &nbsp; &nbsp; let newCharacterId = newCharacter.get("_id"); &nbsp; &nbsp; &nbsp; &nbsp; log("newCharacter = " + JSON.stringify(newCharacter)); &nbsp; &nbsp; &nbsp; &nbsp; _.each(findObjs({type:'attribute', characterid: targetCharId}), (attr) =&gt; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(attr.get("name") == "npc_ac") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("found ac = " + JSON.stringify(attr)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; // create a copy of the character token &nbsp; &nbsp; &nbsp; &nbsp; let tokenLinks = [null, null, null]; &nbsp; &nbsp; &nbsp; &nbsp; let newCharacterToken = null; &nbsp; &nbsp; &nbsp; &nbsp; if (newCharacterTokenData) &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newCharacterTokenData.represents = newCharacterId; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newCharacterToken = createObj("graphic", newCharacterTokenData); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("newCharacterToken = " + JSON.stringify(newCharacterToken)); &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; _.each(findObjs({type:'attribute', characterid: targetCharId}), (attr) =&gt; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(attr.get("name") == "npc_ac") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("found ac again = " + JSON.stringify(attr)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; }); ....
1595012622

Edited 1595013483
Angelo
API Scripter
found the issue, i think it's a bug? I do have a workaround for it in my code but i think this should probably still be addressed so i'll leave the details here. when getting the defaulttoken data for some monsters in the monster manual (e.g. Ape, not sure if it happens with all of them) with something like character.get('defaulttoken', function(defaultToken) { i get this: "target token = {\"width\":70,\"height\":70,\"bar1_value\":\"19\",\"bar1_max\":\"19\",\"bar2_link\":\"-MCT6HYr7fRX3ymUI_EK\",\"represents\":\"-MCT6HJ9odwOMyQGhcOL\",\"name\":\"Ape\",\"imgsrc\":\"<a href="https://s3.amazonaws.com/files.d20.io/images/28449849/MXql7HghIay1nFPZU_PJSg/thumb.png\" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/28449849/MXql7HghIay1nFPZU_PJSg/thumb.png\</a>"}" Now the bar2_link is set but the value/max are not, which means when i go and call this newCharacterToken = createObj("graphic", newCharacterTokenData); with that data the values for the attribute linked to bar2 are cleared since there is no value/max set.
if you deited it it could be that
1595013021

Edited 1595013045
Angelo
API Scripter
Allen S. said: if you deited it it could be that not sure what you mean? i am not editing the original token at all, you can simply try to drag Ape from the compendium into a page and log out the defaulttoken and you should see the same