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 Scripts"

From Roll20 Wiki

Jump to: navigation, search
(eventInfo Object: Add information on newValue, previousValue, and removedInfo)
(Structure of a Sheetworker)
(42 intermediate revisions by 14 users not shown)
Line 1: Line 1:
Sheet Worker Scripts are an advanced feature of the Character Sheets system which allows the sheet author to specify JavaScript which will execute during certain events, such as whenever the values on a sheet are modified.
+
=Sheetworkers=
 +
<div style="background:#f0e2a1; border: 3px solid #dbc870; padding: 10px;">
 +
<big>'''''Attention:'''''
 +
''Roll20 is no longer maintaining this document on the community wiki. For the most up-to-date information please visit this page on our help center for assistance: [https://roll20.zendesk.com/hc/en-us/articles/360037773513-Sheet-Worker-Scripts Here].''</big>
 +
</div>
 +
''Main Article:'' '''[[Building Character Sheets]]'''
  
==Adding a Sheet Worker Script==
+
'''Sheet Worker Scripts'''(AKA simply as "sheetworkers"), are an advanced feature of the [[Building_Character_Sheets|Character Sheets]] system, which allows the sheet author to specify [https://developer.mozilla.org/en-US/docs/Web/javascript JavaScript] which will execute during certain events, such as whenever the attribute values on a character sheet are modified.
  
To add a sheet worker script to a Character Sheet, simply add the script into the "HTML" section of the sheet using the following format:
+
[[Sheetworker examples for Non-programmers]], [[Sheet Worker Snippets]] and [[UniversalSheetWorkers|Universal Sheetworkers]] are the best starting points for practical and working examples on how to use sheetworkers.
 +
 
 +
This page gives a general overview of the available Roll20-specific features that can be used in sheetworkers, but is not a great guide for practical implementations. The pages found under the [[Sheet Worker Scripts#Related_Pages|Related Pages]] and [[Sheet Worker Scripts#See_Also|See Also]]-sections have links to more examples of how to use sheetworkers.
 +
 
 +
__TOC__
 +
 
 +
==General==
 +
[https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps Introduction to JavaScript] - MDN web docs
 +
 
 +
===JavaScript Restrictions===
 +
Many JavaScript functions or functionalities can't be used in Roll20 character sheets due to the [[Building_Character_Sheets#Restrictions|security filter]]. and one should check existing sheets for examples of what can be used, if you're attempting to do any slightly more advanced  data-handling.
 +
* [https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model DOM] can't be used directly in basically any way, so event listeners like <code>[https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onclick onclick]</code> won't work.
 +
 
 +
===Structure of a Sheetworker===
 +
Sheetworkers in Roll20 character sheets are pieces of JavaScript, intertwined with a few custom features made specifically for handling Roll20 character sheet information. All the sheetworkers in a character sheet are places inside a <code><nowiki><script type="text/worker"></nowiki></code>-element.
 +
 
 +
Usually a sheetworker works as follows.
 +
 
 +
'''1.''' Listens for changes to the sheet with an '''Event Listener''' (<code>"change:attributename</code> <code>sheet:opened</code>) (explained in next section) <br>
 +
'''2.''' Retrieves values of a number of attributes from the sheet with the <code>getAttrs</code> function, usually including the attributes that were monitored.<br>
 +
'''3.''' Create some temporary variables and uses the values received from <code>getAttrs</code> and does something based on the info using normal JavaScript.<br>
 +
'''4.''' Save to the character sheet some of the new values made in '''Step 3''' to some number of attributes with the <code>setAttrs</code> function.<br>
 +
 
 +
 
 +
The following is an example of a single sheetworker placed in a <code><nowiki><script type="text/worker"></nowiki></code>-element, taken from the [[Sheetworker examples for Non-programmers]]-page.
 +
 +
<pre data-language="javascript">
 +
on("change:siz change:con sheet:opened", function() { 
 +
  getAttrs(["SIZ","CON"], function(values) {
 +
    let siz = parseInt(values.SIZ)||0;
 +
    let con = parseInt(values.CON)||0;
 +
    let hp = siz + con;
 +
    setAttrs({                           
 +
      "hitpoints": hp
 +
    });
 +
  });
 +
});
 +
</pre>
 +
 
 +
'''1. Event listener'''
 +
 
 +
<code>on("change:siz change:con sheet:opened", function()</code> is the Event listener, which checks the character sheet for changes to specific attributes, if the sheet have been opened, or if a button have been pressed. In the example, the sheetworker is checking if the <code>siz</code> or <code>con</code>-attribute have been changed, or if the sheet have been opened. These two represents the size and constitution of the character.
 +
 
 +
'''2. Value retrieval'''
 +
 
 +
<code>getAttrs(["SIZ","CON"], function(values)</code> looks up the values of the <code>siz</code> or <code>con</code>-attributes on the character sheet, so they can be used in the sheetworker.
 +
 
 +
 
 +
'''3. Making changes'''
 +
 
 +
<pre data-language="javascript">
 +
    let siz = parseInt(values.SIZ)||0;
 +
    let con = parseInt(values.CON)||0;
 +
    let hp = siz + con;
 +
</pre>
 +
 
 +
This is the main part of the sheetworker. It first defines two temporary varible, <code>siz</code> and <code>con</code>, that saves the values of their attributes in a way that JavaScript knows it's a number.
 +
 
 +
On the third row, it defines the <code>hp</code>-varaible to be <code>siz + con</code>, which means that the characters Hit Points are equal to the sum of their Size and Constitution.
 +
 
 +
'''4. Saving the changes'''
 +
 
 +
<pre data-language="javascript">
 +
    setAttrs({                           
 +
      "hitpoints": hp
 +
    });
 +
</pre>
 +
 
 +
Finally, the sheetworker decides to define the character sheet's <code>hitpoints</code>-attribute to be equal to the newly calculated <code>hp</code>-variable, which is the last action of the sheetworker. Note that it does not make changes to the attributes it was listening to.
 +
 
 +
'''Other things'''
 +
 
 +
Outside of this, there may exist some other things saved in the <code><nowiki><script type="text/worker"></nowiki></code>, such as:
 +
 
 +
* Saved list of information, used by the sheetworkers.
 +
** '''Example:''' <code>const stats = ["strength", "intelligence", "charm", "arcana", "grace"];</code>
 +
* the Event Listener might loop through some JavaScript, that decides what attributes to listen/change/save
 +
** '''Example:''' <code>stats.forEach(stat => { some code });</code>
 +
* Define [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions JavaScript functions] to be used in the sheetworkers, instead of repeating the same code snippets in severa places.
 +
 
 +
===Adding a Sheetworker===
 +
 
 +
To add a sheetworker to a Character Sheet, simply add the script to the bottom of your <code>HTML</code>-section of the sheet using the following format:
  
 
<pre data-language="javascript">
 
<pre data-language="javascript">
Line 17: Line 104:
 
</pre>
 
</pre>
  
Note that the script tag must have a type of "text/worker" to be properly handled by the system. When the game loads, all script tags are removed from the sheet's template for security purposes. However, script tags with a type of "text/worker" will be used to spin up a background worker on each player's computer that can respond to changing values in their sheets and take action as needed.
+
The <code><nowiki><script></nowiki></code>-element '''must''' have a type of <code>"text/worker"</code> to be properly handled by the system. When the game loads, all script tags are removed from the sheet's template for security purposes. However, <code><nowiki><script></nowiki></code>-elements with a type of <code>"text/worker"</code> will be used to spin up a background worker on each player's computer, that can respond to changing values in their sheets and take action as needed.
 +
 
 +
All sheetworker should be contained within a single <code><nowiki><script type="text/worker"></nowiki></code> element, and preferably be placed at the bottom of the <code>html</code>-file.
 +
 
 +
'''[[Sheetworker_examples_for_Non-programmers|Sheetworker examples for Non-programmers]]'''
 +
 
 +
'''Warning about "global" variable scope:''' the scope of a variable declared ''inside'' the `<script type="text/worker">` but ''outside'' a function are currently per '''player''', not per character. Changing its value will change it for all the characters of your player's session.
 +
 
 +
As a best practice Asynchronous cascades should be avoided whenever possible. It is generally a better practice to get all attributes needed for calculations in one getAttrs rather than doing getAttrs -> calculations -> setAttrs -> getAttrs -> calculations -> setAttrs…
  
==Sheet Workers vs. Auto-Calculating Fields: Which should I use?==
+
===Sheet Workers vs. Auto-Calculating Fields: Which should I use?===
  
 
There's no hard-and-fast rule about this. Both of these tools are capable of achieving the same thing. Let's take a hypothetical use-case of a "Strength" attribute, which we want to use to keep a "Strength Mod" attribute updated. Here would be the differences between the two tools for this use case:
 
There's no hard-and-fast rule about this. Both of these tools are capable of achieving the same thing. Let's take a hypothetical use-case of a "Strength" attribute, which we want to use to keep a "Strength Mod" attribute updated. Here would be the differences between the two tools for this use case:
Line 33: Line 128:
  
 
==Sheet Worker API==
 
==Sheet Worker API==
 +
A list of sheetworker features accessible in Roll20. Not to be confused with the [https://roll20.zendesk.com/hc/en-us/articles/360037256714-API Roll20 API].
  
 
===Events===
 
===Events===
Line 71: Line 167:
 
on("change:strength change:StrengthMod change:StrengthOtherThing", function(eventInfo) {
 
on("change:strength change:StrengthMod change:StrengthOtherThing", function(eventInfo) {
 
   //Do something here
 
   //Do something here
 +
  // eventInfo.previousValue is the original value of the attribute that triggered this event, before being changed.
 +
  // eventInfo.newValue is the current value of the attribute that triggered this event, having been changed.
 
});
 
});
  
Line 76: Line 174:
 
   //Do something here
 
   //Do something here
 
   // eventInfo.sourceAttribute is the full name (including repeating ID) of the attribute  
 
   // eventInfo.sourceAttribute is the full name (including repeating ID) of the attribute  
//that originally triggered this event,  
+
  // that originally triggered this event,  
 
   // however the entire string will have been translated into lowercase and thus might not be  
 
   // however the entire string will have been translated into lowercase and thus might not be  
//suitable for being fed directly
+
  // suitable for being fed directly
 
   // into getAttrs() or other uses without being devandalized first.
 
   // into getAttrs() or other uses without being devandalized first.
 
});
 
});
Line 85: Line 183:
 
   //Would be triggered when any attribute in the repeating_spells section is changed
 
   //Would be triggered when any attribute in the repeating_spells section is changed
 
   // eventInfo.sourceAttribute is the full name (lowercased)(including repeating ID)  
 
   // eventInfo.sourceAttribute is the full name (lowercased)(including repeating ID)  
//of the attribute that originally triggered this event.  
+
  // of the attribute that originally triggered this event.  
 
});
 
});
  
Line 104: Line 202:
 
whenever a repeating section is re-ordered.  
 
whenever a repeating section is re-ordered.  
 
<pre data-language="javascript">
 
<pre data-language="javascript">
on("change:_reporder_repeating_<sectionname>", function(eventInfo) {
+
on("change:_reporder:<sectionname>", function(eventInfo) {
 
   // Where <sectionname> above should be a repeating section name, such as skills or spells
 
   // Where <sectionname> above should be a repeating section name, such as skills or spells
 
});
 
});
Line 130: Line 228:
  
 
====sheet:opened====
 
====sheet:opened====
This event will fire the first time a sheet is opened by a player in a game session. Note that it will fire again the next game session, and it most likely will fire on multiple people's computers (each time a person opens the sheet). However, it should be useful for doing things like checking for needed sheet upgrades.
+
This event will fire every time a sheet is opened by a player in a game session. It should be useful for doing things like checking for needed sheet upgrades.
  
 
<pre data-language="javascript">
 
<pre data-language="javascript">
Line 137: Line 235:
 
// Do something the first time the sheet is opened by a player in a session
 
// Do something the first time the sheet is opened by a player in a session
  
 +
});
 +
</pre>
 +
 +
====clicked:<button_name>====
 +
 +
This event will trigger when an action button is clicked. The button's name will need to start with "act_". For example:
 +
 +
