The Tokens Layer

The Tokens Layer is the middle layer of the tabletop Canvas and it contains all the Sprites which represent actors within the game space. Tokens are placed onto the Tokens Layer by dragging actors from the Actors Directory (in the right sidebar) onto the canvas.


The Token Layer

All tokens which are rendered on the canvas are drawn as children of the TokenLayer(). The token layer may be accessed globally as canvas.tokens and provides the following API.

class TokenLayer()

The Token Container subclass of the PlaceablesLayer

TokenLayer._lastWildcard

type: string

Remember the last drawn wildcard token image to avoid repetitions

TokenLayer.concludeAnimation()

Immediately conclude the animation of any/all tokens

TokenLayer.controlledTokens

type: Array

An Array of tokens which are currently controlled

TokenLayer.cycleTokens(direction, reset)

Cycle the controlled token by rotating through the list of Owned Tokens that are available within the Scene Tokens are currently sorted in order of their TokenID

Arguments:
  • direction (Boolean) – Which direction to cycle. A “truthy” value cycles forward, while a “falsey” value cycles backwards.
  • reset (Boolean) – Restart the cycle order back at the beginning?
Returns:

Boolean – A flag for whether token control was cycled

TokenLayer.dataArray

type: Array

Define the source data array underlying the placeable objects contained in this layer

TokenLayer.deactivate()

Override the deactivation behavior of this layer. Placeables on this layer remain visible even when the layer is inactive.

TokenLayer.dropActor(actor, tokenData)

Drop the Token representing an Actor on the canvas at a specific position

Arguments:
  • actor (Actor) – The ID of the Actor whose token should be dropped
  • tokenData (Object) – An objet of additional Token data with which to override the Actor prototype
TokenLayer.hud

Token objects on this layer utilize the TokenHUD

TokenLayer.layerOptions

Customize behaviors of this PlaceablesLayer by modifying some behaviors at a class level

TokenLayer.ownedTokens

type: Array

An Array of tokens which belong to actors which are owned

TokenLayer.placeableClass

type: PIXI.Container

Define a Container implementation used to render placeable objects contained in this layer

TokenLayer.selectObjects()

Select all the tokens within a provided set of rectangular coordinates. Control any tokens within the area which you are able to control based on token permissions. See the parent PlaceablesLayer.selectObject method for more details

TokenLayer.targetObjects(x, y, width, height, releaseOthers)

Target all Token instances which fall within a coordinate rectangle.

Arguments:
  • x (number) – The top-left x-coordinate of the selection rectangle
  • y (number) – The top-left y-coordinate of the selection rectangle
  • width (number) – The width of the selection rectangle
  • height (number) – The height of the selection rectangle
  • releaseOthers (boolean) – Whether or not to release other targeted tokens
Returns:

number – The number of Token instances which were targeted.


The Token Object

As a mod developer, you may find yourselves wanting to interact with the tokens which are active within a Scene() and are drawn within the TokenLayer(). The Token() class provides a rich API for interacting with these tokens.

class foundry.Token(data, scene)

An instance of the Token class represents an Actor within a viewed Scene on the game canvas. Each Token is reference using a numeric id which indexes its position within the scene. See the initialization signature of the parent PlaceableObject class for more details.

Arguments:
  • data (Object) – An object of token data which is used to construct a new Token.
  • scene (Scene) – The parent Scene entity within which the Token resides.
foundry.Token._drawNameplate()

Draw the token’s nameplate as a text object

Returns:PIXI.Text – The Text object for the Token nameplate
foundry.Token._getLightRadius(units)

A generic transformation to turn a certain number of grid units into a radius in canvas pixels

Arguments:
  • units (Number) – The radius in grid units
Returns:

number – The radius in canvas units

foundry.Token._noAnimate

type: Boolean

Provide a temporary flag through which this Token can be overridden to bypass any movement animation

foundry.Token._onCreate(sceneId, tokenData)

Handle token creation requests, adding the new token to the Scene data For active Scenes, also draw a new Token onto the canvas

