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

setAttrs never happens on my script

Hello guys, I've been trying to debug this for hours but can't understand what I'm doing wrong: on('sheet:opened change:initattr change:resolveattr change:energyattr change:guardattr',function(){ getAttrs(["initattr","resolveattr","energyattr","guardattr"], function(values) { getAttrs([values.initattr.substring(2, values.initattr.length - 1)], function(v) { setAttrs({ initattr_value: _.toArray(v)[0] }); }); getAttrs([values.resolveattr.substring(2, values.resolveattr.length - 1)], function(v) { setAttrs({ resolveattr_value: _.toArray(v)[0] }); }); getAttrs([values.energyattr.substring(2, values.energyattr.length - 1)], function(v) { setAttrs({ energyattr_value: _.toArray(v)[0] }); }); getAttrs([values.guardattr.substring(2, values.guardattr.length - 1)], function(v) { setAttrs({ guardattr_value: _.toArray(v)[0] }); }); }); }); (couldn't find the tag for code if there is one, sorry) Can you guys spot something I'm doing wrong? Or some tips for better debugging the situation?  Any help would be welcomed. Thanks
1512223911

Edited 1512224318
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Well, I'd start by using console.log(JSON.stringify(v)) to see what is being returned in your second getattrs. Do you get any errors in the console? To define code, highlight the section of text and then click the paragraph symbol in the top left of the text editor for a dropdown menu of various formats. Edit, what are you trying to extract with the substringing? Or put another way, what info is in each of these attribute's?
1512225484

Edited 1512225677
Oh, thanks for the information. I'm trying to make a Character sheet, and in this particular character sheet I have a select box where the player chooses which attribute to use as a bonus to some defenses and what nots. So I have this: <div> <select style="width: 100px;" name="attr_initattr"> <option value="@{Agility}">Agility</option> <option value="@{Intelligence}">Intelligence</option> </select><input type="hidden" name="attr_initattr_value" value="0" disabled="true"> <br>Initiative Attr. </div> So when the player chooses "Agility" I want to put the Agility value (which is in the attr_Agility input) into the "attr_initattr_value" input. So v  should have the attr_Agility I think, but nothing's happening. Maybe there's even an easier way of doing this?
1512225682

Edited 1512226704
Jakob
Sheet Author
API Scripter
So the first thing you should do is add some log statements in the code, as Scott has said, so you know what's going on. The second thing you should do is remove the disabled="true" on the hidden input - disabled fields are autocalc fields, and this will certainly conflict with trying to set them via sheet worker. (optional) Why do you have "@{Agility}" as the value of the initattr attribute if you're not using it directly in any formulas anyway? You could just let the value be "agility", and then you can leave out the substring stuff for the same amount of information. You can just use the initattr_value attribute instead. when you need to use it in calculations. That's what you have it for, right? (optional) Actually, you should add the possible source attributes to your event string; the way it's set up now, if initattr is set to @{Agility}, and agility changes, initattr_value will not change. (optional) On this note, it's probably easiest to get all the attributes you need in the initial getAttrs statement, then you can leave out the nested getAttrs. This will be faster and easier to read. (optional) or  go the other way around: in a formula, using @{initattr} will already give you the value of agility - so why have the extra initattre_value attribute?
Done that, thanks for the info. This was it! Thank you! Indeed, I realized that as I was writing my first post. lol Good idea, will do, thanks. I'm afraid I didn't quite understood what you meant here.
1512228672
Jakob
Sheet Author
API Scripter
I'm afraid I didn't quite understood what you meant here. I mean to just do getAttrs(["initattr","resolveattr","energyattr","guardattr", "agility", "intelligence"], function(values) { and so on, until you have all the attributes you need in one go, instead of running another four getAttrs to get the specific attributes for each sub-choice.
1512229923
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Yep, the difference in time required for getAttrs([array with single attribute name]) vs getAttrs([Array with 100 attribute names]) is negligible, if it's even noticeable. Doing that by chained getAttrs however will cause the user to notice a very definite lag while everything resolves.
1512415203

Edited 1512416242
Jakob said: I mean to just do getAttrs(["initattr","resolveattr","energyattr","guardattr", "agility", "intelligence"], function(values) { and so on, until you have all the attributes you need in one go, instead of running another four getAttrs to get the specific attributes for each sub-choice. Scott C. said: Yep, the difference in time required for getAttrs([array with single attribute name]) vs getAttrs([Array with 100 attribute names]) is negligible, if it's even noticeable. Doing that by chained getAttrs however will cause the user to notice a very definite lag while everything resolves. Hey Jakob and Scott, sorry for the very late reply, and thanks for your input. So with your method, I would have something like this, right? on('sheet:opened change:initattr change:resolveattr change:energyattr change:guardattr',function(){ getAttrs(["initattr","resolveattr","energyattr","guardattr","Might", "Dexterity", "Agility", "Constitution", "Intelligence", "Charisma", "Insight", "Discipline"], function(values) { /*mysterious stuff in here*/ }); }); My problem is; if I get all the all the values with the same getAttrs, how can I select the right value inside the values array? For instance, If someone selects "Agility" in "initattr", I would want to do something like this:  setAttrs({ initattr_value: values[values.initattr] }); But as far as I know, that's very wrong syntax. :) Is there some correct way of doing this? Edit: Well this is awkward. Hahaha, that's exactly how I should do it, and its perfectly acceptable syntax. Thanks again everyone. Problem solved.