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 .
×

Another [EASY]: Sheet Worker Help.

1518815795

Edited 1518815847
teekay1127
Sheet Author
Here is the sheet worker code: on('change:currenthp sheet:opened', function() {   getAttrs(['currenthp', 'greendtype', 'greenmin', 'greenmax', 'yellowdtype',         'yellowmin', 'yellowmax', 'reddtype', 'redmin', 'redmax', 'status' ], function(values) {     var index = parseInt(values.currenthp);     if (index <= 0) {       setAttrs({ 'status': '0' });     } else if (index <= 'redmax' && index >= 'redmin') {       setAttrs({ 'status': 'reddtype' });     } else if (index <= 'yellowmax' && index >= 'yellowmin') {       setAttrs({ 'status': 'yellowdtype' });     } else if (index <= 'greenmax' && index >= 'greenmin') {       setAttrs({ 'status': 'greendtype' });     }   }); }); Here is the HTML: <table style="width: 100% ; margin: 0 auto ; text-align: center ; padding: 2px">                     <tbody> <tr style="background-color: #99ffbb">                         <td style="width: 25%">Green</td>                         <td style="width: 25%">Min</td>                         <td style="width: 25%">Max</td>                         <td style="width: 25%">Current</td>                     </tr>                     <tr style="background-color: #99ffbb">                         <td>                             <select name="attr_greendtype" class="sheet-dtype" style="width: 100%">                                                 <option value="1d4">1d4</option>                                 <option value="1d6">1d6</option>                                 <option value="1d8">1d8</option>                                 <option value="1d10">1d10</option>                                 <option value="1d12">1d12</option>                             </select>                         </td>                         <td>                             <input type="number" name="attr_greenmin" class="sheet-green-min">                         </td>                         <td>                             <input type="number" name="attr_greenmax" class="sheet-green-max">                         </td>                         <td>                             <!-- <input type="radio" name="attr_status" class="sheet-status" value="@{greendtype}"> -->                         </td>                     </tr>                     <tr style="background-color: #ffff99">                         <td style="width: 25%">Yellow</td>                         <td style="width: 25%">Min</td>                         <td style="width: 25%">Max</td>                         <td style="width: 25%"></td>                     </tr>                     <tr style="background-color: #ffff99">                         <td>                             <select name="attr_yellowdtype" class="sheet-dtype" style="width: 100%">                                                 <option value="1d4">1d4</option>                                 <option value="1d6">1d6</option>                                 <option value="1d8">1d8</option>                                 <option value="1d10">1d10</option>                                 <option value="1d12">1d12</option>                             </select>                         </td>                         <td>                             <input type="number" name="attr_yellowmin" class="sheet-yellow-min">                         </td>                         <td>                             <input type="number" name="attr_yellowmax" class="sheet-yellow-max">                         </td>                         <td>                             <!-- <input type="radio" name="attr_status" class="sheet-status" value="@{yellowdtype}"> -->                         </td>                     </tr>                     <tr style="background-color: #ff5c33">                         <td style="width: 25%">Red</td>                         <td style="width: 25%">Min</td>                         <td style="width: 25%">Max</td>                         <td style="width: 25%"></td>                     </tr>                     <tr style="background-color: #ff5c33">                         <td>                             <select name="attr_reddtype" class="sheet-dtype" style="width: 100%">                                                 <option value="1d4">1d4</option>                                 <option value="1d6">1d6</option>                                 <option value="1d8">1d8</option>                                 <option value="1d10">1d10</option>                                 <option value="1d12">1d12</option>                             </select>                         </td>                         <td>                             <input type="number" name="attr_redmin" class="sheet-red-min">                         </td>                         <td>                             <input type="number" name="attr_redmax" class="sheet-red-max">                         </td>                         <td>                             <!-- <input type="radio" name="attr_status" class="sheet-status" value="@{reddtype}"> -->                         </td>                     </tr>                     <tr style="background-color: #bfbfbf">                         <td>                             OUT                         </td>                         <td>                             -                         </td>                         <td>                             <input type="text" name="attr_status" class="sheet-status">                         </td>                         <td>                             <!-- <input type="radio" name="attr_status" class="sheet-status" value="0"> -->                         </td>                     </tr>                     <tr style="background-color: #bfbfbf">                         <td>                             <b>HP</b>                         </td>                         <td>                             <input type="number" name="attr_currenthp" class="sheet-current-hp">                         </td>                         <td>                             <b>Defend</b>                         </td>                         <td>                             <input type="number" name="attr_currentdefend" class="sheet-current-defend">                         </td>                     </tr>                 </tbody>                 </table> The code used to function with Radio buttons that could be selected. Now I am trying to get it to work based off what ever the current HP is.  That way you can set the range for Green, Yellow, and Red, and when you change/load your current HP it uses the right dice size. Just trying to improve my sheet with automation. The selection functionality is fine, but I would like it to make this simple check and grab the right die value.  Probably some really silly mistake but let me know!
You're putting ' around your min and max values instead of using the values object (like you did for currenthp) else if (index <= 'redmax' && index >= 'redmin') should be else if (index <= values.redmax && index >= values.redmin)
1518863659

