Hi Sam!
Thanks for the question! I’m in the process of updating our documentation to clarify this feature along with several others. I’ve included a preview of the upcoming docs below, which should help answer your questions.
Creating an Entity
The following example shows how to create a new entity without any components:
import * as ecs from '@8thwall/ecs'
const eid = world.createEntity()
Deleting an Entity
The following example shows how to delete an existing entity given its id:
import * as ecs from '@8thwall/ecs'
world.deleteEntity(eid)
Adding Components to an Entity
The following example shows how to add a built-in component to an entity at runtime.
const box = world.createEntity()
ecs.BoxGeometry.set(world, box, {
width: 1,
height: 1,
depth: 1,
})
ecs.Material.set(world, box, {
r: 255,
g: 255,
b: 255,
})
Create and Modify Relationships
The following examples show how you can use build-in helper methods to create or change relationships between entities.
// Entities are automatically a child of World.
const foo = world.createEntity()
const bar = world.createEntity()
// Set foo to be a child of bar.
world.setParent(foo, bar)
// Get the parent of bar. (returns an eid where <= 0 is undefined)
world.getParent(bar)
// Get the children of foo. (returns an array of child eids)
world.getChildren(foo)
Adding Custom Components to an Entity
The following example shows how to add a custom Component to an existing entity.
Example
Make sure you export the Component before attempting to import or use it anywhere.
import * as ecs from '@8thwall/ecs'
const CustomComponent = ecs.registerComponent({
name: 'custom-component',
schema: {
foo: ecs.string
},
schemaDefaults: {
},
data: {
},
add: (world, component) => {
},
tick: (world, component) => {
},
remove: (world, component) => {
},
})
export {CustomComponent}
import CustomComponent from './custom-component' // The name of the file without the file type.
const demo = world.createEntity()
CustomComponent.set(world, demo, {
foo: 'bar'
})