Shattered Codex

Wiki

← Back to the SC - More Activities guide

Developer reference

SC - More Activities API Reference

Register custom D&D 5e activities through SC - More Activities, use its public API and lifecycle hooks, and follow a complete integration example.

For installation, settings, and everyday use, start with the module guide.

For installation, settings, and activity workflows, see the SC - More Activities guide.

Developer Registration API

If you are building another module, register activity types through the SC registry instead of mutating CONFIG.DND5E.activityTypes directly.

Public API access:

  • game.modules.get("sc-more-activities")?.api
  • globalThis.ShatteredCodex.activities for the published activities API

Main lifecycle hooks:

  • sc-more-activities.registerActivities
  • sc-more-activities.registryLocked
  • sc-more-activities.apiReady
  • sc-more-activities.activityRegistered
  • sc-more-activities.activityAvailabilityChanged
  • sc-more-activities.activityUseStart
  • sc-more-activities.activityUseComplete
  • sc-more-activities.activityUseError

Registration timing:

  • register during Hooks.on("sc-more-activities.registerActivities", ...)
  • late registrations are rejected after the registry locks during initialization
  • if you need the final published API object, listen for sc-more-activities.apiReady

Minimum registration contract:

  • moduleId
  • type
  • label
  • hint
  • icon
  • documentClass

Your documentClass must provide:

  • static metadata.type that matches the registered type
  • static availableForItem(...)
  • static localize(...)

Minimal example:

js
Hooks.on("sc-more-activities.registerActivities", (activities) => {
  activities.registerType({
    moduleId: "my-module",
    type: "my-module-ignite",
    label: "MYMODULE.Activity.Ignite.Label",
    hint: "MYMODULE.Activity.Ignite.Hint",
    icon: "modules/my-module/icons/ignite.svg",
    documentClass: MyIgniteActivity,
  });
});

The public activities API also exposes helper methods such as:

  • listTypes()
  • listTypeAvailability()
  • setTypeEnabled(type, enabled)
  • getRegistrationReport()
  • createActivityOnItem(item, type, data, options)

Developer Example: SC Simple Sockets

If you want a concrete external-module example, SC - Simple Sockets is the best reference currently in the Shattered Codex module set.

It does not patch sc-more-activities directly. Instead, it keeps the entire integration inside its own module and registers two external activity types when both modules are active:

  • sc-socket-slot
  • sc-socket-extraction

This example focuses on sc-socket-slot, because it shows the full lifecycle for a custom activity type.

What Files The Example Uses

FileRole in the integration
`scripts/main.js`Imports the integration and calls `ScMoreActivitiesIntegration.register()` during module startup.
`scripts/core/integrations/sc-more-activities/ScMoreActivitiesConstants.js`Defines hook names, type ids, group metadata, query id, timeout, and icon paths.
`scripts/core/integrations/sc-more-activities/ScMoreActivitiesIntegration.js`Listens to the registration hook, registers types, exposes GM-mediated query handlers, and dispatches socket mutations.
`scripts/core/integrations/sc-more-activities/activities/socket-slot/ScMoreActivitiesSocketSlotActivity.js`Defines the activity document class and its `use()` behavior.
`scripts/core/integrations/sc-more-activities/activities/socket-slot/ScMoreActivitiesSocketSlotActivityData.js`Defines the activity schema stored on the item.
`scripts/core/integrations/sc-more-activities/activities/socket-slot/ScMoreActivitiesSocketSlotActivitySheet.js`Extends the activity sheet and prepares UI context for the custom effect tab.
`scripts/core/integrations/sc-more-activities/activities/socket-slot/ScMoreActivitiesSocketSlotActivityService.js`Runs the real workflow after `super.use()` succeeds.
`templates/integrations/sc-more-activities/socket-slot-effect.hbs`Renders the custom form fields shown on the activity sheet.

How The Bootstrap Works

SC - Simple Sockets loads the integration from its own scripts/main.js and registers it at startup:

js
import { ScMoreActivitiesIntegration } from "./core/integrations/sc-more-activities/ScMoreActivitiesIntegration.js";
 
ScMoreActivitiesIntegration.register();

That register() method does two jobs:

  1. It listens to sc-more-activities.registerActivities.
  2. It registers a query handler during init so socket mutations can be routed through an active GM when needed.

This is important because the integration owns both the activity definition and the mutation flow. The external module is responsible for its own runtime behavior.

The Real Registration Call

Inside ScMoreActivitiesIntegration.js, SC - Simple Sockets registers sc-socket-slot like this:

js
return activitiesApi.registerType({
  moduleId: Constants.MODULE_ID,
  type: SC_MORE_ACTIVITIES_ACTIVITY_TYPES.SOCKET_SLOT,
  label: "SCSockets.Integrations.ScMoreActivities.SocketSlot.Title",
  hint: "SCSockets.Integrations.ScMoreActivities.SocketSlot.Hint",
  icon: SC_MORE_ACTIVITIES_ICONS.SOCKET_SLOT,
  documentClass: ScMoreActivitiesSocketSlotActivity,
  dataModel: ScMoreActivitiesSocketSlotActivityData,
  sheetClass: ScMoreActivitiesSocketSlotActivitySheet,
  configurable: true,
  category: "sockets",
  ui: {
    scope: "external",
    group: "sockets",
    groupId: SC_MORE_ACTIVITIES_GROUP.id,
    groupLabel: SC_MORE_ACTIVITIES_GROUP.label,
    groupIcon: SC_MORE_ACTIVITIES_GROUP.icon,
    groupOrder: SC_MORE_ACTIVITIES_GROUP.order,
    order: 140
  },
  tags: ["sockets", "slot", "inventory"],
  compatibility: {
    dnd5e: "5.x",
    scMoreActivities: { moduleId: "sc-more-activities", required: true },
    scSimpleSockets: { moduleId: "sc-simple-sockets", required: true }
  },
  templates: [
    "modules/sc-simple-sockets/templates/integrations/sc-more-activities/socket-slot-effect.hbs"
  ],
  ownership: {
    execute: "item-owner",
    hostItem: "activity-item",
    mutation: "gm-mediated"
  },
  source: Constants.MODULE_ID
});

Why this registration is useful:

  • ui.scope: "external" places the type in the external registration flow
  • groupId, groupLabel, and groupOrder create a module-specific activity group in the create dialog
  • dataModel, sheetClass, and templates turn the type into a configurable authoring experience instead of a bare runtime document
  • ownership.mutation: "gm-mediated" matches the real workflow, because socket mutations may need GM execution

The Activity Class

The activity class extends the standard dnd5e activity mixin and connects the registered type id to the custom schema, metadata, availability check, and runtime execution:

js
export class ScMoreActivitiesSocketSlotActivity extends dnd5e.documents.activity.ActivityMixin(
  ScMoreActivitiesSocketSlotActivityData
) {
  static metadata = Object.freeze(
    foundry.utils.mergeObject(super.metadata, {
      type: SC_MORE_ACTIVITIES_ACTIVITY_TYPES.SOCKET_SLOT,
      img: SC_MORE_ACTIVITIES_ICONS.SOCKET_SLOT,
      title: "SCSockets.Integrations.ScMoreActivities.SocketSlot.Title",
      hint: "SCSockets.Integrations.ScMoreActivities.SocketSlot.Hint",
      sheetClass: ScMoreActivitiesSocketSlotActivitySheet
    }, { inplace: false })
  );
 
  static availableForItem(item, ...args) {
    const base = typeof super.availableForItem === "function"
      ? super.availableForItem(item, ...args)
      : true;
    return base && ScMoreActivitiesIntegration.isTypeEnabled(
      SC_MORE_ACTIVITIES_ACTIVITY_TYPES.SOCKET_SLOT
    );
  }
 
  async use(usage = {}, dialog = {}, message = {}) {
    const results = await super.use(usage, dialog, message);
    if (results === undefined) return results;
    return ScMoreActivitiesSocketSlotActivityService.execute(this, { usage, dialog, message, results });
  }
}

