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

Script:Flight

From Roll20 Wiki

Revision as of 10:47, 2 October 2013 by Brian (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The following two scripts both create the API command !fly. The first script is functionally limited to the values 1-9, while the second script is not. However, the second script makes use of an API feature which may or may not be intentional, meaning that it might be broken by an update to Roll20 in the future.

Contents

Syntax

!fly [height]
Parameter Values
height Optional. Set the height of the selected token(s). If this parameter is 0, negative, or omitted, the flight value will be removed from the token(s).

Code: Version 1

on('chat:message', function(msg) {
    if(msg.type != 'api') return;
    var parts = msg.content.toLowerCase().split(' ');
    var command = parts.shift().substring(1);
    var selected = msg.selected;
    if(command != 'fly' || !selected) return; // use the !fly command, and have one or more things selected
    var height = +parts[0];
    if(!height) height = 0; // if no height is returned, treat as 0
    _.each(selected, function(obj) {
        if(obj._type != 'graphic') return; // only fly graphics
        var tok = getObj('graphic', obj._id);
        if(tok.get('subtype') != 'token') return; // don't try to fly cards
        tok.set('status_fluffy-wing', false);
        if(height > 0) tok.set('status_fluffy-wing', ''+height);
    });
});

Code: Version 2

on('chat:message', function(msg) {
    if(msg.type != 'api') return;
    var parts = msg.content.toLowerCase().split(' ');
    var command = parts.shift().substring(1);
    var selected = msg.selected;
    if(command != 'fly' || !selected) return; // use the !fly command, and have one or more things selected
    var height = +parts[0];
    if(!height) height = 0; // if no height is returned, treat as 0
    _.each(selected, function(obj) {
        if(obj._type != 'graphic') return; // only fly graphics
        var tok = getObj('graphic', obj._id);
        if(tok.get('subtype') != 'token') return; // don't try to fly cards
        tok.set('status_fluffy-wing', false);
        var wings = '';
        while(height > 0)
        {
            // get current digit, starting from ones
            var digit = height / 10;
            digit -= Math.floor(digit);
            digit = Math.round(digit * 10);
            // shift height
            height = Math.floor(height / 10);
            wings += 'fluffy-wing@'+digit+',';
        }
        if(wings.length > 0) wings = wings.substring(0, wings.length - 1); // trim trailing comma
        var markers = tok.get('statusmarkers');
        if(markers != '') markers += ',';
        markers += wings;
        tok.set('statusmarkers', markers);
    });
});

Notes

In the second version of the script, numbers with multiple digits will create multiple wing statusmarkers, one for each digit. Zero digits (for example, the tens digit of "205") will show up as a wing statusmarker with no number.

Flight Example.jpg