Skip to main content

NanoForge


npm versionnpm downloadsTests statusLast commitContributors

About

@nanoforge-dev/scene is NanoForge’s scene manager. Scenes are classes that set up part of the game when they are loaded (entities, systems…) and remove it when they are unloaded. They form a tree, and the manager keeps vars that scenes and systems share. It is exposed on Context.scenes.

Installation

Node.js 26 or newer is required.

Example usage

A scene extends Scene, and names the scene it is loaded under with a static parent. Each load creates a new instance: keep what the scene creates in its fields and remove it in unload. The scene module knows nothing of the ECS: the scene calls it.
Register SceneLibrary with the scene to start with:
Change scene from anywhere that has the context, such as a system:

Moving between scenes

Only one chain of scenes is loaded at a time, from a root scene to the current one. Loading a scene unloads the loaded scenes up to the deepest one it shares with the new scene’s parents, then loads its missing parents and the scene itself: Scenes are unloaded deepest first and loaded root first. Only the scene asked for gets the params; the parents loaded along with it get undefined. parent loads a scene under another parent for that load, or under the current scene with "current". Changes are queued, and made when no system runs: before the systems of the next tick, or right after the systems of the tick they were asked in. load and unload return a promise resolved once the change is made. When a scene’s hook throws, the promise is rejected and the error stops the tick, like a system’s. When the game stops, the scenes are dropped without calling unload: the other libraries are cleared by then.

Vars

ctx.scenes.vars holds values shared by scenes and systems:
A var belongs to the scene that made it (with init, or set on a missing key): the scene being loaded or unloaded at the time, or else the current scene. It is removed when that scene is unloaded, unless it is persistent. Type your vars by augmenting SceneVars:

Scene ids

Give SceneLibrary every scene class of the game, by id:
The keys are the scenes’ ids (SceneLoadContext.id, and the names the editor uses). A bundler can rename a class, never these keys. A scene missing from scenes is known by its class name.

Entities and systems

The scene module does not depend on the ECS. EcsScene (@nanoforge-dev/ecs/scene) is a scene whose setup spawns entities and adds systems, removed when it is unloaded.

In the editor

When the NanoForge editor runs the game (@nanoforge-dev/editor-lib), the library sends a scenes event (the loaded scenes, the vars and the known ids) while the editor asks for it, and loads, unloads and sets vars on its scenes-load, scenes-unload and scenes-set-var commands.

Documenting scenes for the editor

Document a scene like a component or a system, with a TSDoc comment:
  • @scene marks the class as a scene;
  • @side is client, server or shared;
  • @vars lists the vars it inits or reads.
Its parent is read from static parent, and its params from its type argument (Scene<{ seed: number }>): document their fields with TSDoc.

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.

Classes

Interfaces

Type Aliases