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

Creating a simple script worker (Help)

1545893015

Edited 1545894080
Hi, I have just begun the process of creating my own sheet, but I don't quite understand how script workers function. I was looking to write a SW that auto-calcates the attr_maxhp tag, using the values of attr_level and attr_constitution. Formula I am trying to apply is maxhp="(20+(@{con}*3)+(@{lvl}*2)) However, even after looking at other sheet's own scriptworkers, and the&nbsp; <a href="https://wiki.roll20.net/Sheetworker_examples_for_Non-programmers" rel="nofollow">https://wiki.roll20.net/Sheetworker_examples_for_Non-programmers</a> &nbsp;I am still unsure how to begin "coding" it. Here is the code so far : &lt;h1&gt;Pokemon Mystery Dungeon Instinct&lt;/h1&gt; &lt;p&gt;Welcome to your sheet, &lt;span type="text" value="@Name")&lt;/span&gt;&lt;/p&gt; &lt;div class='3colrow'&gt; &lt;div class='col'&gt; &lt;p&gt;Level&lt;/p&gt;&lt;input type="number" name="attr_lvl" value="0" /&gt; &lt;p&gt;Strength&lt;/p&gt;&lt;input type="number" name="attr_str" value="0" /&gt; &lt;p&gt;Constitution&lt;/p&gt;&lt;input type="number" name="attr_con" value="0" /&gt; &lt;p&gt;Intelligence&lt;/p&gt;&lt;input type="number" name="attr_int" value="0"/&gt; &lt;p&gt;Wisdom&lt;/p&gt;&lt;input type="number" name="attr_wis" value="0" /&gt; &lt;p&gt;Dexterity&lt;/p&gt;&lt;input type="number" name="attr_dex" value="0"/&gt; Please help me!
1545900045
Finderski
Plus
Sheet Author
Compendium Curator
I'm making the assumption you have a field called maxhp: &lt;input type="number" name="attr_maxhp" readonly value="20" /&gt; I'd recommend making the field readonly and not disabled. Based on what I see above, it appears that default maxhp would be 20 (assuming con and lvl can be 0 as those are the default values). &nbsp;I've found that having a default value helps avoid potential issues down the road. This should do what you want (though it's untested...) on("change:con change:lvl", function() { getAttrs(["con","lvl"], function(v) { console.log("con = " + v.con); console.log("lvl = " + v.lvl); let conTotal = v.con * 3; let lvlTotal = v.lvl * 2; let maxHP = 20 + conTotal + lvlTotal; setAttrs({ maxhp: maxHP });&nbsp; }); }); This bit: on("change:con change:lvl", function() { watches for a change to either the con or the lvl fields. If a change is detected, it will execute the following code. Your field names are already lowercase, so that's good, the on("change:... thing will always need to look for lowercase fields, so if you had uppercase anywhere in those field names, you'd make this lowercase in this section. This bit: getAttrs(["con","lvl"], function(v) { retrieves the current values of con and level and puts them in v. I'm not a JS whiz, so I can't tell you what v is actually called...maybe an array? I'm sure someone can give you correct term... &nbsp;This portion should use the same case as your actual field names. &nbsp;For example, if your field was "Con" instead of "con", you'd need to put "Con" in this portion. This bit: console.log("con = " + v.con); console.log("lvl = " + v.lvl); outputs the current values of con and lvl to the console and helps you validate the values are what you would expect. &nbsp;This the Developer Tools console, I'm talking about. &nbsp;Also, if you used capital letters in the getAttrs portion, you'd use capital letters here, too. This bit: let conTotal = v.con * 3; let lvlTotal = v.lvl * 2; let maxHP = 20 + conTotal + lvlTotal; Creates three variables (conTotal, lvlTotal, maxHP) and assigns values to them. &nbsp;In this case conTotal = the value of v.con multiplied by three. &nbsp;I did this way just because it's easier to see each part is done correctly. This bit: setAttrs({ maxhp: maxHP });&nbsp; is where the field attr_maxhp to the value of the maxHP variable. If you have a default value set, it will be overwritten by this command and the new total will be displayed on the character sheet. I hope this helps.
1545902506
GiGs
Pro
Sheet Author
API Scripter
Unrelated to your issue, you have a couple of errors on this line: &lt;p&gt;Welcome to your sheet, &lt;span type="text" value="@Name")&lt;/span&gt;&lt;/p&gt; It should be &lt;p&gt;Welcome to your sheet, &lt;span type="text" value="@ { Name } " &gt; &lt;/span&gt;&lt;/p&gt; Notice the &gt; ending the span, and the brackets around Name.
1545912689

Edited 1545913471
Jakob
Sheet Author
API Scripter
GiGs said: Unrelated to your issue, you have a couple of errors on this line: &lt;p&gt;Welcome to your sheet, &lt;span type="text" value="@Name")&lt;/span&gt;&lt;/p&gt; It should be &lt;p&gt;Welcome to your sheet, &lt;span type="text" value="@ { Name } " &gt; &lt;/span&gt;&lt;/p&gt; Notice the &gt; ending the span, and the brackets around Name. Actually, it should probably be &lt;p&gt;Welcome to your sheet, &lt;span name="attr_name"&gt;&lt;/span&gt;&lt;/p&gt; if I am judging the intent here correctly. EDIT: &nbsp;syntax corrected.
1545912910
GiGs
Pro
Sheet Author
API Scripter
Finderski said: This bit: getAttrs(["con","lvl"], function(v) { retrieves the current values of con and level and puts them in v. I'm not a JS whiz, so I can't tell you what v is actually called...maybe an array? I'm sure someone can give you correct term... &nbsp;This portion should use the same case as your actual field names.&nbsp;&nbsp; For reference, it's called an object (javascript object), and it will look internally something like: v = {con: 3, lvl: 1}; with an object like this, you can call the values it containts using their keys . In this case, the keys are con and lvl .&nbsp; &nbsp; You can call them using&nbsp; v.con or v[con] , and you'll get con's value of 3. getAttrs creates an object like the one above, with each of the stats named as a key/value pair, with the key being the stats name, and the value being its, well, value. The&nbsp; function(v) part is where you name the object. Some people use v , because it's nice and short, others use values . It can be anything. If your getattrs line used function(values) , you'd get the values needed by using (for example) values.con . If you used&nbsp; function(artichokes) , you'd get the value of con using artichokes.con . Hope this is helpful!
1545913004
GiGs
Pro
Sheet Author
API Scripter
Jakob said: GiGs said: Unrelated to your issue, you have a couple of errors on this line: &lt;p&gt;Welcome to your sheet, &lt;span type="text" value="@Name")&lt;/span&gt;&lt;/p&gt; It should be &lt;p&gt;Welcome to your sheet, &lt;span type="text" value="@ { Name } " &gt; &lt;/span&gt;&lt;/p&gt; Notice the &gt; ending the span, and the brackets around Name. Actually, it should probably be &lt;p&gt;Welcome to your sheet, &lt;span name="attr_name"&lt;/span&gt;&lt;/p&gt; if I am judging the intent here correctly. That's intriguing. I'm a novice with html. How come that syntax is valid?&nbsp;
1545913447

Edited 1545913501
Jakob
Sheet Author
API Scripter
GiGs said: That's intriguing. I'm a novice with html. How come that syntax is valid?&nbsp; It's not, I introduced another error while changing the thing to a proper attribute-backed span... sorry.
1545913704
GiGs
Pro
Sheet Author
API Scripter
hehe, it happens to the best of us.
Finderski said: I'm making the assumption you have a field called maxhp: &lt;input type="number" name="attr_maxhp" readonly value="20" /&gt; I'd recommend making the field readonly and not disabled. Based on what I see above, it appears that default maxhp would be 20 (assuming con and lvl can be 0 as those are the default values). &nbsp;I've found that having a default value helps avoid potential issues down the road. This should do what you want (though it's untested...) on("change:con change:lvl", function() { getAttrs(["con","lvl"], function(v) { console.log("con = " + v.con); console.log("lvl = " + v.lvl); let conTotal = v.con * 3; let lvlTotal = v.lvl * 2; let maxHP = 20 + conTotal + lvlTotal; setAttrs({ maxhp: maxHP });&nbsp; }); }); This bit: on("change:con change:lvl", function() { watches for a change to either the con or the lvl fields. If a change is detected, it will execute the following code. Your field names are already lowercase, so that's good, the on("change:... thing will always need to look for lowercase fields, so if you had uppercase anywhere in those field names, you'd make this lowercase in this section. This bit: getAttrs(["con","lvl"], function(v) { retrieves the current values of con and level and puts them in v. I'm not a JS whiz, so I can't tell you what v is actually called...maybe an array? I'm sure someone can give you correct term... &nbsp;This portion should use the same case as your actual field names. &nbsp;For example, if your field was "Con" instead of "con", you'd need to put "Con" in this portion. This bit: console.log("con = " + v.con); console.log("lvl = " + v.lvl); outputs the current values of con and lvl to the console and helps you validate the values are what you would expect. &nbsp;This the Developer Tools console, I'm talking about. &nbsp;Also, if you used capital letters in the getAttrs portion, you'd use capital letters here, too. This bit: let conTotal = v.con * 3; let lvlTotal = v.lvl * 2; let maxHP = 20 + conTotal + lvlTotal; Creates three variables (conTotal, lvlTotal, maxHP) and assigns values to them. &nbsp;In this case conTotal = the value of v.con multiplied by three. &nbsp;I did this way just because it's easier to see each part is done correctly. This bit: setAttrs({ maxhp: maxHP });&nbsp; is where the field attr_maxhp to the value of the maxHP variable. If you have a default value set, it will be overwritten by this command and the new total will be displayed on the character sheet. I hope this helps. Thank you very much for the answer! I appreciate it a lot. I tested it and, it worked! I am grateful for the bit-by-bit explaining, I have made other worker scripts using yours as an base, and it worked for all of them. Thank you very much! GiGs said: Finderski said: This bit: getAttrs(["con","lvl"], function(v) { retrieves the current values of con and level and puts them in v. I'm not a JS whiz, so I can't tell you what v is actually called...maybe an array? I'm sure someone can give you correct term... &nbsp;This portion should use the same case as your actual field names.&nbsp;&nbsp; For reference, it's called an object (javascript object), and it will look internally something like: v = {con: 3, lvl: 1}; with an object like this, you can call the values it containts using their keys . In this case, the keys are con and lvl .&nbsp; &nbsp; You can call them using&nbsp; v.con or v[con] , and you'll get con's value of 3. getAttrs creates an object like the one above, with each of the stats named as a key/value pair, with the key being the stats name, and the value being its, well, value. The&nbsp; function(v) part is where you name the object. Some people use v , because it's nice and short, others use values . It can be anything. If your getattrs line used function(values) , you'd get the values needed by using (for example) values.con . If you used&nbsp; function(artichokes) , you'd get the value of con using artichokes.con . Hope this is helpful! Thank you for the explanation, that's an interesting way to put it. I'll keep it at function(v) then. GiGs said: Unrelated to your issue, you have a couple of errors on this line: &lt;p&gt;Welcome to your sheet, &lt;span type="text" value="@Name")&lt;/span&gt;&lt;/p&gt; It should be &lt;p&gt;Welcome to your sheet, &lt;span type="text" value="@ { Name } " &gt; &lt;/span&gt;&lt;/p&gt; Notice the &gt; ending the span, and the brackets around Name. I fixed it xD thank you for pointing it out.