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

Use useroptions in worker scripts

1589984659

Edited 1589986001
Tealk
Pro
Sheet Author
Hello, i would like to query a checkbox from the useroptions in the worker scripts, unfortunately i can't find any information about it, can someone tell me how to do that? i would like to deactivate calculations and also set fields to readonly, is this even possible becaus tehis one doesn't work anyway: "document.getElementsByClassName('sheet-readOnly').readOnly = true;"
1589985708
GiGs
Pro
Sheet Author
API Scripter
I'm not sure what you're asking, but you cant generate queries from sheet workers. What useroptions are you referring to?
1589986142

Edited 1589988262
Tealk
Pro
Sheet Author
Hello GiGs and thanks for your quick response. So I have the following option     "useroptions": [         {             "attribute": "calcMoney",             "displayname": "Geld automatisch umwandeln?",             "type": "checkbox",             "value": "1",             "description": "Aktivieren falls ihr möchtet das eure Währung automatisch in das nächst Höhere umgewandelt werden soll."         }     ] and would like to check in the sheet workers if this option is selected
1589986612
GiGs
Pro
Sheet Author
API Scripter
Ah, the Default Settings. Sheet workers dont have direct access to useroptions, but since useroptions just set attributes, you can create a sheet worker to check the status of the attribute mentioned.  You cant do anything with this btw: document.getElementsByClassName. No DOM related javascript works on roll20.  So you;d create a sheet worker that looks something like this on('change:calcmoney sheet:opened', () => {     getAttrs(['calcMoney'], values => {         let calcMoney = parseInt(values.calcMoney) || 0;         // at this point you have calcMoney in a variable and can do whatever you want with it });
1589987317

Edited 1589988251
Tealk
Pro
Sheet Author
Thanks for the example, I am not really good at Javascript, I would like to implement the following:     on("change:MU change:calcmoney sheet:opened'", function() {         getAttrs(["MU", "KL", "calcMoney"], function(values) {             let calcMoney = parseInt(values.calcMoney) || 0;             if (values.MU >= 10 && values.calcMoney == "1") {                 setAttrs({                     MU: Math.floor(parseInt(values.MU) - 10)                 });                 setAttrs({                     KL: Math.floor(parseInt(values.KL) + 1)                 });             }         });     }); probably I cannot write it all so compactly but have to split it into several queries? but it won't work that way either     on('change:calcmoney sheet:opened', () => {         getAttrs(['calcMoney'], values => {             let calcMoney = parseInt(values.calcMoney) || 0;             on("change:MU'", function() {                 getAttrs(["MU", "KL"], function(values) {                     if (values.MU >= 10 && calcMoney == "1") {                         setAttrs({                             MU: Math.floor(parseInt(values.MU) - 10)                         });                         setAttrs({                             KL: Math.floor(parseInt(values.KL) + 1)                         });                     }                 });             });     }); You cant do anything with this btw: document.getElementsByClassName. No DOM related javascript works on roll20.  then i can't make form fields read only with a useroptions
1589988581
GiGs
Pro
Sheet Author
API Scripter
You cant make formfields dynamically on roll20. All you can do is make them ahead of time, and show or hide them  with CSS. The technique to do this is described on the wiki's CSS Wizardry page. But it sounds like you also must  check the Building Character Sheets  page - this details the specifics of how roll20 differs from other websites, and what limitations you must deal with. To your specific problem: can you say in words what you are trying to do? it looks like you are altering the KL and MU values conditionally. Describe how this works and we can tell you how to build the sheet worker. The first version of your sheet worker is close to correct by the way, you wouldnt want to build one like the second version. Heres what your sheet worker looks like with the code corrected. But I'm not sure it;ll do what you need it to, so it's a good idea to describe what it's meant to do: on("change:mu change:calcmoney sheet:opened", function() {     getAttrs(["MU", "KL", "calcMoney"], function(values) {         let calcMoney = parseInt(values.calcMoney) || 0;         let MU = parseInt(values.MU) || 0;         let KL = parseInt(values.KL) || 0;         if (MU >= 10 && calcMoney == 1) {             setAttrs({                 MU: MU - 10,                 KL: KL + 1             });         }     }); }); Some things about the sheet worker: Because of roll20's baffling design, attribute names on the on(change) line (and only that line) must always be written in lower case or they wont work reliably. For this reason, it's generally recommended to create your attributes entirely in lower case. In the three lines starting with let, I define the variables of all the values first. You dont need to do this, but when you're new, its best practice. It makes it easier to do error checking and finding faults later. Attributes in roll20 are stored as strings, so its best to coerce them into numbers before you do any arithmetic or logical checking with them, which is why i used parseInt. setAttrs can handle multiple attribute assignments at the same time, so i combined the setAttrs statements. The problem I see with this: the sheet worker will run every time the sheet is opened. So the MU and KL attributes might keep changing more than you want them to. You could remove the sheet:opened  from the first line, and then they'd change only when the values are altered. 
1589989592

Edited 1589989902
Tealk
Pro
Sheet Author
Because of roll20's baffling design, attribute names on the on(change) line (and only that line) must always be written in lower case or they wont work reliably. For this reason, it's generally recommended to create your attributes entirely in lower case. I did not know that, then I guess I will have to rewrite some of my attributes; i like to use Camel case, so this is not advisable for attributes? many thanks for your detailed explanations, they are really helpful KL and MU are currencies, 10MU give 1KL and I wanted to have this calculated automatically when the game master activates the option for the bow, I want to do this with some automatic calculations so that groups with house rules can also use the bow. So as soon as more than 10 MU are entered, it is automatically converted to the next larger unit: I've already managed to do that without the useroptions, but it doesn't seem to work with this one. Or can't I test it in the Sheet Sandbox? here once the code which represents the description above:     on("change:MU", function() {         getAttrs(["MU", "KL", "calcMoney"], function(values) {             if (values.MU >= 10 && values.calcMoney == "1") {                 setAttrs({                     MU: Math.floor(parseInt(values.MU) - 10)                 });                 setAttrs({                     KL: Math.floor(parseInt(values.KL) + 1)                 });             }         });     });
1589990152
GiGs
Pro
Sheet Author
API Scripter
That description makes sense, I see now why your calculation works the way it does. See the worker I posted, you only need to tweak it slightly to do what you need - as follows: on("change:mu", function() {     getAttrs(["MU", "KL", "calcMoney"], function(values) {         let calcMoney = parseInt(values.calcMoney) || 0;         let MU = parseInt(values.MU) || 0;         let KL = parseInt(values.KL) || 0;         if (MU >= 10 && calcMoney == 1) {             setAttrs({                 MU: MU - 10,                 KL: KL + 1             });         }     }); }); You might also want to check for calcMoney changes; if people switch it to 1, you probably want it to do the conversion immediately if needed. Also, you might want to handle multiples greater than 10. If someone has built up 100MU when the GM switches the calcMoney setting on, you want it all converted. This should do the trick: on("change:mu change:calcmoney", function() {     getAttrs(["MU", "KL", "calcMoney"], function(values) {         let calcMoney = parseInt(values.calcMoney) || 0;         let MU = parseInt(values.MU) || 0;         let KL = parseInt(values.KL) || 0;         if (MU >= 10 && calcMoney == 1) {             let multiples = Math.floor(MU/10);             MU -= multiples *10;             KL += multiples;             setAttrs({                 MU: MU,                 KL: KL             });         }     }); });
1589990164
Tealk
Pro
Sheet Author
Now I am really confused, mu to kl does not work, but kl to tt and tt to gf     on("change:mu change:calcmoney", function() {         getAttrs(["mu", "kl", "calcMoney"], function(values) {             let calcMoney = parseInt(values.calcMoney) || 0;             let mu = parseInt(values.mu) || 0;             let kl = parseInt(values.kl) || 0;             if (mu >= 10 && calcMoney == 1) {                 setAttrs({                     mu: mu - 10,                     kl: kl + 1                 });             }         });     });          on("change:kl change:calcmoney", function() {         getAttrs(["kl", "tt", "calcMoney"], function(values) {             let calcMoney = parseInt(values.calcMoney) || 0;             let kl = parseInt(values.kl) || 0;             let tt = parseInt(values.tt) || 0;             if (kl >= 10 && calcMoney == 1) {                 setAttrs({                     kl: kl - 10,                     tt: tt + 1                 });             }         });     });          on("change:tt change:calcmoney", function() {         getAttrs(["tt", "gf", "calcMoney"], function(values) {             let calcMoney = parseInt(values.calcMoney) || 0;             let tt = parseInt(values.tt) || 0;             let gf = parseInt(values.gf) || 0;             if (tt >= 10 && calcMoney == 1) {                 setAttrs({                     tt: tt - 10,                     gf: gf + 1                 });             }         });     });
1589990452
GiGs
Pro
Sheet Author
API Scripter
If you have multiple currencies to convert, its best to do them all in one sheet worker. One advantage of sheet workers is you can do a lot of things within each one. Can you post the inputs for mu and kl? There may be a clue there to explain why its not working.
1589991207

Edited 1589991357
Tealk
Pro
Sheet Author
I can't figure out what this multiple is for. here are the 4 fields for the currencies &lt;label&nbsp;for="attr_gf"&gt;GF&lt;/label&gt;&lt;input&nbsp;type="number"&nbsp;name="attr_gf"&nbsp;/&gt; &lt;label&nbsp;for="attr_tt"&gt;TT&lt;/label&gt;&lt;input&nbsp;type="number"&nbsp;name="attr_tt"&nbsp;/&gt; &lt;label&nbsp;for="attr_kl"&gt;KL&lt;/label&gt;&lt;input&nbsp;type="number"&nbsp;name="attr_kl"&nbsp;/&gt; &lt;label&nbsp;for="attr_mu"&gt;MU&lt;/label&gt;&lt;input&nbsp;type="number"&nbsp;name="attr_mu"&nbsp;/&gt; here the complete current code <a href="https://github.com/Tealk/roll20-character-sheets/blob/Aborea/Aborea_new/aborea.html" rel="nofollow">https://github.com/Tealk/roll20-character-sheets/blob/Aborea/Aborea_new/aborea.html</a>
1589991696
GiGs
Pro
Sheet Author
API Scripter
Thanks, I'll check that out tomorrow, I'm out of time today. I'm not seeing from the above why the mu conversion isnt working, so I'll import the sheet and do some proper experimenting when i can. The multiple in my script was to handle the case of if someone had, say, 53 MU, and the GM switched calcMoney on. Your sheet worker would only drop it to 43, mine would drop it to 3, and add 5 to KL.
1589992130

Edited 1590064364
Tealk
Pro
Sheet Author
so that's what i thought at the beginning too, but every time it falls by 10 and there are more than 10 left it recalculates, so in a kind of loop, i think it's gone but the better one is yours
1590064347
Tealk
Pro
Sheet Author
So I have now reworked everything so far, since i add the variables but the life, mana and money calculation does not run
1590064524
GiGs
Pro
Sheet Author
API Scripter
I havent had time to look at it today yet. I'll do so this evening. Can you post the current version of the sheet to the github you posted yesterday, so I'm working with current code?
1590065269
Tealk
Pro
Sheet Author
just uploaded the latest version i guess that the useroptions in the sheet sandbox does not work, but the mana calculation does not need any useroptions at all
1590066511
Tealk
Pro
Sheet Author
ok it was my fault with mana, just updated the repo but apparently I cannot check the useroptions
1590067886
GiGs
Pro
Sheet Author
API Scripter
Yes, as i mentioned earlier, sheet workers have no direct access to the useroptions. All useroptions do is set attribute values, so you can have sheet workers respond to those attributes being set.
1590068165
Tealk
Pro
Sheet Author
All useroptions do is set attribute values, so you can have sheet workers respond to those attributes being set. what does that mean? i have to create hidden input fields?
1590068744

Edited 1590068813
GiGs
Pro
Sheet Author
API Scripter
Useroptions are an optional feature, you dont have to use them. But their only purpose is to set the values of existing attributes. For each useroption, there must already exist an attribute, and the useroption just sets that value. The primary purpose is for setting configuration options. Like if your sheet has the option of having all rolls be whispered to the GM, or rolled in public, you would have a useroption to choose between those two. Or if you have a house rule where everyone gets +2 to saving throws (again, just an example), you'd have a user option to set that. Then the GM when they start up a campaign, can pick whichever user options they want to use, and then start the campaign. All new characters designed after that point will have the useroption assigned. Characters which existed before the change, won't (there's a menu option to update the, it's not automatic). You should not be using useroptions at all if you aren't sure what they do. They are an optional feature, and most sheets dont use them. Read more here:&nbsp; <a href="https://wiki.roll20.net/Default_Sheet_Settings" rel="nofollow">https://wiki.roll20.net/Default_Sheet_Settings</a>
1590069157

Edited 1590069319
Tealk
Pro
Sheet Author
The problem is that I know some rounds that use house rules and where the automatic calculations will cause problems. But I would like to make it easier for the people who play by the rules. So it is important that you can turn off the calculation, what do you suggest how to handle this?
1590069505
GiGs
Pro
Sheet Author
API Scripter
First, everything you put in user options must already exist on the character sheet. So you'd have an attribute that controls whether automatic calculation is used for that specific character .&nbsp; Then you can set up useroptions to set that value, and it will set it for all new characters. Useroptions is a feature we did&nbsp; not have for a long time, and had to do manually on a per-character basis. All useroptions does is allow us to set it for all new characters, or all existing characters, at once.
1590071206
Tealk
Pro
Sheet Author
Okay, thanks a lot for your help. I'll see how I can make it happen.
1590074015
GiGs
Pro
Sheet Author
API Scripter
I just had a look at the sheet, and the reason the currency attributes aren't updating, is because there isnt a calcmoney attribute in the sheet. I added this at the start to make it work &lt;input&nbsp;type="number"&nbsp;name="attr_calcmoney"&nbsp;value="0"&nbsp;/&gt; There';s more than one reason your mana worker isnt working. The intelligence bonus worker uses the wrong attribute name: it sets intb ,&nbsp;but the bonus attribute is apparently inb . You also have a&nbsp; parseintt &nbsp;typo in the worker, it should be parseInt . Finally your Beruf &nbsp;attribute must have the name entered perfectly. If it doesnt match one of the preset values (&nbsp;"Dieb", "Krieger", etc), the Mana worker will produce no output. I woulkd recommend using a select for beruf, instead of an input, so you can make sure players cannot enter the wrong value by a typo or whatever.
1590084675

Edited 1590084691
Tealk
Pro
Sheet Author
Finally your&nbsp; Beruf &nbsp;attribute must have the name entered perfectly. Yes the problem is that there are some classes from house rules, then they could not enter them.
1590122803
GiGs
Pro
Sheet Author
API Scripter
You could have a checkbox to switch between the select and input, using CSS to show one and hide the other. Use the same name="attr_" for both. That way when they are entering stuff to be matched in the sheet worker, they get help to enter it accurately.
1590126885
Tealk
Pro
Sheet Author
switch between the select and input Good idea, I implemented it directly.