- 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
This series will guide you in how to write better Sheet Workers. You don’t need everything in this series – you can just refer back to the previous post for all the basics. But if you want to write better sheet workers, you’ll need to understand variables.
- Beginning coders use variables because they don’t know what they are doing, and are following guides like this one which show how to use them.
- Intermediate coders who have enough knowledge to be dangerous. They often omit variables where they can because they think it makes their code better.
- More advanced coders use variables because they realise it makes their code easier to debug, and it makes their code easier to understand for those of all skill levels that follow them.
What is a Variable?
A variable is a placeholder. You use them already, and all the time. When you remember that a banana is $10, and realise you can get 5 of them with $50, you have used a variable and also performed some algebra.
let banana_cost = 10; let money = 50; let my_bananas = money / banana_cost; let money_left = money - (my_bananas * banana_cost);Code language: Markdown (markdown)
So basically you create a variable for everything you want to use in your code.
There’ll be more about comments in the post on Logging and Errors.
Creating – or Defining – a variable is done in a specific way, using keywords of let, const, or var.If you aren’t sure which to use always use let. The code in the first tab could have been written this way:
- let: use this when you are creating a variable that can change.
- const: use this when creating a variable which cannot change.
- var: this is a now-obsolete way of creating variables. You will see older sheet workers using it, but for good practice, you should replace any vars with lets.
This can easily trip you up. There are things you must do, and things you should do.
- Variable names cannot begin with a number
- Variable names can contain only letters and numbers- no spaces or special characters
Here are some rules to follow that you can break, but following them will make your life easier:
- Use all-lower-case for variable names.
- Use snake case (snake_case) for variable names. Avoid other naming conventions (camelCase, PascalCase, or kebab-case).
- Use these same rules in html and in sheet workers because you can sometimes use html names in sheet workers.
The idea is to always divide multiple words with _ (underscores) rather than spaces, and always use lower-case. There are some situations in Roll20 where this can cause problems (repeating section and action button names should have exactly one underscore) and in these cases it’s better to just use one single word (or kebab-case if you must have multiple words), but those are infrequent and you don’t need to worry about them yet.
The Most Common Types of Variable and Data
This can be convenient, but it can also be frustrating, especially when you need a variable to be of one type of data (which does happen). I’ll explain how to coerce variables into one type or another later, but for now here are the main types of variable you will encounter.
A string is basically any single value that isn’t a number, typically words and sentences. Strings are enclosed in quotes, like “Harry”, “This is a string”, and “3”. Notice that last one looks like a number, but because it is in quotes it is a string. You’ll find what difference that makes soon.
By default, Roll20 stores all data as Strings, so if you want to perform arithmetic (and usually you do), you need to convert that data to a number. I’ll describe how to do that in the next few tabs.
The main thing you do with strings is concatenation – which is the technical way of saying “combining together”. This confusingly uses +, so it looks just like arithmetic.
Integers are “whole numbers” like 0, 1, 2, 3000, -5, and so on. Outside of things like encumbrance, this is probably most of what you want to use in your sheet workers.
Integers can be positive or negative but do have a maximum value, a ridiculously large number: 9,007,199,254,740,991. If you somehow exceed that, you’ll get an error (an undefined value) – you’ll learn more about those in the post on Logging and Errors.
This does mean if you specifically want an integer, you might have to create one. You can use the parseInt() function to do this (and parseFloat() to make sure you have a floating point number).
There are other ways to do this – we’ll get more into that in the post about arithmetic.
Booleans are special variables that can only have two values, true and false.
- 0 (the number zero)
- “” (an empty string)
- false (the boolean value)
- NaN (Not A Number, a special value we’ll explore later)
- null (another special constant)
- undefined (another special property you’ll see a lot more of later).
If the value is not one of those 6 values, it is truthy (which is like true, and you can treat as if they were true for logical tests).
You’ll learn about logical tests later, and boolean values and truthy/falsy values will be very important.
An array is not strictly speaking a data type itself. It is a collection of things, which can themselves be of any data type including other arrays!
You’ll learn more about arrays later, but the main thing you need to know is you can grab individual items by their index, and the index starts at 0. This index is more properly called a key.
first_index grabs filled_array’s first item, so has a value of ‘a string’. second_index has a value of 50.
You can extract data from an object with two syntaxes:
You’ll learn more about these later. But basically, use the dot syntax when you know the item’s name and it is a valid variable name. Otherwise, use the bracket syntax.