Character Vault
Any Concept / Any System
Compendium
Your System Come To Life
Roll20 for Android
Streamlined for your Tablet
Roll20 for iPad
Streamlined for your Tablet

Personal tools

Difference between revisions of "Sheet Worker Snippets"

From Roll20 Wiki

Jump to: navigation, search
m (parseValues)
m (Example 2)
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
''Main Article:'' '''[[Sheet Worker Scripts]]'''
  
 
== Auto-calculating Attributes==
 
== Auto-calculating Attributes==
Line 5: Line 6:
  
 
Your best bet would be to avoid the autocalc fields entirely if you can. Monitor the two other fields and when they change, have a sheetworker that adds them up to the new value. Then you can refer to the new value in other calculations much easier.
 
Your best bet would be to avoid the autocalc fields entirely if you can. Monitor the two other fields and when they change, have a sheetworker that adds them up to the new value. Then you can refer to the new value in other calculations much easier.
<pre>
+
<pre data-language="javascript">
 
on("sheet:opened change:stat_a change:stat_b", function() {
 
on("sheet:opened change:stat_a change:stat_b", function() {
 
     getAttrs(["stat_a", "stat_b"], function(values) {
 
     getAttrs(["stat_a", "stat_b"], function(values) {
 
         setAttrs({
 
         setAttrs({
             "foo_modchars": parseInt(values["stat_a"],10) || 0 + parseInt(values["stat_b"],10) || 0
+
             "foo_modchars": parseInt(values["stat_a"]) || 0 + parseInt(values["stat_b"]) || 0
 
         });
 
         });
 
     });
 
     });
 
});
 
});
 
</pre>
 
</pre>
 +
 
===Example 2===
 
===Example 2===
 