Arguments:
  • sceneId (String) – The ID of the scene where the token is being created
  • tokenData (Object) – The data object from which to create the new token
foundry.Token._onDelete(sceneId)

Handle token deletion requests, deleting the token by ID from the Scene data For active Scenes, also remove the Token container from the canvas

Arguments:
  • sceneId (String) – The ID of the scene where the token is being created
foundry.Token._onUpdate(sceneId, data)

Update the Token with new data and push that update back to the server.

New token data is either generated by the client or provided from the server through the updateToken socket. If the updated data is originated locally, it can be pushed back to the server and emitted to other clients.

Arguments:
  • sceneId (String) – The ID of the scene where the token is being created
  • data (Object) – The data object from which to update new token
foundry.Token.actor

type: Actor

An Actor entity constructed using this Token’s data If actorLink is true, then the entity is the true Actor entity Otherwise, the Actor entity is a synthetic, constructed using the Token actorData

foundry.Token.animateMovement(ray)

Animate Token movement along a certain path which is defined by a Ray object

Arguments:
  • ray (Ray) – The path along which to animate Token movement
foundry.Token.brightLightRadius

The radius of bright light that the Token emits

foundry.Token.brightRadius

Translate the token’s bright light distance in units into a radius in pixels.

foundry.Token.canViewSheet

type: Boolean

Does the current user have at least LIMITED permission to the Token

foundry.Token.center

type: Object

The Token’s current central position

foundry.Token.checkCollision(destination, drag)

Check for collision when attempting a move to a new position

Arguments:
  • destination (Object) – An Object containing data for the attempted movement
  • drag (Boolean) – Whether we are checking collision for a drag+drop movement
Returns:

Boolean – A true/false indicator for whether the attempted movement caused a collision

foundry.Token.computeFOV()

Compute the field-of-vision for an object, determining its effective line-of-sight and field-of-vision polygons

Returns:Object – An object containing the rays, LOS polygon, and FOV polygon for the light
foundry.Token.control(multiSelect, releaseOthers, initializeSight)

Assume control over a PlaceableObject, flagging it as controlled and enabling downstream behaviors See PlaceableObject.control() for full details

Arguments:
  • multiSelect (Boolean) – Is this object being selected as part of a group?
  • releaseOthers (Boolean) – Release any other controlled objects first
  • initializeSight (Boolean) – Reinitialize the sight layer
Returns:

Boolean – A Boolean flag denoting whether or not control was successful.

foundry.Token.delete(sceneId, options)

Delete a Token from a specific Scene. Extend the default Token deletion request to also request deletion for any combatants linked to this token This extension occurs before the request is submitted to the Database. See PlaceableObject.delete() for the parent implementation.

Arguments:
  • sceneId (String) – The ID of the Scene within which to update the placeable object
  • options (Object) – Additional options which customize the deletion workflow
Returns:

Promise – A Promise which resolves to the returned socket response (if successful)

foundry.Token.dimLightRadius

The radius of dim light that the Token emits

foundry.Token.dimRadius

Translate the token’s sight distance in units into a radius in pixels.

foundry.Token.draw()

Draw the token, returning a promise once the token’s texture is loaded

Returns:Promise
foundry.Token.drawEffects()

Draw the active effects and overlay effect icons which are present upon the Token

foundry.Token.drawTooltip()

Draw a text tooltip for the token which can be used to display Elevation or a resource value

foundry.Token.emitsLight

type: Boolean

Test whether the Token emits light (or darkness) at any radius

foundry.Token.fov

type: PIXI.Polygon

The Token’s most recently computed field-of-vision polygon

foundry.Token.getBarAttribute(barName)

A helper method to retrieve the underlying data behind one of the Token’s attribute bars

Arguments:
  • barName
Returns:

Object

foundry.Token.getCenter(x, y)

Get the center-point coordinate for a given grid position

Arguments:
  • x (Number) – The grid x-coordinate that represents the top-left of the Token
  • y (Number) – The grid y-coordinate that represents the top-left of the Token
