Skip to content

[v0.4] Add default initializer #173

@soulofmischief

Description

@soulofmischief

bitECS could benefit from a symbol-keyed default initializer. I've implemented one in #172, if it's something you want to include. If not, feel free to close this issue and corresponding PR.

It has the following semantics:

import { $default, Default, addComponent, addEntity, createWorld } from 'bitECS'

const world = createWorld()
const eid = addEntity( world )

const MyComponent = {
  x: [] as number[],
  y: [] as number[],
  [$default]( eid, { x = 1, y = 1 } = {}) {
    MyComponent.x[ eid ] = x
    MyComponent.y[ eid ] = y
  }
}

addComponent( world, eid, MyComponent )
assert( MyComponent.x[ eid ] === 1 )

// or:
addComponent( world, eid, Default( MyComponent, { x: 42 }))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions