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

[Help] Dealing with default values (getting and setting)

Hello, I'm working on a sheet that has a repeating section for powers. One of the properties powers can have is "maintain", which essentially means they're a toggle - when you activate them, you lose some mana from your max, and when you de-activate them, you regain that mana. I have a hidden field, MaintainActive, that's either a 0 (power is currently not active) or a 1 (power is currently active). The default value, of course, is 0. The issue is that Roll20 doesn't like fields that are set to their default value. They recommend using getAttrByName for these situations, which works, but doesn't let you actually change the object (so I can change the 0 to a 1 when players turn on powers for the first time). I've tried some hacks like checking if a find returns undefined, then creating an object for it, but that just seems to confuse the field system. I'm sure there's a simple way to get a field with a default value  -> set a new value for that field, creating a proper object, but I can't figure it out! Any help would be greatly appreciated.
1532788264
The Aaron
Pro
API Scripter
If you're in control of the character sheet, the easiest thing would be to have an on sheet open event that sets the attribute to it's default value if it isn't set already.  Otherwise, the only option from the API is to create the attribute as you mentioned.  You'll need to make sure the character case matches the version in the character sheet.  If that doesn't work, then probably  it's a bug that needs to be addressed.  In that case, see if you can make a minimal character sheet and script that reproduce the issue so that it's easiest for the devs to fix it.
1532796217
GiGs
Pro
Sheet Author
API Scripter
You can also have a sheet change event linked to one of the fields in your repeating fieldset, that whenever its set to a valid value, the sheet worker checks if the related MaintainActive attribute <> 1; if so set it to 0. Since only 2 values are valid, this should create the attribute at 0 if it doesnt exist. 
1532817973

Edited 1532818200
Thanks for your help! I'm having an issue with setting the attribute (this is in the middle of a standard section loop, with idarray[i] being the current section ID). var power = "repeating_powers_" + idarray[i] + "_powername"; console.log(power); //returns repeating_powers_-lixrcgyptscfevmatkv_powername setAttrs({    power: "hello" }); The above does not work, but this does: setAttrs({    "repeating_powers_-lixrcgyptscfevmatkv_powername": "hello" }); Clearly I'm missing something to do with using variables in setAttrs, although I think I've done this before and it's worked! Edit: you need to put variables in square brackets to use them in setAttr, because sure, why not. Thanks again for your help!
1532819025

Edited 1532819160
GiGs
Pro
Sheet Author
API Scripter
I wonder if that has to do with the "-" in idarray, I cant see why it would fail otherwise. However, if you are changing a field within  a row, you don't need to use idarray. Here's an example from the wiki : on ( "change:repeating_spells:spelllevel" , function () { getAttrs ([ "repeating_spells_SpellLevel" , "repeating_spells_SpellName" ], function (values) { setAttrs ({ repeating_spells_SpellDamage: Math. floor (values.repeating_spells_SpellLevel / 2 ) + 10 }); }); }); If your sheet worker reacts to a change in a repeating section, and in response changes an attribute in the same row that triggered it, you can skip having to figure out the row ID, because you dont need it.
1532819062

Edited 1532885149
The Aaron
Pro
API Scripter
Yup. That’s just a language thing. Without the square brackets, it believes power is the name of the property you want to set. The brackets are actually fairly new to the language. In the past, you would have had to use array notation: var settings = {}; settings[power]=“hello”; setAttrs(settings);