Returns:

Object – The coordinate pair which represents the Token’s center at position (x, y)

foundry.Token.getSightOrigin()

Return the token’s sight origin, tailored for the direction of their movement velocity to break ties with walls

Returns:Object
foundry.Token.h

type: Number

Translate the token’s grid height into a pixel height based on the canvas size

foundry.Token.hasActiveHUD

Is the HUD display active for this token?

foundry.Token.hasLimitedVisionAngle

type: Boolean

Test whether the Token has a limited angle of vision or light emission which would require sight to update on Token rotation

foundry.Token.hasSight

type: Boolean

Test whether the Token has sight (or blindness) at any radius

foundry.Token.inCombat

type: Boolean

An indicator for whether or not this token is currently involved in the active combat encounter.

foundry.Token.isVisible

type: Boolean

Determine whether the Token is visible to the calling user’s perspective. If the user is a GM, all tokens are visible If the user is a player, owned tokens which are not hidden are visible Otherwise only tokens whose corner or center are within the vision polygon are visible.

foundry.Token.layer

type: PlaceablesLayer

Provide a reference to the canvas layer which contains placeable objects of this type

foundry.Token.los

type: PIXI.Polygon

The Token’s most recently computed line-of-sight polygon

foundry.Token.name

type: String

Convenience access to the token’s nameplate string

foundry.Token.owner

A Boolean flag for whether the current game User has permission to control this token

foundry.Token.refresh()

Update display of the Token, pulling latest data and re-rendering the display of Token components

foundry.Token.release(resetSight)

Release control over a PlaceableObject, removing it from the controlled set See PlaceableObject.release() for full details

Arguments:
  • resetSight (Boolean) – Trigger a re-initialization of the sight layer, this may not be necessary
Returns:

Boolean – A Boolean flag confirming the object was released.

foundry.Token.rotate(args)

Extend the PlaceableObject.rotate method to prevent rotation if the Token is in the midst of a movement animation

foundry.Token.setPosition(x, y)

Set the token’s position by comparing its center position vs the nearest grid vertex Return a Promise that resolves to the Token once the animation for the movement has been completed

Arguments:
  • x (Number) – The x-coordinate of the token center
  • y (Number) – The y-coordinate of the token center
Returns:

Promise – The Token after animation has completed

foundry.Token.setTarget(targeted=true, user, releaseOthers)

Set this Token as an active target for the current game User

Arguments:
  • targeted (boolean) – Is the Token now targeted?
  • user (User|null) – Assign the token as a target for a specific User
  • releaseOthers (boolean) – Release other active targets for the same player?
foundry.Token.sheet

type: TokenConfig

Provide a singleton reference to the TileConfig sheet for this Tile instance

foundry.Token.shiftPosition(dx, dy)

Perform an incremental token movement, shifting the token’s position by some number of grid units. The offset parameters will move the token by that number of grid spaces in one or both directions.

Arguments:
  • dx (Number) – The number of grid units to shift along the X-axis
  • dy (Number) – The number of grid units to shift along the Y-axis
Returns:

Promise

foundry.Token.targeted

type: Set.<User>

Track the set of User entities which are currently targeting this Token

foundry.Token.toggleCombat()

Add or remove the currently controlled Tokens from the active combat encounter

Returns:Promise
foundry.Token.toggleEffect(texture)

Toggle an active effect by it’s texture path. Copy the existing Array in order to ensure the update method detects the data as changed.

Arguments:
  • texture (String) – The texture file-path of the effect icon to toggle on the Token.
foundry.Token.toggleOverlay(texture)

Set or remove the overlay texture for the Token by providing a new texture path

Arguments:
  • texture (String) – The texture file-path of the effect to set as the Token overlay icon
Returns:

Promise

foundry.Token.toggleVisibility()

Toggle the visibility state of any Tokens in the currently selected set

Returns:Promise
foundry.Token.w

type: Number

Translate the token’s grid width into a pixel width based on the canvas size