Skip to content

Handle

Defined in: handle.ts:163

A generic handle that represents any special kind of entity, such as a component or a resource.

Although the type means Typescript doesn’t know the exact kind of entity, it is known at runtime. Because of this, if you already know the kind, you can simply type cast it:

const Person = component()
const bob = entity().set(Person)
query(Person).forEach((handle) => {
// We know for sure that we won't be giving the `Person` component to anything
// other than simple entities, so we can safely type cast it as one.
const entity = handle as EntityHandle
})

readonly id: RawId

Defined in: handle.ts:175

A numerical entity identifier used internally.

Useful when you cannot pass the entire handle to something, but can pass its numerical ID instead. For example, storing an entity’s ID in an instance’s attribute, which cannot hold complex data structures.

In order to get back the appropriate handle from an ID, use the resolveId function.

children(): Handle[]

Defined in: handle.ts:422

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]

clear(): this

Defined in: handle.ts:330

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

Components and relationship pairs with the Persistent component (i.e. most built-in components) will not be removed.

this


components(): ComponentHandle<unknown>[]

Defined in: handle.ts:346

Returns all components associated with this entity.

ComponentHandle<unknown>[]


despawn(): void

Defined in: handle.ts:526

Completely deletes this entity from the world.

Throws an error if the entity is marked as persistent.

void


exists(): boolean

Defined in: handle.ts:433

Returns true if this entity exists in the world.

boolean


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

Defined in: handle.ts:263

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

Missing components or pairs will return undefined.

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

Args

WrapLuaTuple<Flatten<Nullable<InferValues<Args>>>>

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

has(…componentsOrPairs): boolean

Defined in: handle.ts:293

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<ComponentHandle<unknown> | Pair<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?
}

parent(): Handle

Defined in: handle.ts:405

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

Handle

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

relationships(): Pair<unknown>[]

Defined in: handle.ts:364

Returns all relationship pairs associated with this entity.

Pair<unknown>[]


remove(componentOrPair): this

Defined in: handle.ts:306

Removes a component or relationship pair from this entity.

Throws an error if trying to remove a component or relationship pair with the Persistent component (i.e. most built-in components).

ComponentHandle<unknown> | Pair<unknown>

this


set(tagComponent): this

Defined in: handle.ts:192

Assigns a tag component to this entity.

ComponentHandle<undefined>

this

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

set<V>(component, value): this

Defined in: handle.ts:206

Assigns a component and its value to this entity.

V

ComponentHandle<V>

NoInfer<V>

this

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

set(tagPair): this

Defined in: handle.ts:219

Assigns a relationship pair to this entity.

Pair<undefined>

this

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

set<P>(pair, value): this

Defined in: handle.ts:235

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)

targetOf(relation, nth?): Handle

Defined in: handle.ts:458

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

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)

targetsOf(relation): Handle[]

Defined in: handle.ts:479

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)

toString(): string

Defined in: handle.ts:390

Gets the label assigned to this entity.

string