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:Advanced Examples"

From Roll20 Wiki

Jump to: navigation, search
 
(6 intermediate revisions by one user not shown)
Line 1: Line 1:
<div style="background:#f0e2a1; border: 3px solid #dbc870; padding: 10px;">
+
{{revdate}}[[Category:API Development]]{{HCbox| {{hc|articles/360037256834-API-Advanced-Examples Here}} }}
<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/360037256834-API-Advanced-Examples Here].''</big>
+
</div>
+
 
+
 
{{apibox}}
 
{{apibox}}
  
The Cookboox provides some examples of simple scripts that can help you get started. Feel free to take them and modify them for use in your own campaigns. There's also an [https://app.roll20.net/forum API Scripts Forum] where you can discuss API Scripting and share your scripts with the community.
+
The Cookbook provides some examples of simple scripts that can help you get started. Feel free to take them and modify them for use in your own campaigns. There's also an {{forum|category/46806 Mod Scripts}} where you can discuss Mod Scripting and share your scripts with the community.
  
'''Token Status Manager''' (Contributed by Eric Dalquist)
+
__TOC__
 +
===Token Status Manager===
 +
(Contributed by Eric Dalquist)
  
This script automatically adds status markers based on the bar values. The script can be configured to track any of the three bars, set any of the status markers for any ratio between 0 and 1. The script can manage multiple bars/tokens in a single instance. The example below watches bar 1 and sets the red marker when the value is <= 50% and then sets the dead marker when the value is <= 0.
+
This script automatically adds status markers based on the bar values. The script can be configured to track any of the three bars, set any of the status markers for any ratio between 0 and 1. The script can manage multiple bars/[[tokens]] in a single instance. The example below watches bar 1 and sets the red marker when the value is <= 50% and then sets the dead marker when the value is <= 0.
  
 
<pre data-language="javascript">
 
<pre data-language="javascript">
Line 47: Line 45:
 
});
 
});
 
</pre>
 
</pre>
For the most recent revision see: https://gist.github.com/edalquist/5459830#file-statusmanager-js
+
For the most recent revision see: {{gist|edalquist/5459830#file-statusmanager-js statusmanager-js}}
  
 
+
===Temp HP Manager===
 
+
(Contributed by Eric Dalquist)
 
+
'''Temp HP Manager''' (Contributed by Eric Dalquist)
+
  
 
Automatically manages temporary hitpoints. If one of the token bars is used to track temp hp this script can automatically deduct HP from the temp value before they are removed from the main HP bar.
 
Automatically manages temporary hitpoints. If one of the token bars is used to track temp hp this script can automatically deduct HP from the temp value before they are removed from the main HP bar.
Line 104: Line 100:
 
});
 
});
 
</pre>
 
