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

Failing to do sheetworker that outputs text on condition

1599407022
Yui V.
Pro
Sheet Author
Hello, I'm working on a sheet where I have a script I want to imput but I'm ascriptic, so.... Here it is: on('change:repeating_melee-weapons:mlrea change:repeating_melee-weapons:reaMod remove:repeating_melee-weapons sheet:opened', function() {     repeatingSum("mod_REA","melee-weapons",["mlrea", "reaMod"]);     function() {         getAttrs(["mod_REA"], function(values) {                 let message = " " ;                 let modval = parseInt(values.mod_REA)||0;                 if ( modval > 0) message = " prêt pour la mêlée, arme en main !";                 else if ( modval < 0) message = " prêt pour la mêlée, arme en main !";                 else if ( modval = 0) message = " ";                 return message ;                 setAttrs({ inmelee: values.message              });             });             });     }); I added the repeatingSum from the wiki (with the Const first) and it works just fine: It adds a mod from a repeating section to a die roll, if the correpsonding checkbox is checked. Now I want to input in the roll value, after the inlineroll a " prêt pour la mêlée, arme en main !" if the mod_REA attribute is inferior or superior to 0 (to show that the modifier is included in the roll and non-null). And " ", a simple space (or nothing if it's possible) if mod_REA is equal to 0 (and thus has no influence on the roll result). I added the function inside the repeatingSum because I was not sure if it would pick up the updated mod-REA each time repeatingSum and it trigger. But it doesn't work, be it inside or outside the repeatingSum. Can't figure out how the if/else statements work, or if i'm putting in the text values right... Originally I wanted this script to input the name of the weapon in the roll value but I ditched the idea because it's too complicated for me newbie to get the rowID etc for the weapon name attribute("attr_ml") corresponding the ticked checkboxes... Now I just want to input flat text in the roll value. If you could offer some advice I would be really thankfull!
1599408360
GiGs
Pro
Sheet Author
API Scripter
You'll need to put the repeatingSum and the following bit in separate sheet workers, like this:     on('change:repeating_melee-weapons:mlrea change:repeating_melee-weapons:reamod remove:repeating_melee-weapons sheet:opened', function() {     repeatingSum("mod_REA","melee-weapons",["mlrea", "reaMod"]);     });     on('change:mod_rea sheet:opened', function() {     getAttrs(["mod_REA"], function(values) {                 let message;                 let modval = parseInt(values.mod_REA)||0;                 if ( modval > 0) message = " prêt pour la mêlée, arme en main !";                 else if ( modval < 0) message = " prêt pour la mêlée, arme en main !";                 else if ( modval === 0) message = " ";                 setAttrs({                     inmelee: message              });             });         }); If you compare the workers above with your worker you should see a few significant differences. The first problem is conceptual. Certain functions in sheet workers are asynchronous (anything using getAttrs and setAttrs, which includes repeatingSum). Asynchronous functions take time to complete, and the script doesnt wait for them to complete. So in your script, repeatingSum is triggered, and starts to run. Before it completes, the rest of the worker continue - so it grabs the mod_REA value before it gets updated, and then repeatingSum updates the value. There are syntax errors that would stop it working anyway - but its important to explain that even if the syntax was correct, the function would fail, because the mod_Rea value grabbed in the second half of the script would be the value from before repeatingSum updates it. By moving that off into a separate sheet worker, with a change:mod_rea  trigger, it ensures it runs every time that value is updated, and the correct value is always used. Other breaking issues:  When using attribute names on the on(change) line, they must be in lower case. The line below contains an error: in javascript, a single = always assigns  a value. To compare  values, you must use == or ===. else if ( modval = 0) message = " ";  When you include the keyword return, the code ends at that point. So in the code below, the setAttrs line would never be reached.                 return message ;                 setAttrs({ inmelee: values.message  Also the setAttrs line contains a minor error. values the name of a javascript object - a special variable created in the getAttrs line. Your message variable is not in that object - its an independent variable. So you just use                 setAttrs({ inmelee: message  Just to be clear: this saves the message variable to an attrbute called inmelee. Does such an attribute exist?
1599414568
Yui V.
Pro
Sheet Author
Thank you so much! This is invaluable advice for me! Yes, the inmelee attribute exists. I can't believe I didn't see the values.inmelee: I made the same mistake yesterday in another script and wandered for an hour before realizing I wasn't actually calling my variable. It works now, Thank you Gigs.
1599449360
GiGs
Pro
Sheet Author
API Scripter
yay, glad to help :)