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 .
×

findObjs doesnt get object because it doesnt exist yet, how can you resolve this?

In my sheet i have a input with attr_speed as an example, the findObjs function wont find this attr unless i first touch it in the sheet itself so that it gets created. Can i force this creation somehow so that i will always get the obj even if it wasnt updated yet?
1621984945
The Aaron
Roll20 Production Team
API Scripter
In the sheet, you could create attributes that don't exist in your on('sheet:open') handler.  In the API, you could create the attribute if you fail to find it with findObjs(): let attr = findObjs({type:'attribute',name:'speed',character_id:someCharID})[0] || createObj('attribute',{name:'speed',character_id:someCharID});
1621985131
timmaugh
Roll20 Production Team
API Scripter
Hmm... if you just need to know that the attribute exists, you can pipe it out to a "get or create" function and make sure you return the attr. A multiline solution allows for other code interjections or tests along the way, and might look like: const getOrCreateAttribute = (n, cid) => {     let attr = findObjs({ type: 'attribute', characterid: cid, name: n})[0];     if (typeof attr !== 'undefined') return attr;     else {         return createObj( 'attribute', { characterid: cid, name: n});     } }; Obviously you could set values in there as defaults, but you could have the value setting somewhere else come *after* this, when you're sure you've arrived at an attribute. That way, you only maintain one place for what the value-setting looks like. Another alternative, if you don't need a lot of testing or other code interjections is just to join them with a logical OR: const getOrCreateAttribute = (n, cid) => {     return findObjs({ type: 'attribute', characterid: cid, name: n})[0] || createObj( 'attribute', { characterid: cid, name: n}); };