Skip to main content
The ECS (entity component system) holds the state of your game. It comes from @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

Import EcsLibrary 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 a name field equal to its class name:
The registry files each component under its 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.
Don’t give a component class the name of another class in your game’s bundle. When two classes share a name, the bundler renames one (Position2), and the results of getZipper are then keyed by that new name: destructuring ({Position}) gives undefined. Three.js and Konva export many classes (Sphere, Plane, Box3, Text, Image, Line…), so prefer names that are clearly yours, such as Hitbox or PlayerSprite.
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:
The objects in the result are your component instances: changing them changes the game state. To know which entity a result belongs to, use getIndexedZipper. Each object also has an id, which entityFromIndex turns back into an entity:
Other registry methods: 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 by nf 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.