Skip to main content

NanoForge


npm versionnpm downloadsTests statusLast commitContributors

About

@nanoforge-dev/ecs is NanoForge’s built-in entity-component-system library, backed by a WebAssembly registry (compiled from C++). It ships two subpath entry points — @nanoforge-dev/ecs/client and @nanoforge-dev/ecs/server — each loading its own WASM build (browser-targeted vs Node-targeted) behind the same EcsLibrary API, so a client bundle never pulls in the server build or vice versa. The root entry point, @nanoforge-dev/ecs, only exposes the shared types (Registry, Component, System, Entity, SparseArray, Context, EcsContextApi) and pulls in no WASM binary, so it’s safe to import from code that targets both client and server.

Installation

Node.js 26 or newer is required.

Example usage

Register EcsLibrary from the entry point matching your app:
Shared types like Registry, Component, and System are available from the root entry point, regardless of which platform you’re targeting:
Once registered, Context.ecs.registry gives every other library access to the entity/component registry:
Register systems to run on every tick:
When registered alongside @nanoforge-dev/editor’s EditorLibrary, EcsLibrary also listens for a "hot-reload" command from the editor and applies incoming components via registry.addComponent — useful for live-editing entity state from an external editor host.

Scenes

@nanoforge-dev/ecs/scene has EcsScene, a scene of @nanoforge-dev/scene whose entities and systems go away when it is unloaded. Write its setup like an app’s main:
The registry setup gets is the app’s, tracked: at unload, the systems added through it are removed (by function) and the entities spawned through it are killed, after calling the destroy() of their components that have one. teardown(registry, ctx) runs first, if the scene has one.

Contributing

Before creating an issue, please ensure that it hasn’t already been reported/suggested, and double-check the documentation.
See the contribution guide if you’d like to submit a PR.

Help

If you don’t understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don’t hesitate to ask questions in discussions.

Modules