<button type="action" name="act_activate">Activate!</button>
 +
 +
<pre data-language="javascript">
 +
on("clicked:activate", function() {
 +
  console.log("Activate button clicked");
 
});
 
});
 
</pre>
 
</pre>
Line 144: Line 254:
 
====getAttrs(attributeNameArray, callback) {{Asynchronous}} ====
 
====getAttrs(attributeNameArray, callback) {{Asynchronous}} ====
  
The getAttrs function allows you to get the values of a set of attributes from the sheet. The "_max" suffix is supported, so <code>getAttrs( ["Strength", "Strength_max"], func)</code> will get both the current and max values of "Strength". Note that the function is asynchronous, which means that there is no guarantee that the order in which multiple getAttrs calls are made is the order in which the results will be returned. Rather, you pass a callback function which will be executed when the values have been calculated. The callback function receives a simple Javascript object with a list of key-value pairs, one for each attribute that you requested.
+
The <code>getAttrs</code> function allows you to get the values of a set of attributes from the sheet. The "_max" suffix is supported, so <code>getAttrs( ["Strength", "Strength_max"], func)</code> will get both the current and max values of <code>Strength</code>. Note that the function is asynchronous, which means that there is no guarantee that the order in which multiple <code>getAttrs</code> calls are made is the order in which the results will be returned. Rather, you pass a callback function which will be executed when the values have been calculated. The callback function receives a simple JavaScript object with a list of key-value pairs, one for each attribute that you requested.
  
Here's an example:
+
'''Example:'''
  
 
<pre data-language="javascript">
 
<pre data-language="javascript">
Line 158: Line 268:
 
</pre>
 
</pre>
  
Values in repeating sections require a little special handling. If the event that you are inside of is already inside of a repeating section, you can simply request the variable using its name prefaced by the repeating group name and you will receive the value in the same repeating section the event was triggered in. For example, if we have a repeating_spells section that has both SpellName, SpellLevel, and SpellDamage, then:
+
Values in repeating sections require a little special handling. If the event that you are inside of is already inside of a repeating section, you can simply request the variable using its name prefaced by the repeating group name and you will receive the value in the same repeating section the event was triggered in. For example, if we have a <code>repeating_spells</code> section that has both <code>SpellName</code>, <code>SpellLevel</code>, and <code>SpellDamage</code>, then:
  
 
<pre data-language="javascript">
 
<pre data-language="javascript">
Line 205: Line 315:
 
</pre>
 
</pre>
  
Note: If you are trying to update a disabled input field with this method you may run into trouble. One option is to use this `setAttrs` method to set a hidden input, then set the disabled input to the hidden element. In this example we have an attr named will, and we want to calculate judgement based off 1/2 of the will stat, but not to allow it to exceed 90. See below.
+
'''Note:''' If you are trying to update a disabled input field with this method you may run into trouble. One option is to use this `setAttrs` method to set a hidden input, then set the disabled input to the hidden element. In this example we have an attr named will, and we want to calculate judgement based off 1/2 of the will stat, but not to allow it to exceed 90. See below.
  
 
<pre data-language="html">
 
<pre data-language="html">
Line 221: Line 331:
 
</pre>
 
