Dxcufgb's Workshop

API

For macro and module authors.

const api = game.modules.get("dxcufgbs-token-follower").api;
await api.follow(followerDoc, leaderDoc); // followerDoc starts following leaderDoc and moves next to it
await api.stop(followerDoc);              // followerDoc stops following (nothing happens if it isn't following)
api.getFollowers(leaderDoc);              // array of the TokenDocuments on leaderDoc's scene that follow it
  • All three take TokenDocuments (token.document), not canvas tokens.
  • follow uses the same rules as the follow key: a player can only follow a friendly token they can currently see on their canvas; a GM can follow any token. A token can't follow its own follower. If a rule blocks it, the user gets a notification and nothing changes. Both tokens must be on the same scene.
  • follow and stop only change a follower token the calling user owns (a GM owns every token). Otherwise the user gets a notification and nothing is written.
  • follow and stop show the same "is now following" / "stopped following" notifications as the key. After follow, the module keeps moving the follower by itself, as described in Following a Token.
  • The module calls no hooks of its own. A follow starting or stopping is an updateToken change to the flag below.

From another module

The API is set in the ready hook, so use it after ready (for example when your code reacts to the user), not while your module loads. Make the integration optional:

// Make the selected token follow your target.
const mod = game.modules.get("dxcufgbs-token-follower");
const api = mod?.active ? mod.api : null;
const follower = canvas.tokens.controlled[0]?.document;
const leader = game.user.targets.first()?.document;
if (api && follower && leader) await api.follow(follower, leader);

Add { "id": "dxcufgbs-token-follower", "type": "module" } to relationships.recommends in your module.json.

Data

  • flags["dxcufgbs-token-follower"].leader on the follower's TokenDocument, while it follows: { sceneId, tokenId, actorId, name, userId, dir: { x, y } }. That is the leader's scene and token id, its actor id and name (used to find the leader again after a scene change), the user who started the follow, and the leader's last movement direction (x and y are -1, 0 or 1). Read it with doc.getFlag("dxcufgbs-token-follower", "leader"); start and stop through the API, not by writing the flag.
  • When a follower is moved to another scene, its token is created on the new scene with the same id and the old token is deleted.

This page mirrors the Token Follower wiki on GitHub.