Skip to content

Instantly share code, notes, and snippets.

@goblinHordes
Last active December 25, 2015 01:19
Show Gist options
  • Save goblinHordes/6894527 to your computer and use it in GitHub Desktop.
Save goblinHordes/6894527 to your computer and use it in GitHub Desktop.
CommandTokens for Roll20 API Associate tokens to API functions and trigger functions when the token is moved.
/*
CommandTokens
Associate tokens to API functions and trigger functions when the token is moved.
CommandTokens provide an easy way to trigger API functions without typing or complicated layer manipulation by the GM.
CommandTokens are powered through an ugly-but-effective naming convention:
__[function]_(:[variable_1]:[variable_2]...)__
where [function] is the function to call and the [variable_x] elements are replaced by a colon delimited list
of parameters to pass to the function. Parameters are optional (as long as the underlying function is ok with that)
and do not require a trailing colon.
Functions are indirect - they require registration in the script, tying a function name to a real or anonymous API
function. A very simple example is included in the code:
ctRegister("ctTogglePositionLock", ctTogglePositionLock)
is a function that will toggle the position lock for CommandTokens by calling the real function ctTogglePositionLock.
Moving or rotating a CommandToken causes it to trigger its associated function. Since there is an abstraction between
the actual function called and the CommandToken virtual function, all parameters are passed as a single array and
require parsing by the receiving function.
As long as CT_POSITION_LOCK == true, all CommandTokens are locked in their current position and rotation. The
ctTogglePositionLock function can be used to disable the position lock for moving tokens. You can also deactivate
a specific token by prepending its name with a semicolon: ;__ctTogglePositionLock__
*/
var ctFunctTable = {};
var CT_POSITION_LOCK = true;
function ctTogglePositionLock(lock){
if(lock[0])
CT_POSITION_LOCK=lock[0];
else
CT_POSITION_LOCK = !CT_POSITION_LOCK;
sendChat('API', 'CT Position Lock: ' + CT_POSITION_LOCK);
}
function ctParseName(name){
if(match = name.match(/^__(.*?)_(?::(.*)_)?_$/)){
funct = ctFunctTable[match[1]];
params = match[2];
if(funct==undefined) return false;
if(params){params=params.split(':')}else{params=[]};
return {funct:funct, params:params};
} else return false;
}
function ctRegister(name, funct) {
ctFunctTable[name] = funct;
}
on("ready", function() {
on("change:graphic", function(obj, prev) {
if(obj.get('left')==prev['left'] && obj.get('top')==prev['top'] && obj.get('rotation')==prev['rotation']) return;
if (ct = ctParseName(obj.get('name')) ) {
ct.funct(ct.params);
if(CT_POSITION_LOCK==true) {
obj.set('left', prev['left']);
obj.set('top', prev['top']);
obj.set('rotation', prev['rotation']);
};
} else return;
});
ctRegister("ctTogglePositionLock", ctTogglePositionLock)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment