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

[HELP] Check for attribute, create if not present...

I need a function that checks for an attribute on a journal linked to a token and if that attribute is not present, it creates it on the token. Is that possible with the API right now?
1373450532
Alex L.
Pro
Sheet Author
basically copied from my 4e helper: <a href="https://gist.github.com/pigalot/5965106" rel="nofollow">https://gist.github.com/pigalot/5965106</a> As an added bonus if u make characterAttributes a global you can just go characterAttributes["attribNameHere"] to access the attribute later.
I'm only looking for one attribute... is there a way to shrink that down to a few lines? Basically, I just want to see if a journal has the TempHP attribute and if not, add it. &nbsp;Otherwise, if I try to run my script and a linked token / journal doesn't have a TempHP attribute, it crashes the api.
var initAttributes = function(id) { //use token.get("represents") to get id or something &nbsp; &nbsp; var theAttribute = "TempHP"; //or whatever the attribute is. &nbsp; &nbsp; var objs = findObjs({ _type: "attribute", _characterid: id, name: theAttribute }); &nbsp; &nbsp; if(objs.length == 0) { &nbsp; &nbsp; &nbsp; &nbsp; createObj("attribute", { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: theAttribute, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current: 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; characterid: id &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; } } I think that should work, but I haven't tested it in-game, so there may be a typo or something &nbsp;in it.
Ah, ok. That makes sense to me now. I can easily fit that in with my existing scripts. I would never have thought to use length to determine if an object (attribute) was present or not.
Bleh... not working: } else { // Token is linked to a journal entry var oCharacter = getObj("character", obj.get("represents")); var oTempHP = findObjs({_type: "attribute", name: "TempHP", _characterid: oCharacter.id})[0]; &nbsp; &nbsp; &nbsp; &nbsp; if (oTempHP.length == 0) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; createObj("attribute", { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: "TempHP", &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current: 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; characterid: oCharacter.id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }; var iTempHP = parseInt(oTempHP.get("current")); } This is the relevant portion of the script. When the script detects an on("change:token") event, it gives me this error if the token is linked to a journal and the journal does not &nbsp;have the TempHP attribute: For reference, the error message generated was:&nbsp; TypeError: Cannot read property 'length' of undefined at Sandbox.&lt;anonymous&gt; (evalmachine.&lt;anonymous&gt;:27:20) at eval ( If I remove the [0] at the end of the var oTempHP line, it gives me this error instead: For reference, the error message generated was:&nbsp; TypeError: Object has no method 'get' at Sandbox.&lt;anonymous&gt; (evalmachine.&lt;anonymous&gt;:34:34) at eval ( The second error happens whether or not the linked journal has the TempHP attribute... but if I put the [0] back and the token linked journal has TempHP on it, than I don't get any errors and the script works.
I also tried if (oTempHP == undefined) and got the second error with and without the [0].
Try if( !(oTempHP.length &gt; 0) ) {...}
Well that got rid of the error messages, but now it's creating a new TempHP attribute each time the token changes, lol. Still get the length error if I remove TempHP from the journal. &gt;_&lt;
Apparently the problem is:&nbsp; var iTempHP = parseInt(oTempHP.get("current"));&nbsp; It was throwing an error because even though I had just created the TempHP attribute, oTempHP was still undefined. var oCharacter = getObj("character", obj.get("represents")); var oTempHP = findObjs({_type: "attribute", name: "TempHP", _characterid: oCharacter.id})[0]; &nbsp; &nbsp; &nbsp; &nbsp; if (oTempHP == undefined ) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; createObj("attribute", { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: "TempHP", &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current: 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; characterid: oCharacter.id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var iTempHP = 0; &nbsp; &nbsp; &nbsp; &nbsp; } else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var iTempHP = parseInt(oTempHP.get("current")); &nbsp; &nbsp; &nbsp; &nbsp; } These changes fixed it. It works great now. If the token linked journal has the TempHP attribute, it gets the right value. If the token does not have the TempHP attribute, it creates it on the token and then sets the value to zero. Thanks everyone!
1373468946
Alex L.
Pro
Sheet Author
var oCharacter = getObj("character", obj.get("represents")); var oTempHP = findObjs({_type: "attribute", name: "TempHP", _characterid: oCharacter.id}); &nbsp; &nbsp; &nbsp; &nbsp; if (oTempHP.length == 0 ) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; createObj("attribute", { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: "TempHP", &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current: 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; characterid: oCharacter.id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var iTempHP = 0; &nbsp; &nbsp; &nbsp; &nbsp; } else { oTempHP = oTempHP[0]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var iTempHP = parseInt(oTempHP.get("current")); &nbsp; &nbsp; &nbsp; &nbsp; } Try this.
Using .length instead of undefined will just throw the cannot get .length error. What I have works now and I can implement it in both my token status script and my simple tools script for managing TempHP without opening the token or character sheet.
1373470002
Alex L.
Pro
Sheet Author
HoneyBadger said: Using .length instead of undefined will just throw the cannot get .length error. What I have works now and I can implement it in both my token status script and my simple tools script for managing TempHP without opening the token or character sheet. It shouldn't, you had a [0] at the end before so it wasn't an array so wouldn't have a length. Doesn't matter what you lose i just wanted to make sure you knew what was wrong with the first one.