</pre>
 
   
 
   
Note that although the setAttrs is an asynchronous function and there is no guarantee to which order the actual attributes will be set for multiple setAttrs() calls.
+
Although <code>setAttrs</code> is an asynchronous function and there is no guarantee to which order the actual attributes will be set for multiple <code>setAttrs()</code> calls.
  
 
For repeating sections, you have the option of using the simple variable name if the original event is in a repeating section, or you can specify the full repeating section variable name including ID in any event.
 
For repeating sections, you have the option of using the simple variable name if the original event is in a repeating section, or you can specify the full repeating section variable name including ID in any event.
Line 244: Line 354:
  
 
on("change:repeating_inventory", function() {
 
on("change:repeating_inventory", function() {
   getSectionIDs("repeating_inventory", function(idarray) {
+
   getSectionIDs("inventory", function(idarray) {
 
       for(var i=0; i < idarray.length; i++) {
 
       for(var i=0; i < idarray.length; i++) {
 
         //Do something with the IDs
 
         //Do something with the IDs
Line 252: Line 362:
  
 
</pre>
 
</pre>
Note that you may use GetAttrs() (described above) to request the "_reporder_repeating_<sectionname>" attribute to get a list of all the IDs in the section that have been ordered. However note that this may not include the full listing of all IDs in a section. Any IDs not in the list that are in the section are assumed to come after the ordered IDs in lexographic order. <br>
+
Note that you may use <code>GetAttrs()</code> (described above) to request the <code>_reporder_repeating_<sectionname></code> attribute to get a list of all the IDs in the section that have been ordered. However note that this may not include the full listing of all IDs in a section. Any IDs not in the list that are in the section are assumed to come after the ordered IDs in lexographic order. <br>
Which is to say that getSectionIDs() will get all IDs - but not in the order that they are displayed to the user. getAttrs( "_reporder_repeating_<sectionname>", ... ) will return a list of all IDs that have been moved out of their normal lexographic order. You can use the following function as a replacement for getSectionIDs to get the IDs in the order that they are displayed in instead.
+
Which is to say that <code>getSectionIDs()</code> will get all IDs - but not in the order that they are displayed to the user. <code>getAttrs( _reporder_repeating_<sectionname></code>, ... ) will return a list of all IDs that have been moved out of their normal lexographic order. You can use the following function as a replacement for <code>getSectionIDs</code> to get the IDs in the order that they are displayed in instead.
  
 
<pre data-language="javascript">
 
<pre data-language="javascript">
Line 270: Line 380:
 
====generateRowID()====
 
====generateRowID()====
  
A synchronous function which immediately returns a new random ID which you can use to create a new repeating section row. If you use setAttrs() and pass in the ID of a repeating section row that doesn't exist, one will be created with that ID.
+
A synchronous function which immediately returns a new random ID which you can use to create a new repeating section row. If you use <code>setAttrs()</code> and pass in the ID of a repeating section row that doesn't exist, one will be created with that ID.
  
 
Here's an example you can use to create a new row in a repeating section:
 
Here's an example you can use to create a new row in a repeating section:
Line 284: Line 394:
  
 
====removeRepeatingRow( RowID )====
 
====removeRepeatingRow( RowID )====
A synchronous function which will immediately remove all the attributes associated with a given RowID and then remove the row from the character sheet. The RowID should be of the format "repeating_<sectionname>_<rowid>". For example, "repeating_skills_-KbjuRmBRiJNeAJBJeh2".
+
A synchronous function which will immediately remove all the attributes associated with a given RowID and then remove the row from the character sheet. The RowID should be of the format <code>repeating_<sectionname>_<rowid></code>. For example, <code>repeating_skills_-KbjuRmBRiJNeAJBJeh2</code>.
  
 
Here is an example of clearing out a summary list when the original list changes:
 
Here is an example of clearing out a summary list when the original list changes:
Line 326: Line 436:
 
<pre data-language="javascript">
 
<pre data-language="javascript">
  
var translationLang = getTranslationByKey(); // "en" , for an account using English
+
var translationLang = getTranslationLanguage(); // "en" , for an account using English
  
 
</pre>
 
</pre>
Line 332: Line 442:
 
====setDefaultToken(values)====
 
====setDefaultToken(values)====
  
A function that allows the sheet author to determine what attributes are set on character dropped from the compendium. When setting the default token after a compendium drop, this function can set any attributes on the default token to tie in important attributes specific to that character sheet, such as attr_hit_points.
+
A function that allows the sheet author to determine what attributes are set on character dropped from the compendium. When setting the default token after a compendium drop, this function can set any attributes on the default token to tie in important attributes specific to that character sheet, such as <code>attr_hit_points</code>.
  
The list of token attributes that can be set by setDefaultToken are:
+
The list of token attributes that can be set by <code>setDefaultToken</code> are:
 
<pre style="white-space: pre-wrap;">
 
<pre style="white-space: pre-wrap;">
["bar1_value","bar1_max","bar2_value","bar2_max","bar3_value","bar3_max","aura1_square","aura1_radius","aura1_color","aura2_square","aura2_radius","aura2_color","tint_color","showname","showplayers_name","playersedit_name","showplayers_bar1","playersedit_bar1","showplayers_bar2","playersedit_bar2","showplayers_bar3","playersedit_bar3","showplayers_aura1","playersedit_aura1","showplayers_aura2","playersedit_aura2","light_radius","light_dimradius","light_angle","light_otherplayers","light_hassight","light_losangle","light_multiplier"]
+
["bar1_value","bar1_max","bar2_value","bar2_max","bar3_value","bar3_max","aura1_square","aura1_radius","aura1_color","aura2_square","aura2_radius","aura2_color",
 +
"tint_color","showname","showplayers_name","playersedit_name","showplayers_bar1","playersedit_bar1","showplayers_bar2","playersedit_bar2","showplayers_bar3",
 +
"playersedit_bar3","showplayers_aura1","playersedit_aura1","showplayers_aura2","playersedit_aura2","light_radius","light_dimradius","light_angle","light_otherplayers",
 +
"light_hassight","light_losangle","light_multiplier"]
 
</pre>
 
</pre>
 
For more information about this attributes and what they do, please see the the [https://wiki.roll20.net/API:Objects#Graphic_.28Token.2FMap.2FCard.2FEtc..29 API Objects Page].
 
For more information about this attributes and what they do, please see the the [https://wiki.roll20.net/API:Objects#Graphic_.28Token.2FMap.2FCard.2FEtc..29 API Objects Page].
Line 354: Line 467:
  
 
</pre>
 
</pre>
 +
<br>
 +
 +
=Related Pages=
 +
* '''[[:Category:Sheetworker|List of All Sheetworker pages]]'''
 +
* [[Sheetworker examples for Non-programmers]]
 +
* [[Sheet_Worker_Snippets|Sheet Worker Snippets]] - short and practical examples of sheetworkers
 +
* [[UniversalSheetWorkers|Universal Sheet Workers]] - How to create one function that can handle a bunch of similar sheet workers
 +
* [[CSS Wizardry]] - Contains multiple examples that makes use of sheetworkers.
 +
* [[Button#Action_Button| Action Button]] - how to use sheetworkers in combination with action buttons
 +
 +
=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/7664924/multiversal-sheet-worker-generator-this-is-bonkers/ Multiversal Sheetworker Generator] by GiGs
 +
* [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>
 
[[Category:Docs]]
 
[[Category:Docs]]
 +
[[Category:Sheetworker]]
 +
[[Category:Character Sheet Creation]]

Revision as of 03:44, 15 July 2020

Sheetworkers

Attention: Roll20 is no longer maintaining this document on the community wiki. For the most up-to-date information please visit this page on our help center for assistance: Here.

Main Article: Building Character Sheets

Sheet Worker Scripts(AKA simply as "sheetworkers"), are an advanced feature of the Character Sheets system, which allows the sheet author to specify JavaScript which will execute during certain events, such as whenever the attribute values on a character sheet are modified.

Sheetworker examples for Non-programmers, Sheet Worker Snippets and Universal Sheetworkers are the best starting points for practical and working examples on how to use sheetworkers.

This page gives a general overview of the available Roll20-specific features that can be used in sheetworkers, but is not a great guide for practical implementations. The pages found under the Related Pages and See Also-sections have links to more examples of how to use sheetworkers.

Contents


General

Introduction to JavaScript - MDN web docs

JavaScript Restrictions

Many JavaScript functions or functionalities can't be used in Roll20 character sheets due to the security filter. and one should check existing sheets for examples of what can be used, if you're attempting to do any slightly more advanced data-handling.

  • DOM can't be used directly in basically any way, so event listeners like onclick won't work.

Structure of a Sheetworker

Sheetworkers in Roll20 character sheets are pieces of JavaScript, intertwined with a few custom features made specifically for handling Roll20 character sheet information. All the sheetworkers in a character sheet are places inside a <script type="text/worker">-element.

Usually a sheetworker works as follows.

1. Listens for changes to the sheet with an Event Listener ("change:attributename sheet:opened) (explained in next section)
2. Retrieves values of a number of attributes from the sheet with the getAttrs function, usually including the attributes that were monitored.
3. Create some temporary variables and uses the values received from getAttrs and does something based on the info using normal JavaScript.
4. Save to the character sheet some of the new values made in Step 3 to some number of attributes with the setAttrs function.


The following is an example of a single sheetworker placed in a <script type="text/worker">-element, taken from the Sheetworker examples for Non-programmers-page.

on("change:siz change:con sheet:opened", function() {  
  getAttrs(["SIZ","CON"], function(values) {
    let siz = parseInt(values.SIZ)||0;
    let con = parseInt(values.CON)||0;
    let hp = siz + con;
    setAttrs({                            
      "hitpoints": hp
    });
  });
});

1. Event listener

on("change:siz change:con sheet:opened", function() is the Event listener, which checks the character sheet for changes to specific attributes, if the sheet have been opened, or if a button have been pressed. In the example, the sheetworker is checking if the siz or con-attribute have been changed, or if the sheet have been opened. These two represents the size and constitution of the character.

2. Value retrieval

getAttrs(["SIZ","CON"], function(values) looks up the values of the siz or con-attributes on the character sheet, so they can be used in the sheetworker.


3. Making changes

    let siz = parseInt(values.SIZ)||0;
    let con = parseInt(values.CON)||0;
    let hp = siz + con;

This is the main part of the sheetworker. It first defines two temporary varible, siz and con, that saves the values of their attributes in a way that JavaScript knows it's a number.

On the third row, it defines the hp-varaible to be siz + con, which means that the characters Hit Points are equal to the sum of their Size and Constitution.

4. Saving the changes

    setAttrs({                            
      "hitpoints": hp
    });

Finally, the sheetworker decides to define the character sheet's hitpoints-attribute to be equal to the newly calculated hp-variable, which is the last action of the sheetworker. Note that it does not make changes to the attributes it was listening to.

Other things

Outside of this, there may exist some other things saved in the <script type="text/worker">, such as:

  • Saved list of information, used by the sheetworkers.
    • Example: const stats = ["strength", "intelligence", "charm", "arcana", "grace"];
  • the Event Listener might loop through some JavaScript, that decides what attributes to listen/change/save
    • Example: stats.forEach(stat => { some code });
  • Define JavaScript functions to be used in the sheetworkers, instead of repeating the same code snippets in severa places.

Adding a Sheetworker

To add a sheetworker to a Character Sheet, simply add the script to the bottom of your HTML-section of the sheet using the following format:

<script type="text/worker">

on("change:strength", function() {

});

// ... etc

</script>

The <script>-element must have a type of "text/worker" to be properly handled by the system. When the game loads, all script tags are removed from the sheet's template for security purposes. However, <script>-elements with a type of "text/worker" will be used to spin up a background worker on each player's computer, that can respond to changing values in their sheets and take action as needed.

All sheetworker should be contained within a single <script type="text/worker"> element, and preferably be placed at the bottom of the html-file.

Sheetworker examples for Non-programmers

Warning about "global" variable scope: the scope of a variable declared inside the `<script type="text/worker">` but outside a function are currently per player, not per character. Changing its value will change it for all the characters of your player's session.

As a best practice Asynchronous cascades should be avoided whenever possible. It is generally a better practice to get all attributes needed for calculations in one getAttrs rather than doing getAttrs -> calculations -> setAttrs -> getAttrs -> calculations -> setAttrs…

Sheet Workers vs. Auto-Calculating Fields: Which should I use?

There's no hard-and-fast rule about this. Both of these tools are capable of achieving the same thing. Let's take a hypothetical use-case of a "Strength" attribute, which we want to use to keep a "Strength Mod" attribute updated. Here would be the differences between the two tools for this use case:

  • The auto-calculating fields are all re-calculated every time a sheet is opened for the first time. The Sheet Worker fields, on the other hand, only recalculate when their dependent values change. This means that sheets utilizing the Sheet Worker option will be much, much faster for players to open and interact with.
  • In addition, the Sheet Worker calculations run on a background process, meaning that there is no user interface lag created while the calculations are run. Disabled fields, on the other hand, run on the main process and as such can cause "lockups" or "stuttering" if there are large numbers of them being calculated at once (for example, if you have a very complicated sheet using thousands of disabled fields).
  • The auto-calculating Strength Mod field would appear disabled to the player. A Strength Mod field that is updated by a Sheet Worker, on the other hand, would be modifiable after the calculation has run (although any value entered would be overwritten when the Strength value changes). So a Sheet Worker would better support homebrew rules since the player could simply modify the Mod value after Strength changes. On the other hand, the auto-calculating field would not allow such a change, so rules would be "enforced" more rigidly.


In general, our recommendation is that you use auto-calculating fields sparingly. Give yourself a budget of 500 to 1,000 auto-calculating fields at most. We recommend using the new Sheet Worker functionality for most calculations, especially calculations which only need to be performed rarely (for example, your Strength value (and therefore your Strength Mod) probably only changes at most once per session when the character levels up. There's no need to re-run the same calculation over and over again every time the sheet is opened.

Sheet Worker API

A list of sheetworker features accessible in Roll20. Not to be confused with the Roll20 API.

Events

eventInfo Object

Many of the events are passed an eventInfo object that gives you additional detail about the circumstances of the event.

Property Description
sourceAttribute The original attribute that triggered the event. It is the full name (including RowID if in a repeating section) of the attribute that originally triggered this event.

Note: The entire string will have been translated into lowercase and thus might not be suitable for being fed directly into getAttrs().

sourceType The agent that triggered the event, either player or sheetworker
previousValue The original value of the attribute in an on:change event, before the attribute was changed.
newValue The value to which the attribute in an on:change event has changed.
removedInfo An object containing the values of all the attributes removed in a remove:repeating_groupname event.

change:<attribute_name>

Allows you to listen to the changes of specific attributes, or in the case of a repeating section any changes in the entire section. It's very straightforward:


on("change:strength change:StrengthMod change:StrengthOtherThing", function(eventInfo) {
   //Do something here
   // eventInfo.previousValue is the original value of the attribute that triggered this event, before being changed.
   // eventInfo.newValue is the current value of the attribute that triggered this event, having been changed.
});

on("change:repeating_spells:spellname", function(eventInfo) {
   //Do something here
   // eventInfo.sourceAttribute is the full name (including repeating ID) of the attribute 
   // that originally triggered this event, 
   // however the entire string will have been translated into lowercase and thus might not be 
   // suitable for being fed directly
   // into getAttrs() or other uses without being devandalized first.
});

on("change:repeating_spells", function(eventInfo) {
   //Would be triggered when any attribute in the repeating_spells section is changed
   // eventInfo.sourceAttribute is the full name (lowercased)(including repeating ID) 
   // of the attribute that originally triggered this event. 
});

Note: All attribute names are lowercased in events. So even if you normally refer to an attribute as "Strength", use "change:strength" in the event listener.

Note: This supports the "_max" suffix in that ether "change:strength" or "change:strength_max" will fire an event, however these two variations seem to be interchangeable, in that ether or both will fire when ether "strength" or "strength_max" changes.

For attributes in repeating fields, all of the following would be triggered when the "repeating_spells_SpellName" attribute is updated: "change:repeating_spells:spellname", "change:repeating_spells", "change:spellname", "change:spellname_max". This gives you maximum flexibility in choosing what "level" of change event you want to bind your function to.

Sheetworkers can also listen for a change event of a special attribute that is modified whenever a repeating section is re-ordered.

on("change:_reporder:<sectionname>", function(eventInfo) {
   // Where <sectionname> above should be a repeating section name, such as skills or spells
});

remove:repeating_<groupname>

This is an event which will fire whenever a row is deleted from a repeating field section. You can also listen for a specific row to be deleted if you know its ID, such as on("remove:repeating_inventory:-ABC123")


on("remove:repeating_inventory", function(eventInfo) {
     //Fired whenever a row is removed from repeating_inventory
     // eventInfo.sourceAttribute is the full name (including ID) of the first attribute that triggered the event (you can use this to determined the ID of the row that was deleted)
});

The removed:repeating_<groupname> function's eventinfo includes a special property removedInfo that displays all of the attributes of the now removed repeating section.

on("remove:repeating_inventory", function(eventinfo) {
   console.log(eventinfo.removedInfo);
});

sheet:opened

This event will fire every time a sheet is opened by a player in a game session. It should be useful for doing things like checking for needed sheet upgrades.

on('sheet:opened',function(){

// Do something the first time the sheet is opened by a player in a session

});

clicked:<button_name>

This event will trigger when an action button is clicked. The button's name will need to start with "act_". For example:

<button type="action" name="act_activate">Activate!</button>

on("clicked:activate", function() {
  console.log("Activate button clicked");
});

Functions

getAttrs(attributeNameArray, callback) [Asynchronous]

The getAttrs function allows you to get the values of a set of attributes from the sheet. The "_max" suffix is supported, so getAttrs( ["Strength", "Strength_max"], func) will get both the current and max values of Strength. Note that the function is asynchronous, which means that there is no guarantee that the order in which multiple getAttrs calls are made is the order in which the results will be returned. Rather, you pass a callback function which will be executed when the values have been calculated. The callback function receives a simple JavaScript object with a list of key-value pairs, one for each attribute that you requested.

Example:


on("change:strength", function() {
   getAttrs(["Strength", "Level"], function(values) {
      //Do something with values.Strength and/or values[ "Level" ]
   });
});

Values in repeating sections require a little special handling. If the event that you are inside of is already inside of a repeating section, you can simply request the variable using its name prefaced by the repeating group name and you will receive the value in the same repeating section the event was triggered in. For example, if we have a repeating_spells section that has both SpellName, SpellLevel, and SpellDamage, then:


on("change:repeating_spells:spelllevel", function() {
   getAttrs([
      "repeating_spells_SpellDamage",
      "repeating_spells_SpellName"
    ], function(values) {
      //values.repeating_spells_SpellDamage and values.repeating_spells_SpellName 
//will both be from the same repeating section row that the SpellLevel that changed is in.
   });
});

On the other hand, if you aren't currently in a repeating section, you can specifically request that value of a field in a repeating section row by specifying its ID manually:


getAttrs(["repeating_spells_-ABC123_SpellDamage"]...

You can also request the "_reporder_repeating_<sectionname>" attribute with getAttrs() to get a list of all the IDs in the section that have been ordered. However note that this may not include the full listing of all IDs in a section. Any IDs not in the list that are in the section are assumed to come after the ordered IDs in lexographic order.

setAttrs(values,options,callback) [Asynchronous]

values -- This is an object whose properties are the names of attributes (without the attr_ prefix) and whose values are what you want to set that attribute to.

options -- (Optional) This is an object that specifies optional behavior for the function. Currently the only option is "silent", which prevents propagation of change events from setting the supplied attributes.

callback -- (Optional) This is a callback function which will be executed when the set attributes have been updated.

The setAttrs function allows you to set the attributes of the character sheet. Use the "_max" suffix to set the max value of an attribute. For example "Strength_max".


on("change:strength", function() {
   getAttrs(["Strength", "Level"], function(values) {
      setAttrs({
          StrengthMod: Math.floor(values.Strength / 2)
      });
   });
});

Note: If you are trying to update a disabled input field with this method you may run into trouble. One option is to use this `setAttrs` method to set a hidden input, then set the disabled input to the hidden element. In this example we have an attr named will, and we want to calculate judgement based off 1/2 of the will stat, but not to allow it to exceed 90. See below.

<label>Judgment</label>
<input type="hidden" name="attr_foo_judgment" value="0" />
<input type="number" name="attr_judgment" value="@{foo_judgment}" disabled="true" title="1/2 of will rounded down, 90 max" />
on("change:will", function() {
  getAttrs(["will"], function(values) {
    setAttrs({ foo_judgment: Math.min(90, (values.will/2)) });
  });
});

Although setAttrs is an asynchronous function and there is no guarantee to which order the actual attributes will be set for multiple setAttrs() calls.

For repeating sections, you have the option of using the simple variable name if the original event is in a repeating section, or you can specify the full repeating section variable name including ID in any event.


on("change:repeating_spells:spelllevel", function() {
   getAttrs(["repeating_spells_SpellLevel", "repeating_spells_SpellName"], function(values) {
      setAttrs({
         repeating_spells_SpellDamage: Math.floor(values.repeating_spells_SpellLevel / 2) + 10
      });
   });
});

getSectionIDs(section_name,callback) [Asynchronous]

This function allows you to get a list of the IDs which currently exist for a given repeating section. This is useful for calculating things such as inventory where there may be a variable number of rows.


on("change:repeating_inventory", function() {
   getSectionIDs("inventory", function(idarray) {
      for(var i=0; i < idarray.length; i++) {
         //Do something with the IDs
      }
   });
});

Note that you may use GetAttrs() (described above) to request the _reporder_repeating_<sectionname> attribute to get a list of all the IDs in the section that have been ordered. However note that this may not include the full listing of all IDs in a section. Any IDs not in the list that are in the section are assumed to come after the ordered IDs in lexographic order.
Which is to say that getSectionIDs() will get all IDs - but not in the order that they are displayed to the user. getAttrs( _reporder_repeating_<sectionname>, ... ) will return a list of all IDs that have been moved out of their normal lexographic order. You can use the following function as a replacement for getSectionIDs to get the IDs in the order that they are displayed in instead.

var getSectionIDsOrdered = function (sectionName, callback) {
  'use strict';
  getAttrs([`_reporder_${sectionName}`], function (v) {
    getSectionIDs(sectionName, function (idArray) {
      let reporderArray = v[`_reporder_${sectionName}`] ? v[`_reporder_${sectionName}`].toLowerCase().split(',') : [],
        ids = [...new Set(reporderArray.filter(x => idArray.includes(x)).concat(idArray))];
      callback(ids);
    });
  });
};

generateRowID()

A synchronous function which immediately returns a new random ID which you can use to create a new repeating section row. If you use setAttrs() and pass in the ID of a repeating section row that doesn't exist, one will be created with that ID.

Here's an example you can use to create a new row in a repeating section:


var newrowid = generateRowID();
var newrowattrs = {};
newrowattrs["repeating_inventory_" + newrowid + "_weight"] = "testnewrow";
setAttrs(newrowattrs);

removeRepeatingRow( RowID )

A synchronous function which will immediately remove all the attributes associated with a given RowID and then remove the row from the character sheet. The RowID should be of the format repeating_<sectionname>_<rowid>. For example, repeating_skills_-KbjuRmBRiJNeAJBJeh2.

Here is an example of clearing out a summary list when the original list changes:

on("change:repeating_inventory", function() {
   getSectionIDs("repeating_inventorysummary", function(idarray) {
      for(var i=0; i < idarray.length; i++) {
        removeRepeatingRow("repeating_inventorysummary_" + idarray[i]);
      }
   });
});

getTranslationByKey([key])

A synchronous function which immediately returns the translation string related to the given key. If no key exists, false will be returned and a message in the console will be thrown which list the specific key that was not found in the translation JSON.

Here's an example you can use to fetch a translation string from the translation JSON: With the following translation JSON

{
    "str": "Strength",
    "dex": "Dexterity"
}

var strTranslation = getTranslationByKey('str'); // "Strength"
var dexTranslation = getTranslationByKey('dex'); // "Dexterity"
var intTranslation = getTranslationByKey('int'); // false

getTranslationLanguage()

A synchronous function which immediately returns the 2-character language code for the user's selected language.

Here's an example you can use to fetch the translation language:


var translationLang = getTranslationLanguage(); // "en" , for an account using English

setDefaultToken(values)

A function that allows the sheet author to determine what attributes are set on character dropped from the compendium. When setting the default token after a compendium drop, this function can set any attributes on the default token to tie in important attributes specific to that character sheet, such as attr_hit_points.

The list of token attributes that can be set by setDefaultToken are:

["bar1_value","bar1_max","bar2_value","bar2_max","bar3_value","bar3_max","aura1_square","aura1_radius","aura1_color","aura2_square","aura2_radius","aura2_color",
"tint_color","showname","showplayers_name","playersedit_name","showplayers_bar1","playersedit_bar1","showplayers_bar2","playersedit_bar2","showplayers_bar3",
"playersedit_bar3","showplayers_aura1","playersedit_aura1","showplayers_aura2","playersedit_aura2","light_radius","light_dimradius","light_angle","light_otherplayers",
"light_hassight","light_losangle","light_multiplier"]

For more information about this attributes and what they do, please see the the API Objects Page.

Here's an example:


on("sheet:compendium-drop", function() {
    var default_attr = {};
    default_attr["width"] = 70;
    default_attr["height"] = 70;
    default_attr["bar1_value"] = 10;
    default_attr["bar1_max"] = 15;
    setDefaultToken(default_attr);
});


Related Pages

See Also