(credit: [https://app.roll20.net/users/157788 GiGs])
 
(credit: [https://app.roll20.net/users/157788 GiGs])
Line 22: Line 24:
  
 
I generally don't put my working in the setattrs call, but before it so i can more easily check it. Something like
 
I generally don't put my working in the setattrs call, but before it so i can more easily check it. Something like
<pre>
+
<pre data-language="javascript">
 
on("sheet:opened change:stat_a change:stat_b", function() {
 
on("sheet:opened change:stat_a change:stat_b", function() {
 
   getAttrs(["stat_a", "stat_b"], function(values) {
 
   getAttrs(["stat_a", "stat_b"], function(values) {
       var stat_a = parseInt(values["stat_a"],10)||0;
+
       var stat_a = parseInt(values["stat_a"])||0;
       var stat_b = parseInt(values["stat_b"],10)||0;
+
       var stat_b = parseInt(values["stat_b"])||0;
 
       var output = stat_a + stat_b;
 
       var output = stat_a + stat_b;
 
       setAttrs({
 
       setAttrs({
Line 40: Line 42:
 
(credit: [https://app.roll20.net/users/157788 GiGs])
 
(credit: [https://app.roll20.net/users/157788 GiGs])
 
Many sheet workers have a bunch of lines like this:
 
Many sheet workers have a bunch of lines like this:
<pre>
+
<pre data-language="javascript">
       var stat_a = parseInt(values["stat_a"],10)||0;
+
       var stat_a = parseInt(values["stat_a"])||0;
       var stat_b = parseInt(values["stat_b"],10)||0;
+
       var stat_b = parseInt(values["stat_b"])||0;
 
</pre>
 
</pre>
 
You might also have lines like this:
 
You might also have lines like this:
<pre>
+
<pre data-language="javascript">
 
setAttrs({
 
setAttrs({
 
   "foo_modchars": parseInt(values["stat_a"],10) || 0 + parseInt(values["stat_b"],10) || 0
 
   "foo_modchars": parseInt(values["stat_a"],10) || 0 + parseInt(values["stat_b"],10) || 0
Line 51: Line 53:
 
</pre>
 
</pre>
 
It gets tedious typing out all that. With the function below, you would instead write them as:
 
It gets tedious typing out all that. With the function below, you would instead write them as:
<pre>
+
<pre data-language="javascript">
var stat_a = parseValues("stat_a");
+
var stat_a = parseValues(values,"stat_a");
var stat_b = parseValues("stat_b");
+
var stat_b = parseValues(values,"stat_b");
 
setAttrs({
 
setAttrs({
   "foo_modchars": parseValues("stat_a") + parseValues("stat_b")
+
   "foo_modchars": parseValues(values,"stat_a") + parseValues(values,"stat_b")
 
});
 
});
 
</pre>
 
</pre>
Line 61: Line 63:
 
==== parseValues ====
 
==== parseValues ====
 
Place this at the start of your script block, and you'll be able to use it in all your sheet workers.
 
Place this at the start of your script block, and you'll be able to use it in all your sheet workers.
<pre>
+
<pre data-language="javascript">
 
const parseValues = (values, stat, type='int') => {
 
const parseValues = (values, stat, type='int') => {
     if(type === 'int') return parseInt(values[stat],10)||0;
+
     if(type === 'int') return parseInt(values[stat])||0;
     else if(type === 'float') return parseFloat(values[stat],10)||0;
+
     else if(type === 'float') return parseFloat(values[stat])||0;
 
     else if(type === 'str') return values[stat];
 
     else if(type === 'str') return values[stat];
 
};
 
};
 
</pre>
 
</pre>
 
By default, it returns an integer. If you call it with a second parameter, it will return either a float or a string:
 
By default, it returns an integer. If you call it with a second parameter, it will return either a float or a string:
* parseValues(values, stat) or parseValues(values, stat, 'int') - returns an integer.
+
* <code>parseValues(values, stat)</code> or <code>parseValues(values, stat, 'int')</code> - returns an integer.
* parseValues(values, stat,'float') - returns a Float (a number that is not an integer)
+
* <code>parseValues(values, stat,'float')</code> - returns a Float (a number that is not an integer)
* parseValues(values, stat, 'str') - returns the value as text. (Not really needed!)
+
* <code>parseValues(values, stat, 'str')</code> - returns the value as text. (Not really needed!)
  
 
This function does handle variable attribute names. If you were in a loop and creating attributes like, '''"stat" + i''' it will work fine.
 
This function does handle variable attribute names. If you were in a loop and creating attributes like, '''"stat" + i''' it will work fine.
Line 78: Line 80:
 
__FORCETOC__
 
__FORCETOC__
  
==See Also==
+
==Related Pages==
 +
* '''[[:Category:Sheetworker|List of all Sheetworker-articles]]'''
 
* [[Sheetworker_examples_for_Non-programmers|Sheetworker Examples for Non-programmers]]
 
* [[Sheetworker_examples_for_Non-programmers|Sheetworker Examples for Non-programmers]]
* [[UniversalSheetWorkers|Universal Sheet Workers]]
+
* [[UniversalSheetWorkers|Universal Sheet Workers]] - How to create one function that can handle a bunch of similar sheet workers
* [[repeatingSum|The RepeatingSum Function]]
+
* [[repeatingSum|The RepeatingSum Function]] - How to add up the weight of all items in a repeating section
 +
 
 +
==See Also==
 +
* [https://app.roll20.net/forum/post/8033979/advice-api-sheet-workers-and-performance/?pageforid=8034567#post-8034567 Sheet Worker Optimization] by Scott C.
 
* [https://app.roll20.net/forum/post/6963354/build-lookup-table-into-a-character-sheet/?pageforid=6964447#post-6964447 How to integrate table of stats into a sheet]
 
* [https://app.roll20.net/forum/post/6963354/build-lookup-table-into-a-character-sheet/?pageforid=6964447#post-6964447 How to integrate table of stats into a sheet]
 +
* [https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps Introduction to JavaScript] - MDN web docs
 +
* [https://developer.mozilla.org/en-US/docs/Learn/Accessibility/CSS_and_JavaScript#JavaScript JavaScript Best Practices] - MDN web docs
  
 
<br>
 
<br>
 
<br>
 
<br>
  
[[Category:Tips]]
 
[[Category:User content]]
 
 
[[Category:Sheetworker]]
 
[[Category:Sheetworker]]
 +
[[Category:Character Sheet Creation]]

Revision as of 20:09, 3 June 2020

Main Article: Sheet Worker Scripts

Contents

Auto-calculating Attributes

Example 1

(credit: Rabulias)

Your best bet would be to avoid the autocalc fields entirely if you can. Monitor the two other fields and when they change, have a sheetworker that adds them up to the new value. Then you can refer to the new value in other calculations much easier.

on("sheet:opened change:stat_a change:stat_b", function() {
    getAttrs(["stat_a", "stat_b"], function(values) {
        setAttrs({
            "foo_modchars": parseInt(values["stat_a"]) || 0 + parseInt(values["stat_b"]) || 0
        });
    });
});

Example 2

(credit: GiGs)

I remember seeing a script someone wrote to allow you to use autocalc fields within sheet workers, but it's just simpler to use Rabulias's approach(see example 1 above).

Add the relevant stats to the on(change:) line, and duplicate the calculation within the sheet worker.

I generally don't put my working in the setattrs call, but before it so i can more easily check it. Something like

on("sheet:opened change:stat_a change:stat_b", function() {
  getAttrs(["stat_a", "stat_b"], function(values) {
      var stat_a = parseInt(values["stat_a"])||0;
      var stat_b = parseInt(values["stat_b"])||0;
      var output = stat_a + stat_b;
      setAttrs({
        "foo_modchars": output
      });
  });
});

Helper Functions

This section is for useful functions that aren't complete sheet workers, but are useful to use in sheet workers.

Function: parseValues

(credit: GiGs) Many sheet workers have a bunch of lines like this:

      var stat_a = parseInt(values["stat_a"])||0;
      var stat_b = parseInt(values["stat_b"])||0;

You might also have lines like this:

setAttrs({
   "foo_modchars": parseInt(values["stat_a"],10) || 0 + parseInt(values["stat_b"],10) || 0
});

It gets tedious typing out all that. With the function below, you would instead write them as:

var stat_a = parseValues(values,"stat_a");
var stat_b = parseValues(values,"stat_b");
setAttrs({
   "foo_modchars": parseValues(values,"stat_a") + parseValues(values,"stat_b")
});

I think that's a lot easier to read. Here's the function:

parseValues

Place this at the start of your script block, and you'll be able to use it in all your sheet workers.

const parseValues = (values, stat, type='int') => {
     if(type === 'int') return parseInt(values[stat])||0;
     else if(type === 'float') return parseFloat(values[stat])||0;
     else if(type === 'str') return values[stat];
};

By default, it returns an integer. If you call it with a second parameter, it will return either a float or a string:

  • parseValues(values, stat) or parseValues(values, stat, 'int') - returns an integer.
  • parseValues(values, stat,'float') - returns a Float (a number that is not an integer)
  • parseValues(values, stat, 'str') - returns the value as text. (Not really needed!)

This function does handle variable attribute names. If you were in a loop and creating attributes like, "stat" + i it will work fine.



Related Pages

See Also