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 "Mod:Debugging"

From Roll20 Wiki

Jump to: navigation, search
m (Common Errors)
m
 
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{apibox}}
+
{{revdate}}{{HCbox| {{hc|articles/360037772873-API-Debugging Here}} }}
 +
{{apibox}}{{API dev}}
  
Whenever you're writing programs (from the simplest to the most advanced), inevitable you will encounter mistakes (often called "bugs") that cause the program to malfunction. Due to the Roll20 API's sandboxed nature, it can be a little difficult to tell exactly what's going on. So here are a few tips you can use to help diagnose problems with your scripts.
+
Whenever you're writing programs (from the simplest to the most advanced), inevitable you will encounter mistakes (often called "bugs") that cause the program to malfunction. Due to the Roll20 Mod's sandboxed nature, it can be a little difficult to tell exactly what's going on. So here are a few tips you can use to help diagnose problems with your scripts.
  
 
=="Caveman" Debugging==
 
=="Caveman" Debugging==
Line 7: Line 8:
 
Since you don't have direct access to the environment where the scripts are being run, you can rely on copious amounts of <code>log</code> calls to tell what's going on with your program. For example, if you're not sure why a token isn't moving correctly and you want to gain some insight into the values that are being tossed around, you might do:
 
Since you don't have direct access to the environment where the scripts are being run, you can rely on copious amounts of <code>log</code> calls to tell what's going on with your program. For example, if you're not sure why a token isn't moving correctly and you want to gain some insight into the values that are being tossed around, you might do:
  
<pre data-language="javascript">
+
<pre data-language="javascript">on("change:graphic:left", function(obj) {
on("change:graphic:left", function(obj) {
+
 
   //What's the object's left value coming into this?
 
   //What's the object's left value coming into this?
 
   log(obj.get("left"));
 
   log(obj.get("left"));
Line 16: Line 16:
 
   //You can also debug whole objects to see a list of their current attributes
 
   //You can also debug whole objects to see a list of their current attributes
 
   log(obj);
 
   log(obj);
});
+
});</pre>
</pre>
+
  
You'll find the output of your log commands in the API Console, which is on the Scripts Editor page for your Campaign.
+
You'll find the output of your log commands in the Mod/API Console, which is on the Scripts Editor page for your Campaign.
  
 
== Error Locks ==
 
== Error Locks ==
  
The Roll20 API will automatically recover from small errors in your script by restarting your script as-needed. However, if the API detects a serious error that it can't recover from, rather than just restarting your script over and over again only to have it continue to error out, it will put an "error lock" on your Campaign which causes your API Scripts not to run until the error is resolved. If your scripts have been error locked, you'll see a message like this one on the Scripts Editor page:
+
The Roll20 Mod will automatically recover from small errors in your script by restarting your script as-needed. However, if the Mod detects a serious error that it can't recover from, rather than just restarting your script over and over again only to have it continue to error out, it will put an "error lock" on your Campaign which causes your Mod Scripts not to run until the error is resolved. If your scripts have been error locked, you'll see a message like this one on the Scripts Editor page:
  
 
[[File:Error lock.png|frame|left]]
 
[[File:Error lock.png|frame|left]]
Line 29: Line 28:
 
<div style="clear: both;"></div>
 
<div style="clear: both;"></div>
  
Don't fret! Just make changes to your scripts to try and solve the problem, then click the "Save Script" button. When you do that, the error lock will be "cleared" and the API will attempt to run your scripts again. If there is another error, the error lock will be re-applied. You can repeat this process as often as needed to get the error fixed, the API won't ever keep you from clearing your error locks because you've failed too many times :-)
+
Don't fret! Just make changes to your scripts to try and solve the problem, then click the {{button|Save Script|bluelight}}-button. When you do that, the error lock will be "cleared" and the Mod will attempt to run your scripts again. If there is another error, the error lock will be re-applied. You can repeat this process as often as needed to get the error fixed, the Mod won't ever keep you from clearing your error locks because you've failed too many times :-)
  
=== Common Errors ===
+
== Common Errors ==
  
 
Some commonly encountered errors include:
 
Some commonly encountered errors include:
  
==== myvar is not defined ====
+
=== myvar is not defined ===
  
<pre data-language="javascript">
+
<pre data-language="javascript" style="overflow:auto;white-space:pre-wrap;">
 
