@nilvn/engine
v0.19.0
Published
NilVN — a tiny DOM-based ADV / visual-novel engine with story-like script syntax and a capability-sandboxed plugin host
Maintainers
Readme
@nilvn/engine
A small visual-novel (ADV / galgame) engine for the browser. Scripts read like a
story — yuki: Hi! plus a few bracket commands — and everything expressive
(screen shake, text effects, sprite animation, choice styling, camera moves) is a
plugin you can write in plain JavaScript. The engine is the mechanism: parser,
stage, audio, saves, languages, the finished game's shell (title and ending
pages, the in-game menu with saves / backlog / auto / skip / settings, themeable
without CSS) and a capability-sandboxed plugin host. The first-party plugins live in @nilvn/plugins
and are written against the same public surface as yours. TypeScript, DOM + CSS
rendering, no framework; the only runtime dependency is a TOML parser that the
single-file build drops.
pnpm add @nilvn/engine @nilvn/pluginsimport { createEngine } from '@nilvn/engine'
import { withFirstParty } from '@nilvn/plugins'
const engine = createEngine({ ...withFirstParty(), container: document.getElementById('app')! })
await engine.loadConfig('./nilvn.config.toml') // plugins, actors, title / ending pages, menu, theme
await engine.showTitle() // the title page; New game plays the config's entry scriptstart() plays at once and resolves when the story ends; a host without a title
page calls it instead. From an empty folder to a deployed game:
Getting started.
The content language defaults to en; a multi-language game passes lang,
defaultLang, languages and per-language catalogs (see the engine API).
A script:
[use textfx screenfx charfx]
[actor yuki name=Yuki color=#ff7eb6 sprites=assets/char/yuki-{face}.svg]
[bg assets/bg/street.svg fade=1.5]
An evening street. Shadows stretch long.
[char yuki happy]
yuki: You came! {wave:I've been waiting~}
yuki(angry): You're late again!
[shake strength=14 duration=0.5]
[choice Apologize -> apologize]
[choice Make an excuse -> excuse if=courage>=2]
[label apologize]
me: I'm sorry...
[end]The player clicks, or presses Space / Enter, to advance; [keys] in the
config rebinds the shell's keyboard (auto, skip, quick save / load, …).
Three ways to load a game
| | Call | Good for |
|---|---|---|
| Config + script | loadConfig('./nilvn.config.toml') then start() | Hand-written games: one TOML file declares plugins, actors, path aliases, macros and defaults; the script stays pure story. |
| Script only | load('./story.nvn') or loadSource(text) | Quick embeds; the script declares what it needs with [use] / [actor] / [alias]. |
| Script package | load('./my-game/'), load(zipBytes), load(inlinePackage(payload)) | Works exported by NilVN Studio or packed with npx nilvn pack: nilvn.json plus chunked scripts, per-language text and assets — streamed on demand, switchable language, saves. |
npx nilvn pack turns a config-driven work into a package — a folder to
upload, a .nvs zip, or one double-clickable .html — and reports what would
fail in play before anyone plays it
(script-package.md).
npx nilvn playtest then plays it — explore runs to its endings, or scripted
routes — in a headless Chrome with real clicks and keys (playtest.md).
Documentation
| | |
|---|---|
| Getting started | An empty folder to a deployed game with a title page, endings, saves and settings; npm-user vs contributor commands. |
| Script syntax | Lines, inline markup, @key text references, variables and conditions, macros, actors, plugins, paths. |
| Config file | Every section of nilvn.config.toml. |
| Command reference | Built-in commands and object ids (the first-party plugins' commands are in @nilvn/plugins). |
| Engine API | createEngine options, loading, playing, saves, languages, diagnostics, audio, plugins, stage, packages, the IIFE build. |
| In-game chrome | The built-in title / ending pages, the system menu (saves, backlog, auto / skip, settings), what [title] / [ending.<id>] / [menu] / [settings] / [saves] configure. |
| Theming | The --nilvn-* token contract: dialogue box, name tag, choices, panels; [theme] / [window] in the config, setTheme(), the [theme] command. |
| Several files, events, transitions | [game] scripts / [include] / [call], clicks that run script commands (panels, hotspots, sprites), [trans …] and rule masks. |
| Panels and layered sprites | Declarative HUD / windows ([ui.<id>]), [actors.<id>.layers], [choices], [input], [persist], [preload]; the config schema (CONFIG_SCHEMA / checkConfig). |
| Script packages | nilvn pack, the nilvn.json format, the chunk manifest, custom loaders. |
| Playtesting | nilvn playtest: explore runs and scripted routes in a headless Chrome, with real input; failures, unreachable objects and diagnostics reported. |
| Writing plugins | The plugin package format, permissions and capability objects, lifecycle — in @nilvn/plugin-sdk. |
What the engine guarantees
- Content never crashes the host. A bad line, an unknown command, a missing
plugin, a broken jump target or a chunk that fails to load is reported as a
diagnostic (
engine.diagnostics,onError) and play degrades around it. - Plugins are sandboxed by capability. A plugin declares permissions and receives capability objects for exactly those; it never sees the engine or the DOM, and everything it registers is released when it is deactivated. Plugins can be enabled, disabled and reloaded while a game runs.
- Saves are stable. A save addresses the script by label and offset and carries the stage, variables, audio and language; incompatible saves are refused rather than misapplied.
- Text is localizable at runtime. Scripts reference text by key, catalogs ship per language, and the language can switch mid-line without losing state.
Plugins
The engine registers nothing by itself. A host hands it plugins three ways:
createEngine({ registry, manifests })— modules made available to[use id](or the short name of anapp.nilvn.*id), each with its manifest;@nilvn/plugins'withFirstParty()is exactly this pair for the first-party set.createEngine({ plugins })— the host's own modules, activated at once.[use ./x/plugin.json]/[use ./x.js]in the script — fetched at play time.
The first-party set (textfx, screenfx, charfx, objectfx, spriteanim,
choicefx, voicefx, animstudio, abreplay) is documented in
@nilvn/plugins; writing your own
starts at @nilvn/plugin-sdk.
Two builds
- ESM (
import … from '@nilvn/engine',dist/index.js+.d.ts) for bundled apps. - IIFE (
@nilvn/engine/iife,dist/nilvn-engine.iife.js): one self-contained script that defines a globalADVwith the same exports, for a<script>tag. It has no dependencies, soloadConfig(the TOML parser) is ESM-only. It carries no plugins; the batteries-included bundle that exported games run is@nilvn/plugins/iife.
Working in the repository
pnpm build # dist/: ESM + .d.ts + the bare IIFE
pnpm typecheckTests live in test/ and run with pnpm test from the repository root;
test/iife-smoke.test.ts boots the minified bundle. The demo game lives with
the first-party plugins in @nilvn/plugins.
License
MIT.
