Entity

Entity

An abstract class pattern for all primary data entities within the Foundry VTT Framework. An entity represents a primary data concept, for example: Actor, Item, Scene, or ChatMessage. Each Entity type in Foundry Virtual Tabletop extends this base Entity class which ensures similar behavior and workflow across all entity types.

Documentation for this class is provided for reference, but developers should not extend this class directly, instead work with or extend the Entity implementations that are referenced in this section of the API documentation.

Entities are instantiated by providing their base data, and an optional Array of Application instances which should be automatically refreshed when the Entity experiences an update.

Constructor

(abstract) new Entity(data, options)

Source:
See:
  • Collection The Collection abstract class which contains Entity instances.
  • Actor The Actor Entity.
  • Combat The Combat Encounter Entity.
  • Folder The Folder Entity.
  • Item The Item Entity.
  • JournalEntry The Journal Entry Entity.
  • ChatMessage The Chat Message Entity.
  • Playlist The Audio Playlist Entity.
  • Scene The Scene Entity.
  • RollTable The Rollable Table Entity.
  • User The User Entity.
  • Compendium The Compendium which may contain Entities in a compendium pack.
Example
let actorData = {name: "John Doe", type: "character", img: "icons/mystery-man.png"};
let actor = new Actor(actorData);
Parameters:
Name Type Description
data Object

The data Object with which to create the Entity

options Object

Additional options which modify the created Entity behavior

Properties
Name Type Attributes Description
compendium Compendium <optional>

A reference to the Compendium pack from which this Entity was drawn.

Members

(static) collection :Collection

Source:

Return a reference to the Collection instance which stores Entity instances of this type. This property is available as both a static and instance method and should be overridden by subclass Entity implementations.

Type:

(static) config :Object

Source:
Properties:
Name Type Description
baseEntity Entity

The parent class which directly inherits from the Entity interface.

collection Collection

The Collection instance to which Entities of this type belong.

embeddedEntities Array

The names of any Embedded Entities within the Entity data structure.

Configure the attributes of this Entity class

Type:
  • Object

(static) entity :string

Source:

The class name of the base Entity type, for example "Actor". This is useful in cases where there is an inheritance chain. Many places throughout the framework rely upon the canonical entity name which may not always be equal to the class name. This property is available as both a static and instance method.

Type:
  • string
Example
class Actor2ndGen extends Actor {...}
Actor2ndGen.entity // "Actor"

apps :Object.<Application>

Source:
See:

A collection of Application instances which should be re-rendered whenever this Entity experiences an update to its data. The keys of this object are the application ids and the values are Application instances. Each Application in this object will have its render method called by Entity#render.

Type:

compendium :Compendium|null

Source:

The Entity may optionally belong to a parent Compendium pack. If so this attribute will contain a reference to that Compendium object. Otherwise null.

Type:

data :Object

Source:

The original source data for the object provided at initialization.

Type:
  • Object

folder :Folder|null

Source:

Return a reference to the Folder which this Entity belongs to, if any.

Type:
Example

Entities may belong to Folders

let folder = game.folders.entities[0];
let actor = await Actor.create({name: "New Actor", folder: folder.id});
console.log(actor.data.folder); // folder.id;
console.log(actor.folder); // folder;

id :string

Source:

A convenience accessor for the _id attribute of the Entity data object.

Type:
  • string

limited :boolean

Source:

A boolean indicator for whether the current game user has ONLY limited visibility for this Entity. Note that a GM user's perspective of an Entity is never limited.

Type:
  • boolean

name :string

Source:

A convenience accessor for the name attribute of the Entity data object

Type:
  • string

options :Object

Source:

The options object that was used to configure the Entity upon initialization.

Type:
  • Object

owner :boolean

Source:

A boolean indicator for whether or not the current game User has ownership rights for this Entity. This property has a setter which allows for ownership rights to be temporarily overridden on a per-instance basis.

Type:
  • boolean

permission :Number

Source:

Return the permission level that the current game User has over this Entity. See the CONST.ENTITY_PERMISSIONS object for an enumeration of these levels.

Type:
Example
game.user.id; // "dkasjkkj23kjf"
entity.data.permission; // {default: 1, "dkasjkkj23kjf": 2};
entity.permission; // 2

sheet :BaseEntitySheet

Source:

A property which gets or creates a singleton instance of the sheet class used to render and edit data for this particular entity type.

Type:
Example

A subclass of the Actor entity

let actor = game.entities.actors[0];
actor.sheet; // ActorSheet

uuid :string

Source:

A Universally Unique Identifier (uuid) for this Entity instance

Type:
  • string

visible :boolean

Source:

A boolean indicator for whether or not the current game User has at least limited visibility for this Entity.

Type:
  • boolean

Methods

(async, static) create(data, options) → {Promise}

Source:

Create a new entity using provided input data The data for entity creation is typically provided from the server through the 'create' socket Alternatively, the creation event may originate locally and the new entity can be pushed back to the server.

Example
const createData = {name: "New Entity", img: "path/to/profile.jpg"};
const created = await Entity.create(createData); // Saved to the database
const temp = await Entity.create(createData, {temporary: true}); // Not saved to the database
Parameters:
Name Type Description
data Object

The data with which to create the entity

options Object

Additional options which customize the creation workflow

Properties
Name Type Description
temporary boolean

Create a temporary entity which is not saved to the world database. Default is false.

renderSheet boolean

Display the sheet for the created entity once it is created. Default is false.

Returns:

A Promise which resolves to contain the created Entity

Type
Promise

(async, static) createMany(data, options) → {Promise}

Source:

Create multiple new Entities using provided input data Array, containing one Object per created Entity.

Example
const dataArray = [{name: "Entry 1"}, {name: "Entry 2"}, {name: "Entry 3"}];
const entries = await Entity.createMany(dataArray); // Saved to the database
const temps = await Entity.createMany(dataArray, {temporary: true}); // Not saved to the database
Parameters:
Name Type Description
data Array.<Object>

The data with which to create the entity

options Object

Additional options which customize the creation workflow

Properties
Name Type Description
temporary Boolean

Created entities are temporary and not saved to the database. Default false.

renderSheet Boolean

Display sheets for each created entities. Default false.

Returns:

A Promise which resolves to contain the created Entities

Type
Promise

(async, static) deleteMany(ids, options) → {Promise}

Source:

Delete multiple Entities using a provided Array of ids, one per Entity.

Example
const deleteIds = ["dskjfk23jf23kdjs", "g90klju9yujl9hj2", "23hjdfewh23rgf3"];
const deleted = await Entity.deleteMany(deleteIds);
Parameters:
Name Type Description
ids Array.<string>

The data with which to create the entity

options Object

Additional options which customize the deletion workflow

Properties
Name Type Description
deleteAll boolean

An optional flag which specifies that all Entities should be deleted

Returns:

A Promise which resolves to contain the created Entities

Type
Promise

(async, static) updateMany(data, options) → {Promise}

Source:

Update multiple Entities using an Array of provided update Objects which define incremental data for each Entity.

Example
const updateArray = [{_id: "dgfkjt34kjdgfkjt34", name: "Name 1"}, {_id: "dfskjkj2r3kjdvkj2", name: "Name 2"}];
const updated = await Entity.updateMany(updateArray);
Parameters:
Name Type Description
data Array.<Object>

Data with which to update each Entity. Each Object must include the _id

options Object

Additional options which customize the update workflow

Returns:

A Promise which resolves to contain the updated Entities

Type
Promise

(async) clone(createData, options) → {Promise.<Entity>}

Source:

Clone an Entity, creating a new Entity using the current data as well as provided creation overrides.

Parameters:
Name Type Description
createData Object

Additional data which overrides current Entity data at the time of creation

options Object

Additional creation options passed to the Entity.create method

Returns:

A Promise which resolves to the created clone Entity

Type
Promise.<Entity>

(async) createEmbeddedEntity(embeddedName, createData, options) → {Promise}

Source:

Create one EmbeddedEntity within this parent Entity. Dispatch the creation request to the server for handling. The result will be acknowledged to this client, and broadcast to other connected clients.

Parameters:
Name Type Description
embeddedName string

The name of the Embedded Entity class to create

createData Object

An object of initial data from which to create the Embedded Entity

options Object

Additional creation options which modify the request

Returns:

A Promise which resolves to this Entity once the creation request is successful

Type
Promise

(async) createManyEmbeddedEntities(embeddedName, createData, options) → {Promise}

Source:

Create multiple Embedded Entities within this parent Entity using an Array of creation data. Dispatch the update request to the server for handling. The result will be acknowledged to this client, and broadcast to other connected clients.

Parameters:
Name Type Description
embeddedName string

The name of the Embedded Entity class to update

createData Array

An Array of initial data objects from which to create the Embedded Entities.

options Object

Additional update options which modify the request

Returns:

A Promise which resolves to this Entity once the creation request is successful

Type
Promise

(async) delete(options) → {Promise}

Source:

Delete the entity, removing it from its collection and deleting its data record

Example
const deleted = await entity.delete();
Parameters:
Name Type Description
options Object

Additional options which customize the deletion workflow

Returns:

A Promise which resolves to the ID of the deleted Entity once handled by the server

Type
Promise

(async) deleteEmbeddedEntity(embeddedName, childId, options) → {Promise}

Source:

Delete one EmbeddedEntity within this parent Entity. Dispatch the deletion request to the server for handling. The result will be acknowledged to this client, and broadcast to other connected clients.

Parameters:
Name Type Description
embeddedName string

The name of the Embedded Entity class to delete

childId string

The id of the existing Embedded Entity child to delete

options Object

Additional deletion options which modify the request

Returns:

A Promise which resolves to this Entity once the deletion request is successful

Type
Promise

(async) deleteManyEmbeddedEntities(embeddedName, deleteIds, options) → {Promise}

Source:

Delete multiple Embedded Entities within this parent Entity by an Array of child ids. Dispatch the update request to the server for handling. The result will be acknowledged to this client, and broadcast to other connected clients.

Parameters:
Name Type Description
embeddedName string

The name of the Embedded Entity class to update

deleteIds Array

An Array of Embedded Entity ids to delete from the parent Entity

options Object

Additional update options which modify the request

Returns:

A Promise which resolves to this Entity once the update request is successful

Type
Promise

exportToJSON()

Source:

Export entity data to a JSON file which can be saved by the client and later imported into a different session

getEmbeddedCollection(embeddedName) → {Array}

Source:

Obtain a reference to the Array of source data within the data object for a certain Embedded Entity name

Parameters:
Name Type Description
embeddedName string

The name of the Embedded Entity type

Returns:

The Array of source data where Embedded Entities of this type are stored

Type
Array

getEmbeddedEntity(embeddedName, id, strict) → {Object|null}

Source:

Get an Embedded Entity by it's id from a named collection in the parent Entity.

Parameters:
Name Type Description
embeddedName string

The name of the Embedded Entity type to retrieve

id string

The numeric ID of the child to retrieve

strict boolean

Throw an Error if the requested id does not exist, otherwise return null. Default false.

Returns:

Retrieved data for the requested child, or null

Type
Object | null

getFlag(scope, key) → {*}

Source:

Get the value of a "flag" for this Entity See the setFlag method for more details on flags

Parameters:
Name Type Description
scope String

The flag scope which namespaces the key

key String

The flag key

Returns:

The flag value

Type
*

hasPerm(user, permission, exact) → {boolean}

Source:

Test whether a provided User a specific permission level (or greater) over the Entity instance

Example

Test whether a specific user has a certain permission

// These two are equivalent
entity.hasPerm(game.user, "OWNER");
entity.owner;
// These two are also equivalent
entity.hasPerm(game.user, "LIMITED", true);
entity.limited;
Parameters:
Name Type Default Description
user User

The user to test for permission

permission string | number

The permission level or level name to test

exact boolean false

Tests for an exact permission level match, by default this method tests for an equal or greater permission level.

Returns:

Whether or not the user has the permission for this Entity.

Type
boolean

(async) importFromJSON(json) → {Promise.<Entity>}

Source:

Import data and update this entity

Parameters:
Name Type Description
json String

JSON data string

Returns:

The updated Entity

Type
Promise.<Entity>

(async) importFromJSONDialog() → {Promise.<void>}

Source:

Render an import dialog for updating the data related to this Entity through an exported JSON file

Returns:
Type
Promise.<void>