on("ready", function() {
 
on("ready", function() {
 
     var myVar;
 
     var myVar;
Line 44: Line 43:
 
</pre>
 
</pre>
  
While the error message says "not defined," what's actually happened is that the variable is not ''declared''. One of the most common causes of this is a typo in the name of one of your variables, such as missing a capital letter.
+
While the error message says {{c|not defined}}, what's actually happened is that the variable is not ''declared''. One of the most common causes of this is a typo in the name of one of your variables, such as missing a capital letter.
  
==== Cannot read property 'myProperty' or Cannot call method 'myMethod' ====
+
=== Cannot read property 'myProperty' or Cannot call method 'myMethod' ===
  
<pre data-language="javascript">
+
<pre data-language="javascript" style="overflow:auto;white-space:pre-wrap;">
 
on("ready", function() {
 
on("ready", function() {
 
     var myVar;
 
     var myVar;
Line 61: Line 60:
 
* Your variable is being conditionally defined (with a series of <code>if</code> statements or something similar), and none of your conditions matched, so your variable sat there declared, but never defined. Make sure you have conditions for all possibilities, or make a default option, or perform error-checking to make sure your variable is defined before accessing its properties.
 
* Your variable is being conditionally defined (with a series of <code>if</code> statements or something similar), and none of your conditions matched, so your variable sat there declared, but never defined. Make sure you have conditions for all possibilities, or make a default option, or perform error-checking to make sure your variable is defined before accessing its properties.
  
==== Unexpected token ====
+
=== Unexpected token ===
  
 
You are either missing a character or have one too many characters. This can result from forgetting a comma between a list of properties in an object or elements in an array, or having one too many or too few closing parentheses at the end of a complicated nested method call.
 
You are either missing a character or have one too many characters. This can result from forgetting a comma between a list of properties in an object or elements in an array, or having one too many or too few closing parentheses at the end of a complicated nested method call.
  
==== _displayname returns undefined, while get("_displayname") returns a name ====
+
=== _displayname returns undefined, while get("_displayname") returns a name ===
  
Most of the properties in the Roll20 objects need to be accessed via the <code>get</code> and <code>set</code> methods. When using <code>get</code>, you may omit the leading underscore on read-only properties.
+
Most of the properties in the Roll20 objects need to be accessed via the <code>get()</code> and <code>set()</code> methods. When using <code>get()</code>, you should omit the leading underscore on read-only properties.
  
[[Category:API|Debugging]]
+
[[Category:API]]
 +
[[Category:API Development]]

Latest revision as of 07:07, 9 June 2024

Attention: This page is community-maintained. For the official Roll20 version of this article, see the Help Center for assistance: Here .

Whenever you're writing programs (from the simplest to the most advanced), inevitable you will encounter mistakes (often called "bugs") that cause the program to malfunction. Due to the Roll20 Mod's sandboxed nature, it can be a little difficult to tell exactly what's going on. So here are a few tips you can use to help diagnose problems with your scripts.

Contents

[edit] "Caveman" Debugging

Since you don't have direct access to the environment where the scripts are being run, you can rely on copious amounts of log calls to tell what's going on with your program. For example, if you're not sure why a token isn't moving correctly and you want to gain some insight into the values that are being tossed around, you might do:

on("change:graphic:left", function(obj) {
   //What's the object's left value coming into this?
   log(obj.get("left"));
   obj.set("left", obj.get("left") + 70);
   //What's it now?
   log(obj.get("left"));
   //You can also debug whole objects to see a list of their current attributes
   log(obj);
});

You'll find the output of your log commands in the Mod/API Console, which is on the Scripts Editor page for your Campaign.

[edit] Error Locks

The Roll20 Mod will automatically recover from small errors in your script by restarting your script as-needed. However, if the Mod detects a serious error that it can't recover from, rather than just restarting your script over and over again only to have it continue to error out, it will put an "error lock" on your Campaign which causes your Mod Scripts not to run until the error is resolved. If your scripts have been error locked, you'll see a message like this one on the Scripts Editor page:

Error lock.png

Don't fret! Just make changes to your scripts to try and solve the problem, then click the Save Script-button. When you do that, the error lock will be "cleared" and the Mod will attempt to run your scripts again. If there is another error, the error lock will be re-applied. You can repeat this process as often as needed to get the error fixed, the Mod won't ever keep you from clearing your error locks because you've failed too many times :-)

[edit] Common Errors

Some commonly encountered errors include:

[edit] myvar is not defined

on("ready", function() {
    var myVar;
    log(myvar);
});

While the error message says not defined, what's actually happened is that the variable is not declared. One of the most common causes of this is a typo in the name of one of your variables, such as missing a capital letter.

[edit] Cannot read property 'myProperty' or Cannot call method 'myMethod'

on("ready", function() {
    var myVar;
    log(myVar.myProperty);
    log(myVar.myMethod());
});

myVar is not defined, so the script cannot figure out how to deal with you trying to access a property of myVar. This is likely due to one of:

  • You tried to find an object, but the result was undefined. Perform some error-checking to make sure your variable is defined before accessing its properties.
  • Your variable is being conditionally defined (with a series of if statements or something similar), and none of your conditions matched, so your variable sat there declared, but never defined. Make sure you have conditions for all possibilities, or make a default option, or perform error-checking to make sure your variable is defined before accessing its properties.

[edit] Unexpected token

You are either missing a character or have one too many characters. This can result from forgetting a comma between a list of properties in an object or elements in an array, or having one too many or too few closing parentheses at the end of a complicated nested method call.

[edit] _displayname returns undefined, while get("_displayname") returns a name

Most of the properties in the Roll20 objects need to be accessed via the get() and set() methods. When using get(), you should omit the leading underscore on read-only properties.