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

Character sheet attributes not updating correctly after using sendChat

1692860218

Edited 1692860434
Given my code below... I'm not seeing the values I would expect in the 'brawl' attribute (it should be the sum of the values in the 'brawlEvent' and 'brawlWeapon' attributes). Can anyone tell me why this is not working the way I'd expect it to? The value in 'brawl' should be 6, but I either get 00 or another wrong numeric value. The console log statements aren't displaying what I would expect either. I see "0" and "00", not "3", "3", and "6". Prior to the code shown here I'm using sendChat twice to roll dice and set the 'brawlEvent' and 'brawlWeapon' attribute values for the selected token's character, then I want to sum them to save in its 'brawl' attribute. Am I running into some synchronicity issue here? Where I'm trying to retrieve the values of these two attributes before they've actually been changed? var brawlEvent = findObjs({_type: "attribute", _characterid: char_id, _name: 'brawlEvent'})[0]; var brawlWeapon = findObjs({_type: "attribute", _characterid: char_id, _name: 'brawlWeapon'})[0]; var iBrawlEvent = brawlEvent.get('current'); var iBrawlWeapon = brawlWeapon.get('current'); var iBrawl = iBrawlEvent + iBrawlWeapon; log(iBrawlEvent); log(iBrawlWeapon); log(iBrawl); brawl2 = findObjs({_type: "attribute", _characterid: char_id, _name: 'brawl'})[0]; brawl2.set('current', iBrawl);
1692880450
timmaugh
Pro
API Scripter
You're getting text back when you reference an attribute, even if it stores a number. So when you think you're using the addition operator (+), because you have two strings, you are instead using the concatenation operator (+)... ...thank you javascript. Just coerce the values to be numbers: let iBrawlEvent = brawlEvent.get('current'); let iBrawlWeapon = brawlWeapon.get('current'); let iBrawl = Number(iBrawlEvent) + Number(iBrawlWeapon);
1692885867
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Also, in your findObjs calls, most of the properties should not have the leading underscore. It's probably ok on type and character I'd, but name definitely shouldn't. As for synchronicity, we'd need to see more of your code to know that as the creation is not part of what you show.
1692891108
timmaugh
Pro
API Scripter
To Scott's excellent point, I don't think any of the properties in findObjs() require the underscore. let o = findObjs({ type: 'attribute', characterid: char_id, name: 'MisterWhiskerTeaParties'})[0] || { get: () => { return 0;} }; The underscore is needed in the object.get()... some of the time. I think it's supposed to be that properties pre-pended with an underscore were promoted to standard javascript properties, accessible without the underscore right from the root object instead of with the get() function: obj.id obj.type ...however, I think I recall some cases where other underscore-prepended properties were not promoted like that. So... they're promoted unless they're not. To know which ones have an underscore, you can use Inspector to look at the properties. Here is an attribute, for example: