8.1. DECS, Daslang entity component system
The DECS module implements a Data-oriented Entity Component System. Entities are identified by integer IDs and store components as typed data. Systems query and process entities by their component signatures, enabling cache-friendly batch processing of game objects.
See tutorial_decs for a hands-on tutorial.
All functions and symbols are in “decs” module, use require to get access to it.
require daslib/decs
8.1.1. Type aliases
- ComponentHash = uint64
Hash value of the ECS component type
- TypeHash = uint64
Hash value of the individual type
- DeferEval = lambda<(var act:DeferAction):void>
Lambda which holds deferred action. Typically creation of destruction of an entity.
- ComponentMap = array<ComponentValue>
Table of component values for individual entity.
- PassFunction = function<void>
One of the callbacks which form individual pass.
8.1.2. Constants
- INVALID_ENTITY_ID = struct<decs::EntityId>(uninitialized)
Entity ID which represents invalid entity.
8.1.3. Structures
- CTypeInfo
Type information for the individual component subtype. Consists of type name and collection of type-specific routines to control type values during its lifetime, serialization, etc.
- Fields:
basicType : Type - basic type of the component
mangledName : string - mangled name of the type
fullName : string - full name of the type
hash : TypeHash - hash of the type
size : uint - size of the type
eraser : function<(arr:array<uint8>):void> - function to erase component value
clonner : function<(dst:array<uint8>;src:array<uint8>):void> - function to clone component value
serializer : function<(arch: Archive;arr:array<uint8>;name:string):void> - function to serialize component value
dumper : function<(elem:void?):string> - function to dump component value as text
mkTypeInfo : function<void> - function to make TypeInfo for the component type
gc : function<(src:array<uint8>):lambda<void>> - function to perform GC marking on the component value
- Component
Single ECS component. Contains component name, data, and data layout.
- Fields:
name : string - name of the component
hash : ComponentHash - hash of the component
stride : int - stride of the component data
data : array<uint8> - raw data of the component
info : CTypeInfo - type information of the component
gc_dummy : lambda<void> - this is here so that GC can find real representation of data
- EntityId
- Fields:
id : uint - Unique identifier of the entity. Consists of id (index in the data array) and generation.
generation : int - index of the entity
- Archetype
ECS archetype. Archetype is unique combination of components.
- Fields:
hash : ComponentHash - hash of the archetype (combination of component hashes)
components : array< Component> - list of components in the archetype
size : int - number of entities in the archetype
eidIndex : int - index of the ‘eid’ component in the components array
- ComponentValue
Value of the component during creation or transformation.
- Fields:
name : string - name of the component
info : CTypeInfo - type information of the component
data : float4[4] - raw data of the component
- EcsRequestPos
Location of the ECS request in the code (source file and line number).
- Fields:
file : string - source file
line : uint - line number
- EcsRequest
Individual ECS requests. Contains list of required components, list of components which are required to be absent. Caches list of archetypes, which match the request.
- Fields:
hash : ComponentHash - hash of the request
req : array<string> - required components
reqn : array<string> - required components which are not present
archetypes : array<int> - sorted list of matching archetypes
at : EcsRequestPos - location of the request in the code
- DecsState
Entire state of the ECS system. Contains archetypes, entities and entity free-list, entity lookup table, all archetypes and archetype lookups, etc.
- Fields:
archetypeLookup : table< ComponentHash;int> - lookup of archetype by its hash
allArchetypes : array< Archetype> - all archetypes in the system
entityFreeList : array< EntityId> - list of free entity IDs
entityLookup : array<tuple<generation:int;archetype: ComponentHash;index:int>> - lookup of entity by its ID
componentTypeCheck : table<string; CTypeInfo> - lookup of component type info by its name
ecsQueries : array< EcsRequest> - all ECS requests
queryLookup : table< ComponentHash;int> - lookup of ECS request by its hash
- DecsPass
Individual pass of the update of the ECS system. Contains pass name and list of all pass callbacks.
- Fields:
name : string - name of the pass
calls : array< PassFunction> - list of all pass callbacks
8.1.4. Comparison and access
- ComponentMap.(cmp: ComponentMap; name: string): ComponentValue&
Access to component value by name. For example:
create_entity <| @ ( eid, cmp )
cmp.pos := float3(i) // same as cmp |> set("pos",float3(i))
- Arguments:
cmp : ComponentMap
name : string
- EntityId!=(a: EntityId; b: EntityId): bool
Inequality operator for entity IDs.
- EntityId==(a: EntityId; b: EntityId): bool
Equality operator for entity IDs.
8.1.5. Access (get/set/clone)
8.1.5.1. clone
- clone(cv: ComponentValue; val: bool)
Sets individual component value. Verifies that the value is of the correct type.
- Arguments:
cv : ComponentValue
val : bool
- clone(cv: ComponentValue; val: EntityId)
- clone(cv: ComponentValue; val: urange)
- clone(cv: ComponentValue; val: range)
- clone(cv: ComponentValue; val: string)
- clone(cv: ComponentValue; val: urange64)
- clone(cv: ComponentValue; val: int)
- clone(cv: ComponentValue; val: range64)
- clone(cv: ComponentValue; val: int64)
- clone(cv: ComponentValue; val: int16)
- clone(cv: ComponentValue; val: int2)
- clone(cv: ComponentValue; val: int3)
- clone(cv: ComponentValue; val: int4)
- clone(cv: ComponentValue; val: int8)
- clone(cv: ComponentValue; val: uint16)
- clone(cv: ComponentValue; val: uint8)
- clone(cv: ComponentValue; val: uint64)
- clone(cv: ComponentValue; val: uint2)
- clone(cv: ComponentValue; val: uint3)
- clone(cv: ComponentValue; val: float)
- clone(cv: ComponentValue; val: uint4)
- clone(cv: ComponentValue; val: float2)
- clone(cv: ComponentValue; val: float3)
- clone(cv: ComponentValue; val: uint)
- clone(cv: ComponentValue; val: float3x4)
- clone(cv: ComponentValue; val: float3x3)
- clone(cv: ComponentValue; val: float4x4)
- clone(cv: ComponentValue; val: double)
- clone(dst: Component; src: Component)
- clone(cv: ComponentValue; val: float4)
8.1.5.2. get
- get(arch: Archetype; name: string; value: auto(TT)): auto
Creates temporary array of component given specific name and type of component. If component is not found - panic.
- Arguments:
arch : Archetype
name : string
value : auto(TT)
- get(cmp: ComponentMap; name: string; value: auto(TT)): auto
- get_component(eid: EntityId; name: string; defval: auto(TT)): TT
Returns a copy of the named component for the given entity.
If the entity is dead or the component is not found, returns defval.
The type of the component is inferred from the type of defval.
Panics if the component exists but its type does not match.
- Arguments:
eid : EntityId
name : string
defval : auto(TT)
8.1.5.3. has
- has(cmp: ComponentMap; name: string): bool
Returns true if component map has specified component.
- Arguments:
cmp : ComponentMap
name : string
- has(arch: Archetype; name: string): bool
- remove(cmp: ComponentMap; name: string)
Removes specified value from the component map.
- Arguments:
cmp : ComponentMap
name : string
8.1.5.4. set
- set(cmp: ComponentMap; name: string; value: auto(TT)): auto
Set component value specified by name and type. If value already exists, it is overwritten. If already existing value type is not the same - panic.
- Arguments:
cmp : ComponentMap
name : string
value : auto(TT)
- set(cv: ComponentValue; val: auto): auto
8.1.6. Entity status
- entity_count(): int
Returns the total number of alive entities across all archetypes.
- is_alive(eid: EntityId): bool
Returns true if the entity is alive (exists and has not been deleted). An entity is alive when its id is within bounds and its generation matches the lookup table.
- Arguments:
eid : EntityId
8.1.7. Debug and serialization
- debug_dump()
Prints out state of the ECS system.
- describe(info: CTypeInfo): string
Returns textual description of the type.
- Arguments:
info : CTypeInfo
- finalize(cmp: Component)
Deletes component.
- Arguments:
cmp : Component
- serialize(arch: Archive; src: Component)
Serializes component value.
8.1.8. Stages
- commit()
Finishes all deferred actions.
- decs_stage(name: string)
Invokes specific ECS pass. commit is called before and after the invocation.
- Arguments:
name : string
- register_decs_stage_call(name: string; pcall: PassFunction)
Registration of a single pass callback. This is a low-level function, used by decs_boost macros.
- Arguments:
name : string
pcall : PassFunction
8.1.9. Deferred actions
- create_entity(blk: lambda<(eid:EntityId;var cmp:ComponentMap):void>): EntityId
Creates deferred action to create entity.
- Arguments:
blk : lambda<(eid: EntityId;cmp: ComponentMap):void>
- delete_entity(entityid: EntityId)
Creates deferred action to delete entity specified by id.
- Arguments:
entityid : EntityId implicit
- update_entity(entityid: EntityId; blk: lambda<(eid:EntityId;var cmp:ComponentMap):void>)
Creates deferred action to update entity specified by id.
- Arguments:
entityid : EntityId implicit
blk : lambda<(eid: EntityId;cmp: ComponentMap):void>
8.1.10. GC and reset
- after_gc()
Low level callback to be called after the garbage collection. This is a low-level function typically used by live.
- before_gc()
Low level callback to be called before the garbage collection. This is a low-level function typically used by live.
- restart()
Restarts ECS by erasing all deferred actions and entire state.
8.1.11. Iteration
decs_array (atype: auto(TT); src: array<uint8>; capacity: int) : auto
for_each_archetype (var erq: EcsRequest; blk: block<(arch:Archetype):void>)
get_default_ro (arch: Archetype; name: string; value: auto(TT)) : iterator<TT const&>
get_optional (arch: Archetype; name: string; value: auto(TT)?) : iterator<TT?>
get_ro (arch: Archetype; name: string; value: auto(TT)) : array<TT>
get_ro (arch: Archetype; name: string; value: auto(TT)[]) : array<TT[-2]>
- decs_array(atype: auto(TT); src: array<uint8>; capacity: int): auto
Warning
This is unsafe operation.
Low level function returns temporary array of component given specific type of component.
- Arguments:
atype : auto(TT)
src : array<uint8>
capacity : int
8.1.11.1. for_each_archetype
- for_each_archetype(hash: ComponentHash; erq: function<():void>; blk: block<(arch:Archetype):void>)
Invokes block for each entity of each archetype that can be processed by the request. Request is returned by a specified function.
- Arguments:
hash : ComponentHash
erq : function<void>
blk : block<(arch: Archetype):void>
- for_each_archetype(erq: EcsRequest; blk: block<(arch:Archetype):void>)
- for_each_archetype_find(hash: ComponentHash; erq: function<():void>; blk: block<(arch:Archetype):bool>): bool
Invokes block for each entity of each archetype that can be processed by the request. Request is returned by a specified function. If block returns true, iteration is stopped.
- Arguments:
hash : ComponentHash
erq : function<void>
blk : block<(arch: Archetype):bool>
- for_eid_archetype(eid: EntityId; hash: ComponentHash; erq: function<():void>; blk: block<(arch:Archetype;index:int):void>): bool
Invokes block for the specific entity id, given request. Request is returned by a specified function.
- Arguments:
eid : EntityId implicit
hash : ComponentHash
erq : function<void>
blk : block<(arch: Archetype;index:int):void>
- get_default_ro(arch: Archetype; name: string; value: auto(TT)): iterator<TT const&>
Returns const iterator of component given specific name and type of component. If component is not found - iterator will keep returning the specified value.
- Arguments:
arch : Archetype
name : string
value : auto(TT)
- get_optional(arch: Archetype; name: string; value: auto(TT)?): iterator<TT?>
Returns const iterator of component given specific name and type of component. If component is not found - iterator will keep returning default value for the component type.
- Arguments:
arch : Archetype
name : string
value : auto(TT)?
8.1.11.2. get_ro
- get_ro(arch: Archetype; name: string; value: auto(TT)): array<TT>
Returns const temporary array of component given specific name and type of component for regular components.
- Arguments:
arch : Archetype
name : string
value : auto(TT)
- get_ro(arch: Archetype; name: string; value: auto(TT)[]): array<TT[-2]>
8.1.12. Request
- EcsRequestPos(at: LineInfo): EcsRequestPos
Constructs EcsRequestPos from rtti::LineInfo.
- Arguments:
at : LineInfo
- compile_request(erq: EcsRequest)
Compiles ECS request, by creating request hash.
- Arguments:
erq : EcsRequest
- lookup_request(erq: EcsRequest): int
Looks up ECS request in the request cache.
- Arguments:
erq : EcsRequest
- verify_request(erq: EcsRequest): tuple<ok:bool;error:string>
Verifies ECS request. Returns pair of boolean (true for OK) and error message.
- Arguments:
erq : EcsRequest