Handle
Defined in: src/handle.ts:136
The base class that represents any kind of entity, may it be a simple entity, a component, or any other variation.
Although the type means Typescript doesn’t know the exact entity variation, it is known at runtime. Because of this, one that knows the exact variation can simply type cast it, like so:
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})Extended by
Section titled “Extended by”Properties
Section titled “Properties”
readonlyid:RawId
Defined in: src/handle.ts:148
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.
Methods
Section titled “Methods”children()
Section titled “children()”children():
Handle[]
Defined in: src/handle.ts:372
Gets all children (the sources of ChildOf relationships) for this entity.
Returns
Section titled “Returns”Handle[]
Example
Section titled “Example”const alice = entity()const charlie = entity().set(pair(ChildOf, alice))const bob = entity().set(pair(ChildOf, alice))
const children = alice.children() // [charlie, bob]clear()
Section titled “clear()”clear():
this
Defined in: src/handle.ts:286
Clears all components and relationship pairs from this entity, but does not despawn the entity.
Components with the Persistent component (i.e. built-in components) will not removed.
Returns
Section titled “Returns”this
components()
Section titled “components()”components():
ComponentHandle<unknown>[]
Defined in: src/handle.ts:298
Returns all components associated with this entity.
Returns
Section titled “Returns”ComponentHandle<unknown>[]
despawn()
Section titled “despawn()”despawn():
void
Defined in: src/handle.ts:474
Completely deletes this entity from the world.
Returns
Section titled “Returns”void
exists()
Section titled “exists()”exists():
boolean
Defined in: src/handle.ts:383
Returns true if this entity exists.
Returns
Section titled “Returns”boolean
get<
Args>(…componentsOrPairs):WrapLuaTuple<Flatten<Nullable<InferValues<Args>>>>
Defined in: src/handle.ts:234
Retrieves the values of up to 4 components or relationship pairs on this entity.
Missing components or pairs will return undefined.
Type Parameters
Section titled “Type Parameters”Args extends OneUpToFour<Pair<unknown> | ComponentHandle<unknown>>
Parameters
Section titled “Parameters”componentsOrPairs
Section titled “componentsOrPairs”…Args
Returns
Section titled “Returns”WrapLuaTuple<Flatten<Nullable<InferValues<Args>>>>
Example
Section titled “Example”const name = myEntity.get(Name)
const [position, velocity] = myEntity.get(Position, Velocity)
const carCount = myEntity.get(pair(Owns, car))has(…
componentsOrPairs):boolean
Defined in: src/handle.ts:259
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.
Parameters
Section titled “Parameters”componentsOrPairs
Section titled “componentsOrPairs”…OneUpToFour<Pair<unknown> | ComponentHandle<unknown>>
Returns
Section titled “Returns”boolean
Example
Section titled “Example”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()
Section titled “parent()”parent():
Handle|undefined
Defined in: src/handle.ts:355
Gets the parent (the target of a ChildOf relationship) for this entity, if such a relationship exists.
Returns
Section titled “Returns”Handle | undefined
Example
Section titled “Example”const alice = entity()const charlie = entity().set(pair(ChildOf, alice))
const parent = charlie.parent() // alicerelationships()
Section titled “relationships()”relationships():
Pair<unknown>[]
Defined in: src/handle.ts:316
Returns all relationship pairs associated with this entity.
Returns
Section titled “Returns”Pair<unknown>[]
remove()
Section titled “remove()”remove(
componentOrPair):this
Defined in: src/handle.ts:268
Removes a component or relationship pair from this entity.
Throws an error if trying to remove a component with the Persistent component (i.e. built-in components).
Parameters
Section titled “Parameters”componentOrPair
Section titled “componentOrPair”Pair<unknown> | ComponentHandle<unknown>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”set(
tagComponent):this
Defined in: src/handle.ts:163
Assigns a tag component to this entity.
Parameters
Section titled “Parameters”tagComponent
Section titled “tagComponent”ComponentHandle<undefined>
Returns
Section titled “Returns”this
Example
Section titled “Example”const IsAlive = component()myEntity.set(IsAlive)Call Signature
Section titled “Call Signature”set<
V>(component,value):this
Defined in: src/handle.ts:177
Assigns a component and its value to this entity.
Type Parameters
Section titled “Type Parameters”V
Parameters
Section titled “Parameters”component
Section titled “component”NoInfer
Returns
Section titled “Returns”this
Example
Section titled “Example”const Health = component<number>()const Stamina = component<number>()
entity() .set(Health, 100) .set(Stamina, 50)Call Signature
Section titled “Call Signature”set(
tagPair):this
Defined in: src/handle.ts:190
Assigns a relationship pair to this entity.
Parameters
Section titled “Parameters”tagPair
Section titled “tagPair”Pair<undefined>
Returns
Section titled “Returns”this
Example
Section titled “Example”const Likes = component()
const bob = entity()const alice = entity() .set(pair(Likes, bob))Call Signature
Section titled “Call Signature”set<
P>(pair,value):this
Defined in: src/handle.ts:206
Assigns a relationship pair and its value to this entity.
Type Parameters
Section titled “Type Parameters”P extends Pair<unknown>
Parameters
Section titled “Parameters”P
InferValue<P>
Returns
Section titled “Returns”this
Example
Section titled “Example”const Owns = component<number>()
const car = entity()const perfume = entity()
const alice = entity() .set(pair(Owns, car), 2) .set(pair(Owns, perfume), 5)targetOf()
Section titled “targetOf()”targetOf(
relation,nth?):Handle|undefined
Defined in: src/handle.ts:408
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).
Parameters
Section titled “Parameters”relation
Section titled “relation”number = 0
Returns
Section titled “Returns”Handle | undefined
Example
Section titled “Example”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()
Section titled “targetsOf()”targetsOf(
relation):Handle[]
Defined in: src/handle.ts:429
Returns all target entities of a relationship pair from this entity.
Parameters
Section titled “Parameters”relation
Section titled “relation”Returns
Section titled “Returns”Handle[]
Example
Section titled “Example”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()
Section titled “toString()”toString():
string
Defined in: src/handle.ts:340
Gets the label assigned to this entity.
Returns
Section titled “Returns”string