API Reference
Complete reference for all bitECS 0.4 functions.
World
createWorld
createWorld(context?, entityIndex?) → WorldCreates a new world. Optionally accepts a context object and/or shared entity index.
Parameters
contextobject? - Custom context object to attach to the worldentityIndexEntityIndex? - Shared entity index for multiple worlds
Returns
World - The created world object
resetWorld
resetWorld(world) → WorldResets a world to its initial state, removing all entities and components.
Parameters
worldWorld - The world to reset
Returns
World - The reset world
deleteWorld
deleteWorld(world) → voidDeletes a world and cleans up its internal data.
Parameters
worldWorld - The world to delete
getWorldComponents
getWorldComponents(world) → ComponentRef[]Returns all components registered to a world.
Parameters
worldWorld - The world object
Returns
ComponentRef[] - Array of registered components
getAllEntities
getAllEntities(world) → number[]Returns all existing entities in a world.
Parameters
worldWorld - The world object
Returns
number[] - Array of entity IDs
Entity
addEntity
addEntity(world) → numberAdds a new entity to the world.
Parameters
worldWorld - The world object
Returns
number - The new entity ID
removeEntity
removeEntity(world, eid) → voidRemoves an entity from the world.
Parameters
worldWorld - The world objecteidnumber - Entity ID to remove
entityExists
entityExists(world, eid) → booleanChecks if an entity exists in the world.
Parameters
worldWorld - The world objecteidnumber - Entity ID to check
Returns
boolean - True if entity exists
getEntityComponents
getEntityComponents(world, eid) → ComponentRef[]Returns all components an entity has.
Parameters
worldWorld - The world objecteidnumber - Entity ID
Returns
ComponentRef[] - Array of components
addPrefab
addPrefab(world) → numberCreates a new prefab entity. Prefabs are excluded from normal queries.
Parameters
worldWorld - The world object
Returns
number - The prefab entity ID
createEntityIndex
createEntityIndex(options?) → EntityIndexCreates a standalone entity index for sharing across worlds.
Parameters
optionsobject? - Configuration from withVersioning()
Returns
EntityIndex - The entity index
withVersioning
withVersioning(versionBits?) → objectCreates configuration for entity ID recycling with versioning.
Parameters
versionBitsnumber? - Bits for version (default: 8)
Returns
object - Configuration object
Component
registerComponent
registerComponent(world, component) → ComponentDataExplicitly registers a component with a world.
Parameters
worldWorld - The world objectcomponentComponentRef - Component to register
Returns
ComponentData - Registered component data
registerComponents
registerComponents(world, components) → voidRegisters multiple components at once.
Parameters
worldWorld - The world objectcomponentsComponentRef[] - Components to register
addComponent
addComponent(world, eid, component) → booleanAdds a component to an entity.
Parameters
worldWorld - The world objecteidnumber - Entity IDcomponentComponentRef | ComponentSetter - Component or set(component, data)
Returns
boolean - True if added, false if already existed
addComponents
addComponents(world, eid, ...components) → voidAdds multiple components to an entity.
Parameters
worldWorld - The world objecteidnumber - Entity IDcomponents...ComponentRef - Components to add
removeComponent
removeComponent(world, eid, ...components) → voidRemoves one or more components from an entity.
Parameters
worldWorld - The world objecteidnumber - Entity IDcomponents...ComponentRef - Components to remove
hasComponent
hasComponent(world, eid, component) → booleanChecks if an entity has a component.
Parameters
worldWorld - The world objecteidnumber - Entity IDcomponentComponentRef - Component to check
Returns
boolean - True if entity has component
getComponent
getComponent(world, eid, component) → anyGets component data (triggers onGet observers).
Parameters
worldWorld - The world objecteidnumber - Entity IDcomponentComponentRef - Component to get
Returns
any - Component data or undefined
setComponent
setComponent(world, eid, component, data) → voidSets component data (triggers onSet observers).
Parameters
worldWorld - The world objecteidnumber - Entity IDcomponentComponentRef - Component to setdataany - Data to set
set
set(component, data) → ComponentSetterHelper to set component data when adding.
Parameters
componentComponentRef - The componentdataany - Initial data
Returns
ComponentSetter - Object for use with addComponent
Query
query
query(world, terms, ...modifiers) → QueryResultQueries entities matching component patterns.
Parameters
worldWorld - The world objecttermsQueryTerm[] - Components and operatorsmodifiers...QueryModifier - asBuffer, isNested, etc.
Returns
QueryResult - Array of matching entity IDs
And / All
And(...components) → OpReturnTypeMatches entities with ALL specified components (default behavior).
Parameters
components...ComponentRef - Components to match
Returns
OpReturnType - Query operator
Or / Any
Or(...components) → OpReturnTypeMatches entities with ANY of the specified components.
Parameters
components...ComponentRef - Components to match
Returns
OpReturnType - Query operator
Not / None
Not(...components) → OpReturnTypeExcludes entities with the specified components.
Parameters
components...ComponentRef - Components to exclude
Returns
OpReturnType - Query operator
commitRemovals
commitRemovals(world) → voidCommits all pending entity removals for queries.
Parameters
worldWorld - The world object
Query Modifiers
asBuffer- Return Uint32Array instead of number[]isNested/noCommit- Skip committing removals (safe for nested queries)
Observer
observe
observe(world, hook, callback) → () => voidSubscribes to component changes.
Parameters
worldWorld - The world objecthookObservableHook - onAdd, onRemove, onSet, or onGetcallbackfunction - Handler function
Returns
function - Unsubscribe function
onAdd
onAdd(...components) → ObservableHookTriggers when entity gains all specified components.
Parameters
components...ComponentRef - Components to observe
Returns
ObservableHook - Hook for observe()
onRemove
onRemove(...components) → ObservableHookTriggers when entity loses any specified component.
Parameters
components...ComponentRef - Components to observe
Returns
ObservableHook - Hook for observe()
onSet
onSet(component) → ObservableHookTriggers when component data is set. Callback receives (eid, params).
Parameters
componentComponentRef - Component to observe
Returns
ObservableHook - Hook for observe()
onGet
onGet(component) → ObservableHookTriggers when component data is retrieved. Callback returns data.
Parameters
componentComponentRef - Component to observe
Returns
ObservableHook - Hook for observe()
Relation
createRelation
createRelation(...modifiers) → RelationCreates a new relation with optional modifiers.
Parameters
modifiers...function - withStore, makeExclusive, etc.
Returns
Relation - The created relation
withStore
withStore(createStore) → (Relation) => RelationAdds a data store to a relation.
Parameters
createStore() => T - Function returning store object
Returns
function - Relation modifier
makeExclusive
makeExclusive(relation) → RelationMakes a relation exclusive (one target per subject).
Parameters
relationRelation - The relation to modify
Returns
Relation - Modified relation
withAutoRemoveSubject
withAutoRemoveSubject(relation) → RelationRemoves subject when target is removed.
Parameters
relationRelation - The relation to modify
Returns
Relation - Modified relation
withOnTargetRemoved
withOnTargetRemoved(callback) → (Relation) => RelationAdds callback when a target is removed.
Parameters
callback(world, subject, target) => void - Handler
Returns
function - Relation modifier
getRelationTargets
getRelationTargets(world, eid, relation) → number[]Gets all targets for an entity's relation.
Parameters
worldWorld - The world objecteidnumber - Subject entity IDrelationRelation - The relation
Returns
number[] - Array of target entity IDs
Wildcard
WildcardMatches any target in relation queries.
IsA
IsABuilt-in relation for prefab inheritance.
Hierarchy
Hierarchy / Cascade
Hierarchy(relation, depth?) → HierarchyTermCreates hierarchy query term for topological ordering.
Parameters
relationComponentRef - The relation (e.g., ChildOf)depthnumber? - Optional depth limit
Returns
HierarchyTerm - Hierarchy term for queries
getHierarchyDepth
getHierarchyDepth(world, eid, relation) → numberGets the hierarchy depth of an entity.
Parameters
worldWorld - The world objecteidnumber - Entity IDrelationComponentRef - The relation
Returns
number - Depth (0 = root)
getMaxHierarchyDepth
getMaxHierarchyDepth(world, relation) → numberGets the maximum depth in a hierarchy.
Parameters
worldWorld - The world objectrelationComponentRef - The relation
Returns
number - Maximum depth
Serialization
Import from bitecs/serialization
SoA Serialization
createSoASerializer
createSoASerializer(components, options?) → (eids) => ArrayBufferCreates a serializer for Structure of Arrays components.
Parameters
componentsComponentRef[] - Components to serializeoptionsobject? - diff, buffer, epsilon
Returns
function - Serializer function
createSoADeserializer
createSoADeserializer(components, options?) → (buffer, idMap?) => voidCreates a deserializer for SoA data.
Parameters
componentsComponentRef[] - Components to deserializeoptionsobject? - diff
Returns
function - Deserializer function
AoS Serialization
createAoSSerializer
createAoSSerializer(components, options?) → (eids) => ArrayBufferCreates a serializer for Array of Structures components.
Parameters
componentsComponentRef[] - Components to serializeoptionsobject? - diff, buffer, epsilon
Returns
function - Serializer function
createAoSDeserializer
createAoSDeserializer(components, options?) → (buffer, idMap?) => voidCreates a deserializer for AoS data.
Parameters
componentsComponentRef[] - Components to deserializeoptionsobject? - diff
Returns
function - Deserializer function
Observer Serialization
createObserverSerializer
createObserverSerializer(world, networkTag, components, options?) → () => ArrayBufferCreates a serializer that tracks entity/component additions and removals.
Parameters
worldWorld - The world objectnetworkTagComponentRef - Tag for networked entitiescomponentsComponentRef[] - Components to trackoptionsobject? - buffer
Returns
function - Serializer function
createObserverDeserializer
createObserverDeserializer(world, networkTag, components, options?) → (buffer, idMap?) => voidCreates a deserializer for observer data.
Parameters
worldWorld - The world objectnetworkTagComponentRef - Tag for networked entitiescomponentsComponentRef[] - Components to trackoptionsobject? - idMap
Returns
function - Deserializer function
Snapshot Serialization
createSnapshotSerializer
createSnapshotSerializer(world, components, buffer?) → () => ArrayBufferCreates a serializer for full world state.
Parameters
worldWorld - The world objectcomponentsComponentRef[] - Components to serializebufferArrayBuffer? - Backing buffer
Returns
function - Serializer function
createSnapshotDeserializer
createSnapshotDeserializer(world, components) → (buffer, idMap?) => voidCreates a deserializer for snapshot data.
Parameters
worldWorld - The world objectcomponentsComponentRef[] - Components to deserialize
Returns
function - Deserializer function
Type Tags
u8(),i8()- 8-bit integersu16(),i16()- 16-bit integersu32(),i32()- 32-bit integersf32()- 32-bit floatsf64()- 64-bit floats (default)str()- UTF-8 stringsarray(type)- Arrays of type