Mod:Cookbook
From Roll20 Wiki
The following are not full scripts. They are meant to be stitched together along with business logic to assist in the creation of full scripts, not create scripts on their own.
Contents |
Revealing Module Pattern
The Module Pattern emulates the concept of classes from other languages by encapsulating private and public members within an object. The Revealing Module Pattern improves upon the Module Pattern by making the syntax more consistent.
var myRevealingModule = myRevealingModule || (function() { var privateVar = 'This variable is private', publicVar = 'This variable is public'; function privateFunction() { log(privateVar); } function publicSet(text) { privateVar = text; } function publicGet() { privateFunction(); } return { setFunc: publicSet, myVar: publicVar, getFunc: publicGet }; })(); log(myRevealingModule.getFunc()); // "This variable is private" myRevealingModule.setFunc('But I can change its value'); log(myRevealingModule.getFunc()); // "But I can change its value" log(myRevealingModule.myVar); // "This variable is public" myRevealingModule.myVar = 'So I can change it all I want'; log(myRevealingModule.myVar); // "So I can change it all I want"
Memoization
Memoization is an optimization technique which stores the result for a given input, allowing the same output to be produced without computing it twice. This is especially useful in expensive computations. Of course, if it is rare that your function will receive the same input, memoization will be of limited utility while the storage requirements for it continue to grow.
var factorialCache = {}; function factorial(n) { var x; n = parseInt(n || 0); if (n < 0) { throw 'Factorials of negative numbers are not well-defined'; } if (n === 0) { return 1; } else if (factorialCache[n]) { return factorialCache[n]; } x = factorial(n - 1) * n; factorialCache[n] = x; return x; }
In a Roll20 API script, the cached values could potentially be stored in state
, which will persist between game sessions. If you have a large number of potential inputs, however, be aware that Roll20 may throttle your use of state
.
Utility Functions
Utility functions complete common tasks that you may want to use throughout many scripts. If you place a function at the outermost scope of a script tab, that function should be available to all of your scripts, reducing your overhead. Below is a selection of such functions.
getSenderForName
Dependencies: None
Given a string name, this function will return a string appropriate for the first parameter of sendChat. If there is a character that shares a name with a player, the player will be used. You may also pass an options
object, which is structured identically to the options
parameter of findObjs.
function getSenderForName(name, options) { var character = findObjs({ type: 'character', name: name }, options)[0], player = findObjs({ type: 'player', displayname: name.lastIndexOf(' (GM)') === name.length - 5 ? name.substring(0, name.length - 5) : name }, options)[0]; if (player) { return 'player|' + player.id; } if (character) { return 'character|' + character.id; } return name; } // example use on('chat:message', function(msg) { if (msg.content === '!nsp') { sendChat(getPlayerCharacterFrom(msg.who), 'None shall pass!'); } });