Options
All
  • Public
  • Public/Protected
  • All
Menu

Namespace Entity

A module used to manage entities and their components.

Index

Type aliases

Functions

Type aliases

Id

Id: number

An unsigned integer between 0 and Number.MAX_SAFE_INTEGER that uniquely identifies an entity or schema within the ECS.

Functions

reserve

  • reserve(world: World.Struct, entity?: number): number
  • Reserve an entity id without inserting it into the world.

    example

    Add a component to a reserved entity

    const entity = Entity.reserve(world)
    Entity.has(world, entity, [Position]) // Error: Failed ... entity is not real
    Entity.set(world, entity, [Position])
    Entity.has(world, entity, [Position]) // true

    Parameters

    • world: World.Struct
    • entity: number = world.entityHead

    Returns number

make

  • make<$Type>(world: World.Struct, layout?: $Type, init?: Init<$Type>): number
  • Create an entity using an array of schema ids as a template. Optionally accepts an array of data used to initialize components. Undefined values within the initializer array are filled with defaults (e.g. 0 for numeric Format).

    example

    Make an entity with no components

    Entity.make(world, [])
    
    example

    Make an entity with one or more components

    Entity.make(world, [Health, Stamina])
    
    example

    Initialize component values

    Entity.make(world, [Health, Stamina], [120, 100])
    
    example

    Initialize a single component value

    Entity.make(world, [Position, Velocity], [, { x: -10, y: 42 }])
    

    Type parameters

    • $Type: readonly Schema.Id<AnySchema>[]

    Parameters

    • world: World.Struct
    • layout: $Type = ...
    • init: Init<$Type> = ...

    Returns number

get

  • get<$Type>(world: World.Struct, entity: number, layout: $Type, out?: unknown[]): RowData<$Type>
  • Get the value of one or more components for an entity. Throws an error if the entity is not real, or if it does not have a component of each of the provided schema ids.

    example

    Get the value of a single component

    const [position] = Entity.get(world, entity, [Position])
    
    example

    Get the value of multiple components

    const [health, inventory] = Entity.get(world, entity, [Health, Inventory])
    
    example

    Re-use an array to avoid allocating intermediate array

    const results = []
    const [health, stats] = Entity.get(world, entity, Player, results)

    Type parameters

    • $Type: readonly Schema.Id<AnySchema>[]

    Parameters

    • world: World.Struct
    • entity: number
    • layout: $Type
    • out: unknown[] = []

    Returns RowData<$Type>

set

  • set<$Type>(world: World.Struct, entity: number, layout: $Type, init?: Init<$Type>): void
  • Update the value of one or more components for an entity. If the entity does not yet have components of the provided schema ids, add them. Throws an error if the entity is not real.

    This function has the same interface as Entity.make, that is, it optionally accepts a sparse array of initial component values.

    example

    Add or update a single component

    Entity.set(world, entity, [Position])
    
    example

    Initialize component values

    Entity.set(world, entity, [Health, Stamina], [100, 120])
    
    example

    Update an existing component and add a new component

    const entity = Entity.make(world, [Health], [100])
    Entity.set(world, entity, [Health, Stamina], [99])

    Type parameters

    • $Type: readonly Schema.Id<AnySchema>[]

    Parameters

    • world: World.Struct
    • entity: number
    • layout: $Type
    • init: Init<$Type> = ...

    Returns void

unset

  • unset<$Type>(world: World.Struct, entity: number, layout: $Type): void
  • Remove one or more components from an entity. Throws an error if the entity does not exist.

    example

    Remove a single component from an entity

    Entity.unset(world, entity, [Health])
    
    example

    Remove multiple components from an entity

    Entity.unset(world, entity, [Health, Faction])
    

    Type parameters

    • $Type: readonly Schema.Id<AnySchema>[]

    Parameters

    Returns void

has

  • Check if an entity has one or more components. Returns true if the entity has a component corresponding to all of the provided schema ids, otherwise returns false. Throws an error if the entity is not real.

    example

    Check if an entity has a single component

    Entity.has(world, entity, [Health])
    
    example

    Check if an entity has multiple components

    Entity.has(world, entity, [Position, Velocity])
    

    Parameters

    Returns boolean

tryHas

  • Check if an entity has one or more components. Unlike has, tryHas will not throw if the entity is not real.

    Parameters

    Returns boolean

destroy

  • Remove all components from an entity. Throws an error if the entity does not exist.

    example

    Destroy an entity

    Entity.destroy(world, entity)
    

    Parameters

    Returns void

Generated using TypeDoc