The pattern to copy into your own module is:

  1. Extend dnd5e.documents.activity.ActivityMixin(...).
  2. Put the registered type id into static metadata.type.
  3. Route availability through availableForItem(...).
  4. Call super.use(...) first.
  5. Hand off the actual side effects to a dedicated service class.

The Data Model

ScMoreActivitiesSocketSlotActivityData extends dnd5e.dataModels.activity.BaseActivityData and stores all custom fields under slot.

Real schema fields in the example include:

  • slot.operation
  • slot.name
  • slot.color
  • slot.cursorImage
  • slot.description
  • slot.condition
  • slot.targetCondition
  • slot.hidden
  • slot.deleteGemOnRemoval
  • slot.ignoreMaxSockets

That structure is worth copying because it keeps custom activity state grouped under one schema branch instead of scattering unrelated top-level fields.

The Sheet and Template

ScMoreActivitiesSocketSlotActivitySheet extends dnd5e.applications.activity.ActivitySheet and injects a custom effect-part template:

js
static PARTS = {
  ...super.PARTS,
  effect: {
    template: TEMPLATE_PATH,
    templates: [...super.PARTS.effect.templates]
  }
};

That template is templates/integrations/sc-more-activities/socket-slot-effect.hbs, and it renders the actual authoring controls for:

  • slot operation
  • slot name and color
  • cursor image
  • description
  • condition code
  • target condition code
  • behavior toggles such as hidden, delete on removal, and ignore socket limit

Use this pattern when your custom activity needs real item-authoring UI instead of a small number of primitive fields.

The Runtime Service

The actual socket behavior does not live in the activity class. It lives in ScMoreActivitiesSocketSlotActivityService.

That service decides whether the configured operation is:

  • add a slot to a selected item
  • remove one empty slot from the host item

The service also validates the target condition, launches the slot-picker UI when needed, and hands the mutation back to ScMoreActivitiesIntegration.

That separation matters:

  • the activity class stays small and predictable
  • the data model only describes stored state
  • the service owns workflow branching, validation, dialogs, and user feedback

How GM-Mediated Execution Works

SC - Simple Sockets is a strong example because it shows how to handle mutations that may require GM authority.

Inside ScMoreActivitiesIntegration.js, the integration:

  1. Builds a request payload with activityUuid, itemUuid, operation, and requestUserId.
  2. Executes directly when the current user is a GM or already has socket-edit permission.
  3. Falls back to an active GM query using a registered query id when direct execution is not allowed.
  4. Resolves the request on the GM side and turns the result into a normalized success or failure object.

This is the concrete pattern to follow when your custom activity mutates actors, items, scenes, or other documents that should not be changed blindly by every player client.

Minimal Blueprint For Your Own Module

If you are building a new external activity, the smallest practical file set is:

scripts/main.js
scripts/integrations/sc-more-activities/MyIntegration.js
scripts/integrations/sc-more-activities/MyActivity.js
scripts/integrations/sc-more-activities/MyActivityData.js
scripts/integrations/sc-more-activities/MyActivitySheet.js
scripts/integrations/sc-more-activities/MyActivityService.js
templates/integrations/sc-more-activities/my-activity-effect.hbs

Build it in this order:

  1. Register an integration class from scripts/main.js.
  2. Listen to sc-more-activities.registerActivities.
  3. Call activitiesApi.registerType(...).
  4. Create a document class with metadata, availableForItem(...), and use(...).
  5. Add a data model for your stored fields.
  6. Add a sheet class and Handlebars template for configuration.
  7. Move the actual side effects into a service class.
  8. Add GM-mediated dispatch only if your workflow really needs it.

When To Copy This Pattern

Use the sc-simple-sockets structure when your activity:

  • needs custom authoring fields
  • performs complex runtime logic after super.use(...)
  • may require GM-mediated document changes
  • belongs to a module-specific activity group in the create dialog
  • should stay fully owned by the integrating module instead of by sc-more-activities