Skip to content

ResourceHandle

Defined in: src/handle.ts:543

A handle for resources spawned with resource().

Value = unknown

[VALUE_SYMBOL]: Value

Defined in: src/handle.ts:544


readonly id: RawId

Defined in: src/handle.ts:130

The numeric ID underlying this handle.

Meant to be used when one cannot use the higher-level abstractions provided by Toucan, such as storing an entity’s ID in an instance’s attribute, which cannot hold complex data structures.

In order to get back the high-level handle from an ID, use the resolveId function.

Handle.id

changed(listener): () => void

Defined in: src/handle.ts:572

Registers a listener that is called whenever the value of this resource changes.

The returned function can be called to unregister the listener.

(newValue) => void

(): void

void


children(): Handle[]

Defined in: src/handle.ts:347

Gets all children (the sources of ChildOf relationships) for this entity.

Handle[]

const alice = entity()
const charlie = entity().set(pair(ChildOf, alice))
const bob = entity().set(pair(ChildOf, alice))
const children = alice.children() // [charlie, bob]

Handle.children


clear(): this

Defined in: src/handle.ts:262

Clears all components and relationship pairs from this entity, but does not despawn the entity.

this

Handle.clear


components(): ComponentHandle<unknown>[]

Defined in: src/handle.ts:271

Returns all components associated with this entity.

ComponentHandle<unknown>[]

Handle.components


despawn(): void

Defined in: src/handle.ts:449

Completely deletes this entity from the world.

void

Handle.despawn


exists(): boolean

Defined in: src/handle.ts:358

Returns true if this entity exists.

boolean

Handle.exists


get<Args>(…componentsOrPairs): Flatten<Nullable<InferValues<Args>>>

Defined in: src/handle.ts:216

Retrieves the values of up to 4 components or relationship pairs on this entity.

Missing components or pairs will return undefined.

Args extends OneUpToFour<Pair<unknown> | ComponentHandle<unknown>>

Args

Flatten<Nullable<InferValues<Args>>>

const name = myEntity.get(Name)
const [position, velocity] = myEntity.get(Position, Velocity)
const carCount = myEntity.get(pair(Owns, car))

Handle.get


has(…componentsOrPairs): boolean

Defined in: src/handle.ts:245

Returns true if this entity has all of the specified components or relationship pairs.

A maximum of 4 components or pairs can be checked at once.

OneUpToFour<Pair<unknown> | ComponentHandle<unknown>>

boolean

const IsDead = component()
const Owns = component()
const house = entity()
const bob = entity()
.set(IsDead)
.set(pair(Owns, house))
if (bob.has(IsDead, pair(Owns, house))) {
// Why don't we rob Bob's house?
}

Handle.has


parent(): Handle | undefined

Defined in: src/handle.ts:330

Gets the parent (the target of a ChildOf relationship) for this entity, if such a relationship exists.

Handle | undefined

const alice = entity()
const charlie = entity().set(pair(ChildOf, alice))
const parent = charlie.parent() // alice

Handle.parent


read(): Value

Defined in: src/handle.ts:552

Returns the current value of this resource.

Not to be confused with get, which can be used to retrieve the value of components attached to resources, just like with entities.

Value


relationships(): Pair<unknown>[]

Defined in: src/handle.ts:292

Returns all relationship pairs associated with this entity.

Pair<unknown>[]

Handle.relationships


remove(componentOrPair): this

Defined in: src/handle.ts:252

Removes a component or relationship pair from this entity.

Pair<unknown> | ComponentHandle<unknown>

this

Handle.remove


set(tagComponent): this

Defined in: src/handle.ts:145

Assigns a tag component to this entity.

ComponentHandle<undefined>

this

const IsAlive = component()
myEntity.set(IsAlive)

Handle.set

set<V>(component, value): this

Defined in: src/handle.ts:159

Assigns a component and its value to this entity.

V

ComponentHandle<V>

NoInfer

this

const Health = component<number>()
const Stamina = component<number>()
entity()
.set(Health, 100)
.set(Stamina, 50)

Handle.set

set(tagPair): this

Defined in: src/handle.ts:172

Assigns a relationship pair to this entity.

Pair<undefined>

this

const Likes = component()
const bob = entity()
const alice = entity()
.set(pair(Likes, bob))

Handle.set

set<P>(pair, value): this

Defined in: src/handle.ts:188

Assigns a relationship pair and its value to this entity.

P extends Pair<unknown>

P

InferValue<P>

this

const Owns = component<number>()
const car = entity()
const perfume = entity()
const alice = entity()
.set(pair(Owns, car), 2)
.set(pair(Owns, perfume), 5)

Handle.set


targetOf(relation, nth?): Handle | undefined

Defined in: src/handle.ts:383

Returns the target entity of a relationship pair from this entity.

If there are multiple targets for the given relationship, the nth index can be specified (starting at 0).

ComponentHandle

number = 0

Handle | undefined

const Likes = component()
const bob = entity()
const charlie = entity()
const alice = entity()
.set(pair(Likes, bob))
.set(pair(Likes, charlie))
// The order of targets is not guaranteed.
const maybeBob = alice.targetOf(Likes, 0)
const maybeCharlie = alice.targetOf(Likes, 1)

Handle.targetOf


targetsOf(relation): Handle[]

Defined in: src/handle.ts:404

Returns all target entities of a relationship pair from this entity.

ComponentHandle

Handle[]

const Likes = component()
const bob = entity()
const charlie = entity()
const alice = entity()
.set(pair(Likes, bob))
.set(pair(Likes, charlie))
const likedEntities = alice.targetsOf(Likes)

Handle.targetsOf


toString(): string

Defined in: src/handle.ts:315

Gets the label assigned to this entity.

string

Handle.toString


write(value): this

Defined in: src/handle.ts:562

Updates the value of the resource.

Not to be confused with set, which can be used to set the value of components attached to resources, just like with entities.

Value

this