Edited 1518864607
GiGs
Pro
Sheet Author
API Scripter
I think it's a good idea (especially when still learning) to get into the habit of putting all the values you are using in variables at the top of the code. It makes writing the code a bit more laborious, but if you reuse values, or have long macros, it can make things clearer, and most importantly, you can then use console.log statements to check what their values are. (they will appear in the browser's dev console, which you can usually find by pressing f12). Another tip is its a good idea to just use a single setAttrs statement, at the end of the sheet worker. For this one which is just modifying a single attribute, it doesnt matter, but you'll eventually get into making workers that set multiple attributes. when that happens, you definitely want to group them into a single statement, to avoid unpredictable errors. Also its a good idea to use variable names that match the value they are calling. In the code below, for example, i renamed index to currenthp, which I think is more descriptive. One final tip: if you are using parseInt on cells that users can enter values into, it's a good idea to add a default value.  For instance parseInt('something')|||0 means that if the cell contains text, or not a number, you'll get a number in the code. Your if statements will fail without this, if the cells being called dont actually have numbers in them. Like so: on('change:currenthp sheet:opened', function() {   getAttrs(['currenthp', 'greendtype', 'greenmin', 'greenmax', 'yellowdtype',         'yellowmin', 'yellowmax', 'reddtype', 'redmin', 'redmax', 'status' ], function(values) {     var currenthp = parseInt(values.currenthp)||0; var redmin = parseInt(values.redmin)||0; var redmax = parseInt(values.redmax)||0; var greenmax = parseInt(values.greenmax)||0; var greenmin = parseInt(values.greenmin)||0; var yellowmax = parseInt(values.yellowmax)||0; var yellowmin = parseInt(values.yellowmin)||0; var reddtype = values.reddtype; var yellowdtype = values.yellowdtype; var greendtype = values.greendtype; // this allows you to use something like // console.log("yellowmin:" + yellowmin); // to print the output to the browsers dev console, and check individual values. // It's a good way of tracking down errors when they occur. var status = 0; // create a variable to hold the value you'll be setting the attribute to later.     if (currenthp <= 0) {       status = 0; // note, if this is a dtype variable, you might want to change this to 'd0' - not sure.     } else if (currenthp <= redmax && currenthp >= redmin) {       status = reddtype;     } else if (currenthp <= yellowmax && currenthp >= yellowmin) {       status = yellowdtype;     } else if (currenthp <= greenmax && currenthp >= greenmin) {       status = greendtype;     }     setAttrs({ 'status': status });   }); });
1518864140

Edited 1518891044
GiGs
Pro
Sheet Author
API Scripter
Also I;m wondering if you need all of a greenmin/max yellowmin/max and redmin/max I think you should probably only need two values. By default its green  If it drops below the yellow threshold, its yellow  if it drops below the red threshold it's red and if it drop below 0, it's zero As it stands, it seems like if you aren't careful, you could easily end up with numbers that fall into none of the thresholds above. Imagine a green min/max of 32-60, a yellow min/max of 15-30, and a red min/max of 5-10. In this case, values of 1-4, 11-14, and 31 dont match any of the if statements, and so wont have a status set. (I'm just inventing number ranges here because I dont know the system you are using, but it should be illustrative of the point I'm making.) If these values are set manually, its going to be very easy to create situations like this by accident. Whereas the bulleted list i suggest above is much simpler and less error-prone.
1518883916
vÍnce
Pro
Sheet Author
I'm loving all the info that's being shared in this thread.  Thank you!
I'm guessing the system is the Sentinel Comics RPG, in which case yes, simple thresholds should be easier as there shouldn't be any values outside or overlapping those ranges (except 0).
It is, I am super excited for the devs to get to the pull requests! Vince, G G, and Author X- you guys have been awesome and really made me making my first sheet possible. It was just a flat and un-automated sheet before. I really did learn a lot about how sheet worker handles variables.   My final version is in the pull requests if you wanna see how it all came together. 
I'm just figuring this out and building my first sheet too, so I'm glad I could help a bit!
Yeah I am new to coding. I'm part of a degree program that is super accelerated so in 6 months I've done C#, Java, JS, HTML, CSS, SQL, PHP, and worked in many different IDE's. The info starts to just all blend together until you actually put it to practical use. So I will certainly chime in and give back when I can!  Have fun making your sheet! Is it for a custom game or a retail game? 
It's for  Fellowship , a Powered by the Apocalypse system, so I just used the existing Apocalypse World sheet as a base but there are a lot of unique system quirks (like an "Iron" stat that the Dwarf playbook starts with, but any playbook could get by sharing a Dwarf move). I'm going to run it for friends, some of whom are new to RPGs and Roll20, so I want to make it as easy and automated as I can.