Getting Started
Inspired by Twine SugarCube, Sugarbox is a lightweight1, headless, framework-agnostic library for building web-based interactive fiction.
Features
Section titled “Features”- Passages and navigation
- State with snapshot-based history
- Saving and loading with versioning, optional compression, and migrations
- Deterministic pseudorandom number generation (PRNG)
- Custom class serialization for richer game objects
- Achievements and settings stored separately from save slots
- Headless by design (you bring the renderer and UI)
Installation
Section titled “Installation”This library is uploaded to npm at https://www.npmjs.com/package/sugarbox
npm install sugarboxpnpm install sugarboxyarn install sugarboxbun install sugarboxSample usage
Section titled “Sample usage”Notes on the current API:
- Engine initialization uses
vars(notvariables) - Passage content lives on
passage.data(notpassage.passage)
import { SugarboxEngine } from "sugarbox";
const startPassage = { name: "Start", data: "You wake up in a quiet room.", tags: ["intro"],};
const otherPassages = [ { name: "Hallway", data: "A long corridor stretches ahead." },];
const engine = await SugarboxEngine.init({ name: "Test", vars: { playerName: "Dave", inventory: { gold: 123, gems: 12 } }, startPassage, otherPassages, // Optionally: config, classes, achievements, settings, migrations});
engine.setVars((state) => { state.playerName = "Sheep"; state.inventory.gems = 21;});
engine.navigateTo("Hallway");Footnotes
Section titled “Footnotes”-
Less than 5KB after minification and gzip compression ↩