@nanoforge-dev/ecs, a registry written in C++ and compiled to WebAssembly.
- An entity is an id. It has no data of its own.
- A component is a data object attached to an entity: a position, a velocity, a health value.
- A system is a function called on every tick. It finds the entities that have some components and updates them.
Register the library
ImportEcsLibrary from the entry point of your side of the game:
ecs.registry throws before app.init has resolved. The types Registry, Entity, Component and System come from the same entry point.
Components
A component is an instance of a class with aname field equal to its class name:
name. When you add a component it reads name on the instance; when you query, it reads name on the class you pass. The two must match.
An entity has at most one component of each type: adding a second Position replaces the first.
Entities
killEntity removes every component of the entity.
Systems
A system has the type(registry, ctx) => void. Add it with registry.addSystem(system). Systems run on every tick, in the order you added them, unless the game is paused.
registry.getZipper([A, B]) returns one object per entity that has every listed component, keyed by component name:
getIndexedZipper. Each object also has an id, which entityFromIndex turns back into an entity:
To add and remove a group of entities and systems together, such as a level, use a scene: see Scenes.
Errors
An error thrown in the C++ registry reaches JavaScript as a runtime error, with less detail than a JavaScript error. For example,entityFromIndex throws for an id that was never spawned or was killed: check your ids before calling it.
Editor manifests
Projects generated bynf new export an EDITOR_COMPONENT_MANIFEST next to each component and an EDITOR_SYSTEM_MANIFEST next to each system. The NanoForge editor reads them to show and generate your components and systems. The types are EditorComponentManifest and EditorSystemManifest. To generate a new component or system with its manifest, see the CLI component and system schematics.