Table of Contents

Extending with Perks

Overview

The core component of BitsmistJS “Unit” has few functions. Each unit can be extended in functionality by applying perks.

For example, in "Create a Sample Unit", an event handler is set in the configuration, which is handled by EventPerk. Loading and applying HTML files is done by SkinPerk.

Perk has the following functions.

By default, the following perks are applied to a unit.

Applying Perks

To apply a perk, it must be written in the settings of each unit. There are two ways to do so.

Write in the "perk" Section

Specify the name of the perks in apply option of Perk. Multiple perks can be specified.

_getSettings()
{
    return {
        "perk": {
            "options": {
                "apply": ["EventPerk"]
            }
        }
    };
}

Add Perk's Dedicated Section

Each perk has a dedicated section for the perk, and the presence of that section in the settings will automatically apply the perk.

For example, the following settings has an “event” section, so the EventPerk will be applied automatically.

_getSettings()
{
    return {
        "event": {
            "events": {
                "this": {
                    "handlers": {
                        "doSetup": this.onDoSetup
                    }
                },
            }
        }
    };
}

To see which perks are associated with which section, see the “section” tag under the title of each perk reference.

Event Handlers

Some perks register event handlers to the units and perform their own processing within those handlers. The event handlers registered by the perks have a higher priority and are designed to be processed before user event handlers.

Which events are handled and how they are handled varies from perk to perk. For example, SkinPerk loads the necessary HTML file in “beforeTransform” and applies it to the unit in “doTransform” event.

Below is a list of what the perk in the core library does at which events. For more information, please refer to each link.

EventPerkDescription
doApplySettingsEventPerkInstall event handlers.
StatuePerkInstall event handlers to wait for other units.
UnitPerkAdd child units.
beforeTransformSkinPerkLoad HTML file.
StylePerkLoad and Apply common CSS files.
doTransformSkinPerkApply HTML file.
StylePerkLoad and Apply unit-specifi CSS files.
afterTransformEventPerkInstall event handlers for child elements.

Extended Features

Each perk also has the ability to add assets, inventory, skills, and spells to the unit prototype and/or each instance.

Below is a list of the functions added by the perks in the core library. Please see the description of each perk for more details.