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 Entity Component System (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

decs::ComponentHash = uint64

Hash value of the ECS component type

decs::TypeHash = uint64

Hash value of the individual type

decs::DeferEval = lambda<(var act:DeferAction):void>

Lambda which holds deferred action. Typically creation of destruction of an entity.

decs::ComponentMap = array<ComponentValue>

Table of component values for individual entity.

decs::PassFunction = function<void>

One of the callbacks which form individual pass.

8.1.2. Constants

decs::INVALID_ENTITY_ID = struct<decs::EntityId>

Entity ID which represents invalid entity.

8.1.3. Structures

decs::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

decs::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

decs::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

decs::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

decs::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

decs::EcsRequestPos

Location of the ECS request in the code (source file and line number).

Fields
  • file : string - source file

  • line : uint - line number

decs::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

decs::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

decs::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
decs::EntityId!=(a: EntityId; b: EntityId) : bool()

Inequality operator for entity IDs.

Arguments
decs::EntityId==(a: EntityId; b: EntityId) : bool()

Equality operator for entity IDs.

Arguments

8.1.5. Access (get/set/clone)

8.1.5.1. clone

decs::clone(cv: ComponentValue; val: bool)

Sets individual component value. Verifies that the value is of the correct type.

Arguments
decs::clone(cv: ComponentValue; val: EntityId)
decs::clone(cv: ComponentValue; val: urange)
decs::clone(cv: ComponentValue; val: range)
decs::clone(cv: ComponentValue; val: string)
decs::clone(cv: ComponentValue; val: urange64)
decs::clone(cv: ComponentValue; val: int)
decs::clone(cv: ComponentValue; val: range64)
decs::clone(cv: ComponentValue; val: int64)
decs::clone(cv: ComponentValue; val: int16)
decs::clone(cv: ComponentValue; val: int2)
decs::clone(cv: ComponentValue; val: int3)
decs::clone(cv: ComponentValue; val: int4)
decs::clone(cv: ComponentValue; val: int8)
decs::clone(cv: ComponentValue; val: uint16)
decs::clone(cv: ComponentValue; val: uint8)
decs::clone(cv: ComponentValue; val: uint64)
decs::clone(cv: ComponentValue; val: uint2)
decs::clone(cv: ComponentValue; val: uint3)
decs::clone(cv: ComponentValue; val: float)
decs::clone(cv: ComponentValue; val: uint4)
decs::clone(cv: ComponentValue; val: float2)
decs::clone(cv: ComponentValue; val: float3)
decs::clone(cv: ComponentValue; val: uint)
decs::clone(cv: ComponentValue; val: float3x4)
decs::clone(cv: ComponentValue; val: float3x3)
decs::clone(cv: ComponentValue; val: float4x4)
decs::clone(cv: ComponentValue; val: double)
decs::clone(dst: Component; src: Component)
decs::clone(cv: ComponentValue; val: float4)

8.1.5.2. get

decs::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)

decs::get(cmp: ComponentMap; name: string; value: auto(TT)) : auto()

decs::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

decs::has(cmp: ComponentMap; name: string) : bool()

Returns true if component map has specified component.

Arguments
decs::has(arch: Archetype; name: string) : bool()

decs::remove(cmp: ComponentMap; name: string)

Removes specified value from the component map.

Arguments

8.1.5.4. set

decs::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
decs::set(cv: ComponentValue; val: auto) : auto()

8.1.6. Entity status

decs::entity_count() : int()

Returns the total number of alive entities across all archetypes.

decs::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

8.1.7. Debug and serialization

decs::debug_dump()

Prints out state of the ECS system.

decs::describe(info: CTypeInfo) : string()

Returns textual description of the type.

Arguments
decs::finalize(cmp: Component)

Deletes component.

Arguments
decs::serialize(arch: Archive; src: Component)

Serializes component value.

Arguments

8.1.8. Stages

decs::commit()

Finishes all deferred actions.

decs::decs_stage(name: string)

Invokes specific ECS pass. commit is called before and after the invocation.

Arguments
  • name : string

decs::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

8.1.9. Deferred actions

decs::create_entity(blk: lambda<(eid:EntityId;var cmp:ComponentMap):void>) : EntityId()

Creates deferred action to create entity.

Arguments
decs::delete_entity(entityid: EntityId)

Creates deferred action to delete entity specified by id.

Arguments
decs::update_entity(entityid: EntityId; blk: lambda<(eid:EntityId;var cmp:ComponentMap):void>)

Creates deferred action to update entity specified by id.

Arguments

8.1.10. GC and reset

decs::after_gc()

Low level callback to be called after the garbage collection. This is a low-level function typically used by live.

decs::before_gc()

Low level callback to be called before the garbage collection. This is a low-level function typically used by live.

decs::restart()

Restarts ECS by erasing all deferred actions and entire state.

8.1.11. Iteration

decs::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

decs::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
decs::for_each_archetype(erq: EcsRequest; blk: block<(arch:Archetype):void>)

decs::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
decs::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
decs::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)

decs::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

decs::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)

decs::get_ro(arch: Archetype; name: string; value: auto(TT)[]) : array<TT[-2]>()

8.1.12. Request

decs::EcsRequestPos(at: LineInfo) : EcsRequestPos()

Constructs EcsRequestPos from rtti::LineInfo.

Arguments
decs::compile_request(erq: EcsRequest)

Compiles ECS request, by creating request hash.

Arguments
decs::lookup_request(erq: EcsRequest) : int()

Looks up ECS request in the request cache.

Arguments
decs::verify_request(erq: EcsRequest) : tuple<ok:bool;error:string>()

Verifies ECS request. Returns pair of boolean (true for OK) and error message.

Arguments