Compendium Content Packs

Module developers can interact with compendum packs in a programmatic way using the API offered by the Compendium() class.


The Compendium Pack Class

class Compendium(metadata, options)

The Compendium class provides an interface for interacting with compendium packs which are collections of similar Entities which are stored outside of the world database but able to be easily imported into an active session.

When the game session is initialized, each available compendium pack is constructed and added to the game.packs.

Each Compendium is distinctly referenced using its canonical “collection” name which is a unique string that contains the module name which provides the compendium as well as the name of the pack within that module. For example, in the D&D5e system, the compendium pack which provides the spells available within the SRD has the collection name “dnd5e.spells”.

Arguments:
  • metadata (Object) – The compendium metadata, an object provided by game.data
  • options (Object) – Application rendering options

Examples:

// Let's learn the collection names of all the compendium packs available within a game
game.packs.map(p => p.collection);

// Suppose we are working with a particular pack named "dnd5e.spells"
const pack = game.packs.find(p => p.collection === "dnd5e.spells");

// We can load the index of the pack which contains all entity IDs, names, and image icons
pack.getIndex().then(index => console.log(index));

// We can find a specific entry in the compendium by its name
let entry = pack.index.find(e => e.name === "Acid Splash");

// Given the entity ID of "Acid Splash" we can load the full Entity from the compendium
pack.getEntity(entry.id).then(spell => console.log(spell));
// We often may want to programmatically create new Compendium content
// Let's start by creating a custom spell as an Item instance
let itemData = {name: "Custom Death Ray", type: "Spell"};
let item = new Item(itemData);

// Once we have an entity for our new Compendium entry we can import it, if the pack is unlocked
pack.importEntity(item);

// When the entity is imported into the compendium it will be assigned a new ID, so let's find it
pack.getIndex().then(index => {
  let entry = index.find(e => e.name === itemData.name));
  console.log(entry);
});

// If we decide to remove an entry from the compendium we can do that by the entry ID
pack.removeEntry(entry.id);
Compendium._render()

Override the default Application() rendering logic to wrap the render call in a promise which retrieves the compendium data.

Compendium.close()

Customize Compendium closing behavior to toggle the sidebar folder status icon

Compendium.collection

The canonical Compendium name - comprised of the originating module and the pack name

Compendium.create(metadata)

Create a new Compendium pack using provided

Arguments:
  • metadata (Object) – The compendium metadata used to create the new pack
Returns:

Promise.<Compendium>

Compendium.entity

type: String

The Entity type which is allowed to be stored in this collection

Compendium.getData()

Return the Compendium index as the data for rendering First query the server to obtain the index and then return it once prepared

Returns:Promise.Object – The data to render
Compendium.getEntity(entryId)

Get a single Compendium entry as an Entity instance

Arguments:
  • entryId (String) – The compendium entry ID to instantiate
Returns:

Promise – A Promise containing the returned Entity

Compendium.getEntry(entryId)

Get a single Compendium entry as an Object

Arguments:
  • entryId (String) – The compendium entry ID to retrieve
Returns:

Promise – A Promise containing the return entry data, or undefined

Compendium.getIndex()

Get the Compendium index Contains names and IDs of all data in the compendium

Returns:Promise – A Promise containing an index of all compendium entries
Compendium.importEntity(entity)

Import a new Entity into a Compendium pack

Arguments:
  • entity (Entity) – The Entity instance you wish to import
Compendium.metadata

type: Object

The compendium metadata which defines the compendium content and location

Compendium.public

Is this Compendium pack publicly visible to all players or private for the GM only?

Compendium.removeEntity(entryId)

Remove a single Compendium entry by its ID

Arguments:
  • entryId (String) – The compendium entry ID to remove