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

Check to see if attribute exists before creating one automatically

1394637630

Edited 1394638696
Ok, so I have the following set up to assign attributes to a new token automatically. on("add:character", function(obj) { createObj("attribute", { name: "Strength", current: 10, characterid: obj.id }); }); The problem is, when ever a new sandbox spins up, it adds the attribute again. Making redundant copies. I don't want this, obviously. My solution thus far has been to disable the script. However, I want to add more tokens and characters now, and it keeps adding to those that already exist. I've tried this, on("add:character", function(obj) { if (obj.get("Strength") === 0){ createObj("attribute", { name: "Strength", current: 10, characterid: obj.id }); }; }); But this doesn't work. What do I need to do to simply check if the attribute already exists? obj.get() returns the value, so I checked it against FALSE 0, rather than "0" obviously. Would this work? The error I get is: ReferenceError: Invalid left-hand side in assignment at evalmachine.:3:5 at eval ( This makes me think it's a syntax error. I'm not familiar with JavaScript, I use LUA, and Perl.
1394684942

Edited 1394685348
Lithl
Pro
Sheet Author
API Scripter
In an add:character event, obj is the character object, not the attribute object. You'll want to find the attribute with the name "Strength" and a _characterid equal to obj.id , and check if that exists: on('add:character', function (obj) { var strAttr = findObjs({ _characterid: obj.id, name: 'Strength' })[0]; // If the character doesn't have the Strength attribute, findObjs() will return a 0-length array. // The first element in a 0-length array (index 0) is undefined. // If the character has at least one Strength attribute, findObjs() will return an array with elements // and we assume the first element is the most relevant one if (!strAttr) { // `undefined` is a "falsey" value, so `!undefined` returns `true` // Create attribute Strength } }); Because characters do not have a property named "Strength", obj.get("Strength") should be returning undefined . The exactly equal operator (===) compares both value and type, and undefined is not a numeric type, like 0.
Thank you, very much. I've only been working with Roll20's API and/or JS for 2 weeks.