</pre>
For the most recent revision see: https://gist.github.com/edalquist/5459830#file-temphp-js
+
For the most recent revision see: {{gist|edalquist/5459830#file-temphp-js temphp-js}}
  
 +
===Patrolling Token===
  
'''Patrolling Token'''
+
<pre data-language="javascript">on("ready", function() {
 
+
<pre data-language="javascript">
+
on("ready", function() {
+
 
   //Wait until the ready event fires so we know the campaign is completely loaded.
 
   //Wait until the ready event fires so we know the campaign is completely loaded.
 
   //Get a reference to our patrolling token.
 
   //Get a reference to our patrolling token.
Line 127: Line 121:
 
});
 
});
 
</pre>  
 
</pre>  
[[Category:API|Advanced Examples]]
+
 
 +
[[Category:API Development]]

Latest revision as of 08:40, 9 June 2024

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


The Cookbook provides some examples of simple scripts that can help you get started. Feel free to take them and modify them for use in your own campaigns. There's also an Mod Scripts(Forum) where you can discuss Mod Scripting and share your scripts with the community.

Contents

[edit] Token Status Manager

(Contributed by Eric Dalquist)

This script automatically adds status markers based on the bar values. The script can be configured to track any of the three bars, set any of the status markers for any ratio between 0 and 1. The script can manage multiple bars/tokens in a single instance. The example below watches bar 1 and sets the red marker when the value is <= 50% and then sets the dead marker when the value is <= 0.

/**
 * Set various token markers based on bar cur/max ratios
 * 
 * The CONFIG array can have any number of configuration objects. These objects
 * are processed in order.
 * 
 * barId - The ID of the bar to look at the values for [1, 2, 3]
 * barRatio - The ratio of bar value to max value that triggers setting the status marker [0 - 1]
 * status - The name of the status marker to toggle [redmarker, bluemarker, greenmarker, brownmarker, purplemarker, dead]
 * whenLow - The state of the marker when the bar value is <= the ratio [true, false]
 */
var CONFIG = [
    {barId: 1, barRatio: .5, status: "redmarker", whenLow: true},
    {barId: 1, barRatio: 0, status: "dead", whenLow: true}];


on("change:token", function(obj) {
    CONFIG.forEach(function(opts) {
        var maxValue = parseInt(obj.get("bar" + opts.barId + "_max"));
        var curValue = parseInt(obj.get("bar" + opts.barId + "_value"));
        log(opts.barId + ": " + curValue + "/" + maxValue);
    
        if (!isNaN(maxValue) && !isNaN(curValue)) {
            var markerName = "status_" + opts.status;
            if (curValue <= (maxValue * opts.barRatio)) {
                obj.set(markerName, opts.whenLow);
            }
            else {
                obj.set(markerName, !opts.whenLow);
            }
        }
    });
});

For the most recent revision see: statusmanager-js

[edit] Temp HP Manager

(Contributed by Eric Dalquist)

Automatically manages temporary hitpoints. If one of the token bars is used to track temp hp this script can automatically deduct HP from the temp value before they are removed from the main HP bar.

/**
 * Automatically removes temp HP if they exist.
 * 
 * When a token has its HP reduced the script checks to see if there are any
 * temp HP available. If it does those are removed first and the real HP is
 * updated to reflect the temp HP absorbing the hit.
 * 
 * TEMP_BAR_ID - The bar used to track temp HP [1, 2, 3]
 * HP_BAR_ID - The bar used top track real HP [1, 2, 3]
 */ 
var TEMP_BAR_ID = 2;
var HP_BAR_ID = 1;
 
on("change:token", function(obj, prev) {
    var prevHpValStr = prev["bar" + HP_BAR_ID + "_value"];
    var prevHpVal = parseInt(prevHpValStr);
    if (isNaN(prevHpVal)) {
        log("WARN: Previos bar " + HP_BAR_ID + " does not contain a number: '" + prevHpValStr + "'");
        return;
    }
    
    var hpValStr = obj.get("bar" + HP_BAR_ID + "_value");
    var hpVal = parseInt(hpValStr);
    if (isNaN(hpVal)) {
        log("WARN: Bar " + HP_BAR_ID + " does not contain a number: '" + hpValStr + "'");
        return;
    }
    
    if (prevHpVal > hpVal) {
        var tmpHpVal = parseInt(obj.get("bar" + TEMP_BAR_ID + "_value"));
        log(prevHpVal + " - " + hpVal + " - " + tmpHpVal);
        if (!isNaN(tmpHpVal)) {
            var hpChange = prevHpVal - hpVal;
            var remainingTmp = tmpHpVal - hpChange;
            if (remainingTmp > 0) {
                obj.set("bar" + TEMP_BAR_ID + "_value", remainingTmp);
                obj.set("bar" + HP_BAR_ID + "_value", prevHpVal);
            }
            else {
                var remainingHp = prevHpVal + remainingTmp;
                obj.set("bar" + TEMP_BAR_ID + "_value", 0);
                obj.set("bar" + HP_BAR_ID + "_value", remainingHp);
            }
        }
    }
});

For the most recent revision see: temphp-js

[edit] Patrolling Token

on("ready", function() {
   //Wait until the ready event fires so we know the campaign is completely loaded.
   //Get a reference to our patrolling token.
   var patroltoken = findObjs({_type: "graphic", name: "Guard A"})[0]; //We know there is a token in the Campaign called "Guard A".
   var direction = -70; //Walk left 70 pixels.
   var stepstaken = 0; //How many steps have we walked in the current direction?
   setInterval(function() {
     if(stepstaken > 3) {
       //Switch directions!
       direction = direction * -1; //will "flip" the direction we're walking
       stepstaken = 0; //reset steps back to 0.
     }
     patroltoken.set("left", patroltoken.get("left") + direction); //walk!
     stepstaken++;
   }, 5000); //take an action every 5 seconds
});