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

Sheet Worker Scripts - Detecting nonexistent attributes?

November 14 (7 years ago)

Edited November 14 (7 years ago)
Missingquery
Pro
Sheet Author
API Scripter
I want to be able to detect when a character sheet is opened if certain attributes do not exist yet. I initially tried something like:
if (className == undefined){
setAttrs({
//set stats
})
}
Unfortunately, this didn't work as I planned because the class name always returned as undefined when the sheet was opened, regardless of if it was changed in the past. And I would like to be able to detect the lack of attributes because despite setting their values in my HTML, certain attribute values are not properly detected and when I try to use them in calculations, the script returns a NaN error. This may have to do with the fact that they are set in spans as opposed to inputs, but I'm not sure. So if there is a better way to resolve this error, I would be grateful. I'm also willing to share any if my code if it might help?
November 14 (7 years ago)

Edited November 14 (7 years ago)
Jakob
Sheet Author
API Scripter
If you want to have a default value, you need an actual input, not just a span (just add a hidden input with the default value in addition to the span).

In general, an attribute will return undefined via getAttrs if and only if
1. There doesn't exist any input somewhere on the sheet that specifies a default value for the attribute, AND
2. The attribute has not been set manually somehow, either by a sheet worker or on the attributes & abilities tab.

So it should never happen that an attribute that has been changed in the past should be returned as undefined (due to 2). I imagine this is some kind of user error. [just to make sure, you ARE using getAttrs() to get the value of attributes, yes? I'm sorry if I am insulting you by assuming this may not be the case]
November 14 (7 years ago)

Edited November 14 (7 years ago)
Missingquery
Pro
Sheet Author
API Scripter
Edit: Oh, I think I see what the problem is. I didn't use getAttrs() to get the value of the attributes and instead used eventInfo.newValue in the case that the attributes wouldn't exist yet, but such a value wouldn't exist if the Class attribute hadn't changed. If I add a hidden input value with the same name as the span, that should solve my problem, correct? Since that would negate my need to have the script run when the sheet is opened at all.
November 14 (7 years ago)
Jakob
Sheet Author
API Scripter

Marth Lowell said:

Edit: Oh, I think I see what the problem is. I didn't use getAttrs() to get the value of the attributes and instead used eventInfo.newValue in the case that the attributes wouldn't exist yet, but such a value wouldn't exist if the Class attribute hadn't changed. If I add a hidden input value with the same name as the span, that should solve my problem, correct? Since that would negate my need to have the script run when the sheet is opened at all.

Yeah, that's it, on a sheet:opened event you cannot use the eventInfo.newValue property.

But the hidden input with the desired default value should indeed solve the problem.
November 14 (7 years ago)
Finderski
Sheet Author
Compendium Curator
Speaking of default values...if set a default value, then update the sheet later with a different default value, will the new default value override the previous default value? I'm assuming it does, but just want to double check before I make a bad assumption.
November 14 (7 years ago)
Jakob
Sheet Author
API Scripter

Finderski said:

Speaking of default values...if set a default value, then update the sheet later with a different default value, will the new default value override the previous default value? I'm assuming it does, but just want to double check before I make a bad assumption.

As long as the attribute object itself hasn't been created, yes (if the user changes it to something else and then back, or if a sheet worker changes the value at any point, the attribute will be created and the default is no longer being used).
November 14 (7 years ago)
Finderski
Sheet Author
Compendium Curator

Jakob said:

Finderski said:

Speaking of default values...if set a default value, then update the sheet later with a different default value, will the new default value override the previous default value? I'm assuming it does, but just want to double check before I make a bad assumption.

As long as the attribute object itself hasn't been created, yes (if the user changes it to something else and then back, or if a sheet worker changes the value at any point, the attribute will be created and the default is no longer being used).

That's what I thought...thanks for confirming. :)