Repository navigation
Using CTLib
CTLib is a Foundry library module, so using it takes two steps: say your module needs it, then reach it from your code.
Add CTLib to relationships.requires in your module.json. Foundry then offers to install it with your module, and won't let a world enable yours without it.
"relationships": {
"requires": [
{
"id": "draw-steel-ctlib",
"type": "module",
"manifest": "https://github.com/TheGenieOfTheCode/draw-steel-ctlib/releases/latest/download/module.json",
"compatibility": { "minimum": "1.5.0" }
}
]
}Set minimum to the oldest CTLib version with everything your module uses.
CTLib brings socketlib with it, so you don't need to list socketlib yourself unless your own code uses it.
Foundry won't run a module whose required module is missing or older than its minimum, and doesn't say which one. For every module that requires CTLib, CTLib checks this when the world loads and, if something is wrong, whispers the Director a chat message and shows a warning naming the module, what it needs, and how to fix it. It tells each Director once per module version and problem.
There are three ways in. All of them reach the same single copy of the library, so state such as a registered service or a picker in progress is shared whichever way a module gets there.
CTLib is a library module, so Foundry loads it before the system and every ordinary module, and it sets a ctlib global as it loads. By the time your module's files run, it is already there:
const { safeUpdate, beginPickerOverlay, config, services } = ctlib;A tidy habit is one small file in your module that takes what you need from CTLib, so it's written once:
// src/ctlib.mjs
export const { safeUpdate, beginPickerOverlay, config, services } = ctlib;Then everywhere else: import { safeUpdate } from './ctlib.mjs';
If your module only uses CTLib when it happens to be there, check for it first: globalThis.ctlib?.safeUpdate.
The same exports are also on game.modules.get('draw-steel-ctlib').api:
const lib = game.modules.get('draw-steel-ctlib').api;
await lib.safeUpdate(token.document, { hidden: true });CTLib sets this during its own init hook. Modules run their init hooks one after another, so yours may run first and find api still empty. Read it in setup, ready or later, or use the global.
Before 1.2.0 there was no global, so modules imported src/index.mjs directly. That file still works, but avoid it in new code: a relative path such as ../../draw-steel-ctlib/src/index.mjs breaks on hosts that serve each module from its own versioned address, such as The Forge.
CTLib loads before the Draw Steel system and touches nothing of it while loading. Anything that needs the system is built the first time you ask for it: the Settings Window base class comes from ctlib.settingsSubmenu(), which you call from your own module's code, after the system has loaded.
Registration calls, such as config.define, services.provide, registerPanelDecorator and registerStatusGroup, only store what you give them. They're safe at the top level of a file or in any hook. Calls that touch the canvas, the grid or documents need a ready canvas, the same as any Foundry code.
Names on these pages may still change between minor versions while the family of modules settles; changes are listed in each release's notes. Anything not listed on these pages is internal and may change without notice.
Getting Started
Working With Documents
The Canvas
Interface