initialize()

Source:

Initialize data structure for the Entity. First initialize any Embedded Entities and prepare their data. Next prepare data for the Entity itself, which may depend on Embedded Entities.

prepareData()

Source:

Prepare data for the Entity whenever the instance is first created or later updated. This method can be used to derive any internal attributes which are computed in a formulaic manner. For example, in a d20 system - computing an ability modifier based on the value of that ability score.

prepareEmbeddedEntities()

Source:

Prepare Embedded Entities which exist within this parent Entity. For example, in the case of an Actor, this method is responsible for preparing the Owned Items the Actor contains.

removeEmbeddedEntity(embeddedName, id) → {Object|null}

Source:

Remove an Embedded Entity from the parent Entity data by it's id.

Parameters:
Name Type Description
embeddedName string

The name of the Embedded Entity type to retrieve

id number

The numeric ID of the child to retrieve

Returns:

The embedded entity data that was removed

Type
Object | null

render(…args)

Source:

Render all of the Application instances which are connected to this Entity by calling their respective Application#render methods.

Parameters:
Name Type Attributes Description
args * <repeatable>

Variable arguments which are forwarded to each Application's render call

(async) setFlag(scope, key, value) → {Promise.<Entity>}

Source:

Assign a "flag" to this Entity. Flags represent key-value type data which can be used to store flexible or arbitrary data required by either the core software, game systems, or user-created modules.

Each flag should be set using a scope which provides a namespace for the flag to help prevent collisions.

Flags set by the core software use the "core" scope. Flags set by game systems or modules should use the canonical name attribute for the module Flags set by an individual world should "world" as the scope.

Flag values can assume almost any data type. Setting a flag value to null will delete that flag.

Parameters:
Name Type Description
scope String

The flag scope which namespaces the key

key String

The flag key

value *

The flag value

Returns:

A Promise resolving to the updated Entity

Type
Promise.<Entity>

(async) sortRelative()

Source:

Sort this Entity relative a target by providing the target, an Array of siblings and other options. If the Entity has an rendered sheet, record the sort change as part of a form submission See SortingHelper.performIntegerSort for more details

toJSON() → {Object}

Source:

Serializing an Entity should simply serialize it's inner data, not the entire instance

Returns:
Type
Object

(async) unsetFlag(scope, key) → {Promise}

Source:

Remove a flag assigned to the Entity

Parameters:
Name Type Description
scope string

The flag scope which namespaces the key

key string

The flag key

Returns:

A Promise resolving to the updated Entity

Type
Promise

(async) update(data, options) → {Promise}

Source:

Update the current entity using new data This new data is typically provided from the server through the 'update' socket Alternatively, the update may originate locally, in which case it can be pushed back to the server

Example
const updateData = {name: "New Name"};
const updated = await entity.update(updateData);
Parameters:
Name Type Description
data Object

The data with which to update the entity

options Object

Additional options which customize the update workflow

Properties
Name Type Description
diff Boolean

Diff the provided data against existing entity data, only submitting the difference to the server. Default is true.

Returns:

A Promise which resolves to the updated Entity

Type
Promise

(async) updateEmbeddedEntity(embeddedName, updateData, options) → {Promise}

Source:

Update one EmbeddedEntity within this parent Entity using incremental data. Dispatch the update request to the server for handling. The result will be acknowledged to this client, and broadcast to other connected clients.

Parameters:
Name Type Description
embeddedName string

The name of the Embedded Entity class to update

updateData Object

An object of incremental data from which to update the Embedded Entity

options Object

Additional update options which modify the request

Returns:

A Promise which resolves to this Entity once the update request is successful

Type
Promise

(async) updateManyEmbeddedEntities(embeddedName, updateData, options) → {Promise}

Source:

Update multiple Embedded Entities within this parent Entity using incremental data. Dispatch the update request to the server for handling. The result will be acknowledged to this client, and broadcast to other connected clients.

Parameters:
Name Type Description
embeddedName string

The name of the Embedded Entity class to update

updateData Array

An Array of incremental data, one per Embedded Entity, with which to update the Entity

options Object

Additional update options which modify the request

Returns:

A Promise which resolves to this Entity once the update request is successful

Type
Promise