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
This post has been closed. You can still view previous posts, but you can't post any new replies.

on(change:) Event issue - API not updating attributes properly ... ?

1549798140

Edited 1549799959
Hey guys. I have an issue with the  >  on(change:)  < Event. Event: on("change:Bullenstärke", function() {        console.log('BULLENSTÄRKE');    getAttrs(["str-statusmod", "Bullenstärke"], function(values) {       console.log(values);       console.log(parseInt(values["Bullenstärke"]*4,10));       setAttrs({   "str-statusmod": parseInt(values["Bullenstärke"]*4,10) });     }); }); HTML: <div class="sheet-status-wrapper">    <input type="checkbox" name="attr_Bullenstärke" value="1" />    <div class="sheet-status">       <div class="sheet-status-icon sheet-status-icon-strength"></div>       <div>          Bullenstärke  <label>(+4 Stärke)</label>       </div>    </div> </div> API CALL (relevant snippet) function attribtoggle(characterObj,attributeObjArray) { var attributeName = attributeObjArray[0].get("name"); var attributeValue = attributeObjArray[0].get("current"); var newValue; if(attributeValue == '1') {     newValue = '0'; } else {     newValue = '1'; } attributeObjArray[0].set("current", newValue); }; I try to explain it with images. First of all. I have some hidden checkboxes in my character sheet that are toggled on and off by an API macro. This is mostly to change attributes and show icons for buffs and so on. Everything works fine with attributes that are calculated by the character sheet itself. (@{xxx} + @{yyy}) But if I use the on(change:) Event to recalc stats i have the following issue: I now use the macro to change the attribute value of the attribute by simulating a "check" (setting the value from 0 to 1). As you can see: The CSS responds by showing the correct div with the buff. The attribute itself is changed on the attribute page from 0 to 1. But strangely... there is no logging of my on(change:) Event. Now comes the weird part: I now change the CSS to use the checkboxes manually. And i check the checkbox. Everything is the same now... attribute value is identical. CSS shows the buff box. BUT Now it Logs my values and the Event is actually fired. From NOW on - the macro also fires the event. And now it keeps firing the event and logging the toggle while using the macro to check the checkbox until I reload the page. Then the issue starts over again. Are there any ideas on that one?
1549839980
vÍnce
Pro
Sheet Author
Not sure if it will help, but here's a couple of suggestions; Try changing your event listener from " Bullenstärke" to "b ullenstärke" <a href="https://wiki.roll20.net/Sheet_Worker_Scripts#change:.3Cattribute_name.3E" rel="nofollow">https://wiki.roll20.net/Sheet_Worker_Scripts#change:.3Cattribute_name.3E</a> Note: All attribute names are lowercased in events. So even if you normally refer to an attribute as "Strength", use "change:strength" in the event listener. You may need trigger your checkbox using a second, hidden input attribute. <a href="https://wiki.roll20.net/Sheet_Worker_Scripts#setAttrs.28values.2Coptions.2Ccallback.29_.5BAsynchronous.5D" rel="nofollow">https://wiki.roll20.net/Sheet_Worker_Scripts#setAttrs.28values.2Coptions.2Ccallback.29_.5BAsynchronous.5D</a> Note: If you are trying to update a disabled input field with this method you may run into trouble. One option is to use this `setAttrs` method to set a hidden input, then set the disabled input to the hidden element. In this example we have an attr named will, and we want to calculate judgement based off 1/2 of the will stat, but not to allow it to exceed 90. See below. Sorry that I can't really assist beyond that... ;-(
Update. Didn't work as well. I changed the attribute names. I tried to forward the values to another input to read them from there. But still -&gt; The change Event does not fire when i change the attribute value over the API. It just works on manual clicks on the checkbox... Can anyone confirm that it works in any way? Cause im working on it for a few days now.
1549922908
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
I'll be back with a more in depth dive, but how are you hiding the checkbox? Is it just pure CSS, or a change to the HTML? The HTML that you've shared doesn't use a hidden checkbox, so I'm guessing it is just pure CSS; in which case it shouldn't have any effect on whether the api or sheetworker fires. So, let me make sure I've got the issue down correctly: When your checkboxes are hidden, changing the value of the checkbox (whether manually or via API) correctly causes the display to change, but does not cause your sheetworker to fire and so your starke attribute is not updated. However, if you unhide the checkbox, your sheetworker is fired whether you manually change the value or use the API. Additionally, there appears to be a repetitive firing of something as your log continues until you reload. When you reload, the issue returns? How so? Are you unhiding the checkboxes by just editing the css/html in the inspector or by making actual changes to the sheet code and reloading the game after saving the new code?
1549964616

Edited 1549964648
Let me clarify a few things: The CSS: .sheet-statuses input[type="checkbox"] { display:none; } .sheet-statuses input[type="checkbox"]:checked + .sheet-status { display:grid; } Main goal is to add status-icons to the character token. At the same time an explanation for the icon shows up in the character sheet. That is what's happening with the CSS. The attribute (something like attr-buff-xyz-active) is changed and the CSS shows a div with the Icon and an explanation of the buff. Also with the checkbox some values on the character sheet are updated. For example &lt;input type="text" name="attr_warcrybonus" value="@{warcry}*2" readonly="readonly" disabled="true"&gt; This worked fine for all the buffs that just added another value for the character sheet calculations. Sadly the D&amp;D3.5 Sheet came along with a lot of sheet worker calculations for the attributes. So buffs adding to the base attributes like STR, CON, and so on are calculated by the on(change:) event of the corresponding fields. on("sheet:opened change:str-base change:str-misc change:str-temp change:str-class change:str-statusmod ", function() { getAttrs(["str", "str-base", "str-misc", "str-temp", "str-class", "str-statusmod" ], function(values) { setAttrs({ str: (parseInt(values["str-base"],10) + parseInt(values["str-misc"],10) + parseInt(values["str-temp"],10) + parseInt(values["str-class"],10) + parseInt(values["str-statusmod"],10) ) }); }); }); As you can see in the entry poist str-statusmod is the field I want to update for the calculation. Here is the (modified) on(change:) for the checkbox again: on("change:bullenstaerke", function() {&nbsp;&nbsp;&nbsp;&nbsp; getAttrs(["str-statusmod", " bullenstaerke "], function(values) { setAttrs({ "str-statusmod": parseInt(values[" bullenstaerke "]*4,10) }); }); }); &nbsp; I want to change-str-statusmod when the checkbox changes - so I can update the STR-calculation. ------------- Now to answer your questions: When your checkboxes are hidden, changing the value of the checkbox (whether manually or via API) correctly causes the display to change, but does not cause your sheetworker to fire and so your starke attribute is not updated. Exactly. The character sheet and the CSS correctly do their job. The checked value of the checkbox changes. The box is shown or hidden. Additionally the tab "attributes &amp; abilities" changes the corresponding value (bullenstaerke) from 0 to 1 and from 1 to 0. If I switch into that tab and keep pushing the Macro Button i can see the value being changed. The sheet worker - however - does not respond by firing the event. However, if you unhide the checkbox, your sheetworker is fired whether you manually change the value or use the API. Additionally, there appears to be a repetitive firing of something as your log continues until you reload. The "repetitive firing of something" are the other calculations of the STR-value. It changes the STR-value - then it changes ther STR-modifier. These are the 2 or 3 other events. It's not the change of visibility that makes it work. As soon as I MANUALLY click the checkbox it logs the following message: CLICKED: radio/checkbox &lt;input type=​"checkbox" name=​"attr_bullenstaerke" value=​"1"&gt;​ THEN the sheet worker actually fires including the&nbsp; on("change:bullenstaerke") &nbsp;event of the checkbox. This does NOT trigger when using the API. And then it does not fire the event. It DOES however work with the macro as soon as I have clicked the checkbox MANUALLY at least once. At least until i reload the page. Then i have to manually click the checkbox to fire the event manually and THEN the macro also works again. (until I close the character sheet again). The change of visibility was a way of debugging because i thought: "What the hell - why doesn't it work" and then i fiddled with the developer tools and made the checkboxes visible to see if they're actually checked. A little later i found out that the event fires when clicking them manually.
1549987255
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Hmm, ok, this is a little weird then. As a troubleshooting step, could you install chatsetattr by Jakob from the one click drop down and use that to try changing the attribute? I'm just trying to figure out if there's an issue with your script or an issue with your sheet. I'm expecting an issue with the script, but I can't currently see anything wrong with it. The chatsetattr command would be: !setattr --name Some Character -- Bullenstärke |1
1549992748

Edited 1549993002
Hey Scott, thanks so far! Seems like it's working properly with the script. Of course it does not add icons to the selected token but it logs the debug-messages in the API Sandbox-Window. I'll get the code from the script from github and compare the ways of setting the attributes. Maybe I'll just modify that one for adding the icons for the tokens. I'll let you know if I run into the same issue with that script after manipulating the tokens.&nbsp; First thing I found the the script: name: "useWorkers", command: "use-workers", desc: "Determines if setting attributes should trigger sheet worker operations." Guess I'll look into that one if he had the same issues in his script. :)
1549992998

Edited 1549993141
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
You could actually probably do all of this with existing scripts. Using ChatsetAttr for adjusting the sheet, and using Aaron's Tokenmod to apply the token markers. As a note, I would guess that your script is not getting the attribute correctly for some reason when the sheet is freshly loaded. From your sheet snippets and script snippets I can't see exactly where this issue might be coming from, but if you still wanted to do your custom script, you might look at logging attributeObjArray and the variables in that API function; keep in mind these logs will be output to the api page, not to the browser developer console.
1549993293

Edited 1549993309
Thanks for pointing me into the right direction Scott. Everything works fine now. Solution: attributeObjArray[0]. setWithWorker( "current", newValue); instead of: attributeObjArray[0].set("current", newValue);
1549993422
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
:facepalm: Well, I feel like an idiot now for not having spotted that when I first posted. Sorry for the run around.
Hello all, As this issue has been resolved, I am going to close it and mark it as resolved. Feel free to open a new thread if you have any additional issues.