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 Help: Change attribute when another attribute is empty

1511009522

Edited 1511009686
Will K.
Sheet Author
Okies, put this script together with some cut and paste.  The attribute logos1_power_name_2 is a text field and attribute  logos1_power_add_2 is a checkbox.  I'm trying to set things up so that when the text field is empty it toggles the checkbox off.  The newValue="" seems to be the problem, it works if I set it to look for an arbitrary value, newValue="X". on("change:logos1_power_name_2", function(eventInfo) { if(eventInfo.newValue =="")   setAttrs({ logos1_power_add_2: 0,   }); });
1511010526

Edited 1511010582
Will K.
Sheet Author
Well, this is embarrassing.  Literally two minutes after posting this I went and found out about the "null".  New script looks like this and works for anyone that bumps into the same issue. on("change:logos1_power_name_2", function(eventInfo) { if(eventInfo.newValue ==null)   setAttrs({ logos1_power_add_2: "0",   }); });
1511011087

Edited 1511012013
Jakob
Sheet Author
API Scripter
This is really weird. I only use === to avoid weird Javascript equality issues, but eventInfo.newValue should be a string, so (eventInfo.newValue == null) should always be false. But it seems as if it's either null or undefined (probably the latter) in this case. Edit: Aha! It seems that when the new value of an attribute is an empty string, the eventInfo object does not have a newValue property, which is why you're getting undefined, and undefined == null. This really seems like a bug in the Roll20 implementation, though. Even weirder, when you reset an attribute to the default (and that default is not empty) by clearing the field, there's also no eventInfo.newValue property, even though the new value of the attribute is not an empty string. I'm just afraid of reporting as a bug because it will break existing code like yours above.
That went pretty far over my head.  So, the script is working but not because it's suppose to.  Is there perhaps a cleaner way to do this?
1511012399

Edited 1511014181
Jakob
Sheet Author
API Scripter
Will K. said: That went pretty far over my head.  So, the script is working but not because it's suppose to.  Is there perhaps a cleaner way to do this? I can't read the minds of whoever wrote this, but if it was working the way I think it ought to work, your first attempt would have worked, because it's the logical thing to do! I can suggest an alternative that would catch the undefined case as well as the empty string case, in case Roll20 ever changes this: if (!eventInfo.newValue) This will convert eventInfo.newValue into a Boolean and then check if the result is false. It will be false if and only if eventInfo.newValue is one of the following: undefined null NaN 0 "" false As you can see, this catches both undefined (which is what it actually is) and the empty string (which is what it should be). The other examples should actually never happen in normal Roll20 operation, since every attribute is stored as a string, though I have seen numbers happen before in weird cases. So if Roll20 ever decides to change this to be an empty string instead of just not having the property there, it would be future-proof.
1511015694

Edited 1511016361
Will K.
Sheet Author
Excellent!  Thanks, that's a real help.  Trying to figure this stuff in roll20 has been like trying to teach myself a language through cross-words puzzles. :)