- Anatomy of a Sheet Worker
- Events, and watching Attributes
- Variables – How to Name Things
- Arithmetic in Sheet Workers
- What If? in Sheet Workers
- Logging in the Browser Console
- Strings, Arrays, and Loops
- Asynchronicity and Things to Avoid With Loops
- Changes and the eventInfo Object
- Action Buttons
- setAttrs and Saving Attributes
- A Sheet Worker Reprise
- Castle Falkenstein Design – Sheet Workers
- The Perils of Sheet Worker Functions
- The Script Block and Identifying Characters
- Arrays and Dropdowns
- Undefined and Other Error Values
- The Ternary Operator – The One-Line If
- Template Literals
- Functions and the Fat Arrow
- Strings in Sheet Workers
Possibly have a separate article for each type of these
- Number conversion: int and num
- Changes – avoiding typing change: over and over
- sum/produce: directly targetting the values object
- Fat Arrow Syntax – difference between function() and fat arrow syntax
This post is for those who already know how to use Sheet Workers and is a collection of function snippets I use in almost every character sheet.
What is a Function?
A function is any block of code you want to run on its own. Whenever you repeat code in a worker, you should make a function of it, and make it simpler to write.
In Roll20 character sheets, you often want to grab the attribute, coerce it to a number, and put in a variable. For example you do this:
You might do that dozens or hundreds of times in a single character sheet, and might even do it several times in the same worker. So, you could copy much of that out into a function, a little bit of repeatable code, to save a bunch of typing.
In this example, we have created a function called int. We create it once, at the start of the script block, and then can use it in every sheet worker in the format described. Typing int() is much simpler than typing parseInt() || 0 each time.
There are other advantages too. For example, if you want to add two numbers together, you must enclose each in a set of brackets, like so:
That first one is deceptive. It might look correct, but bear in mind the position of the || boolean check. That says:
- calculate parseInt(values.stat1);
- if that is an error, calculate parseInt(values.stat2),
- and if that is also an error, the result is 0.
This is one reason it’s better to separate grabbing each stat into its own calculation, like the last example, but using a function to calculate int avoids that problem.
Whenever you find yourself repeating similar code, it’s a good idea to figure out how to turn it into a function.
In the above example, we created a score parameter. Scores are temporary variables created within the function. You pass a value to it, and the function treats the parameter as that value.
You can create multiple parameters and even given them default values.
In the first of the following two examples, we assume the default is going to be zero so we don’t have to specify it. In the second example, we specify a different default.
Separate parameters with commas.
The Big Reason
The main reason to create functions is to reduce errors. If you have the same code repeated three times, or a hundred times, in your sheet workers, and decide you need to change them, you have to go through all that code and change them all. It’s very easy to create a small mistake in one or more of them.
If you use a function, you only have one set of code to change, and if you have an error in that code, you’ll spot it and correct it more easily. When you correct it for one place, it is corrected for all places that function might be used. When you do a lot of programming, this is incredibly useful.
Fat Arrow Syntax
There’s another way you can write functions, called the fat arrow syntax. This affects the first line of functions. The following two are identical*:
Notice that this replaces the word function with a variable declaration (const or let), then an = before the parameters, then that fat arrow.
One big advantage of fat arrow syntax is the ability to create a function as a single line item and removing the return statement. These are both the same way of writing the same thing:
Think of the fat arrow as “goes to”. I find the second version easier to write. But if there are multiple complicated statements in the function, you will need to use a code block.
Standard Roll20 Functions
Roll20 includes a lot of functions that aren’t explicitly called out as functions. The event line of a worker, and the getAttrs and setAttrs lines are all functions. You can use the fat arrow syntax with these.
It’s very common to write above with fat arrow syntax, like this:
Whenever you have the word function you can remove it on the left of the parameters, and put the fat arrow on the right side.
In practice, it doesn’t matter which you use, but the fat arrow syntax saves a bit of writing.
You can use this technique with a lot of code. Remember to look for the word function. I often use it with forEach loops. Here’s a standard loop:
This code loops through the six attributes and adds their value to the output object. This uses a lot of techniques already covered in this series – for example, the int function, and creating an output object using a template literal.
Here’s one way to write the code using Fat Arrow syntrax.
And here’s an extremely compact version of the same code.
I personally wouldn’t compact the code this much, but it shows what can be done with the Fat Arrow syntax.
I’ll shortly make another post describing some commonly helpful functions and may expand that file as time goes on. Watch for the Function Library post.