On this page

← All documentation

Concepts

How scripts run in Prism: the three kinds, the lifecycle, threads and budgets, events, permissions, errors, determinism, units, ids and what is forbidden.

Read this page once before you write more than a toy. It explains the rules that every other page assumes.

The three kinds of script

Mods, skins and map scripts run on the same engine with the same budgets. They differ in who owns them, when they run and what they may call.

Mod Skin Map script
Entry file main.ts or index.ts in a folder of a mods root skin.ts in a skin folder script.ts or <chart>.script.ts next to a chart
Declares itself with defineMod defineSkin defineChart
Has an id Yes Yes (same namespace as mods) No: it belongs to its map
Runs Loaded when the mod host starts, then lives until it stops At every map launch, on a fresh copy At every map launch, after the skin, on a fresh copy
Listens to events (ctx.on) Yes Yes Yes
Reads the game (game.*) Yes Yes Yes
playfield.*, lanes.set No: `playfield.set` is only available to skins and map scripts Yes, in setup and during a play Yes, in setup and during a play
playfield.setNoteMask Yes, inside the playfield.noteMask handler of a noteMask modifier, once per play No No
hud.*, storage.* Yes Yes No: `hud.text` is not available to map scripts: they only change the playfield
Provides and configures HUD elements (provides, uses, ctx.element) Yes Yes No: ctx.has is always false, and configure throws
*.register (judgements, scroll speed, ratings, library filters, tabs, key actions, gameplay modifiers, download sources and bridges, tables) Yes, in setup only No No
leaderboard.*, ratings.backfill, metron.performance, ui.popup, skinImport.* Yes No No
stage.* With permissions: ["stage"] With permissions: ["stage"] With permissions: ["stage"]
When it fails for good Off until the mod host starts again Off until the next map launch Off until the next map launch

The rule behind the table: a mod adds to the game for every play. A skin describes the look of the playfield for every map. A map script describes one map, and only changes the playfield.

Lifecycle

A mod goes through these steps.

  1. Discovery. The mod host starts once the interface has sent the saved settings, and loads in the background: the game does not wait for it. It reads the game's mods folder first, then the player's, each in folder-name order. A folder is a mod if it holds main.ts or index.ts and its name does not start with .. Folders named editor-… are never loaded.
  2. Load. The top-level code of the entry file (and of what it imports) runs. Only defineMod, log.* and ctx.on work. Anything else throws `X` is not available while the mod loads: call it from `setup` or an event handler. That includes storage.* and game.*. This is also how installing a .pvmod reads its declaration: nothing else runs.
  3. Declaration. defineMod must be called exactly once, at the top level. The game validates the id, the version, the API version, the texts, the fonts and the dependencies. An invalid declaration, a taken id or a second call fails the mod, even when the exception is caught.
  4. Setup. setup runs once, after the setup of every package the mod uses. This is the only place for *.register calls: they throw `X.register` is only available in a mod's setup anywhere else, and the registries close once every mod has set up.
  5. Events. From then on the mod receives events, and may call everything except defineMod.
  6. Stop. The mod stops when the host restarts (Reload, a skin change, a restart of the game), when the player turns it off, or when the game disables it after repeated failures.

The order of mods is: providers before the mods that use them, otherwise loadOrder (0 to 10000, default 1000), then id. That order is also the order of event delivery and of the stacking of HUD layers.

A skin and a map script have two steps instead of six. At every map launch the game loads skin.ts fresh, runs setup(play), then loads the map script fresh and runs its setup(play). Then it validates and decodes everything the two described (playfield.set, lanes.set, images). playfield.* and lanes.set work in setup, and in handlers between game.songStart and game.songEnd. Everywhere else they throw the playfield only changes while a chart plays: call it from a handler between `game.songStart` and `game.songEnd`.

The game waits at most 1000 ms for the two setups. After that the map plays with the engine's defaults and without the script, and says so.

Threads and budgets

All script code runs on one thread, named mods, never on the input, render, audio or window thread. By default the thread is pinned to the last logical core. It has an 8 MiB stack. Game threads never wait for it: they push events into a bounded queue.

The shipped mods share one engine, because the game's author wrote them. Every mod the player installs gets an engine of its own, so one cannot starve another. Each script keeps its own globals either way.

Limit Value
Run time of one load, setup, event or tick 30 ms
Memory per script 16 MiB
Stack per script 1 MiB
Failures in a row that disable a script 3 (an exception or a timeout)
Timeouts in all that disable a script 3
HUD nodes per mod 384
Characters in one HUD text 256
Storage per mod 256 KiB
Event queue 1024 events
Message queue (what the Mods page shows) 256 messages
Length of a log line 1024 characters, cut with …
game.tick 30 per second, during a play

Limits has the full table, down to every per-call bound. The budget is wall-clock time, not CPU time. A handler that needs 25 ms on a quiet machine will fail on a busy one: keep handlers short.

Events

An event is a message from the game. A script listens with ctx.on("<name>", handler) and stops with ctx.off. Handlers run in registration order. There are 25 events in four families:

Family Events
The play game.songStart, game.songEnd, game.judgement, game.pause, game.resume, game.tick, game.beat, game.pauseMenu, game.lobbyLive, game.playfieldChange, game.settingsChange
For something you registered or provide chart.transform, controls.action, elements.configure
Answers to a request you made leaderboard.result, metron.performanceResult, ratings.progress, ui.action, skinImport.action, skinImport.created, skinImport.opened, skinImport.staged
The library library.ready, library.chartAdd, library.chartsAdded

Events has the payload of each.

Delivery is built so that the game never waits for a script:

  • The game puts each event into a bounded queue of 1024. If the queue is full, the new event is dropped and counted. Nothing blocks.
  • A script that falls behind therefore loses events instead of slowing the play. Do not write a handler that assumes it saw every event.
  • Payloads carry running totals for that reason. game.judgement has combo, counts and accuracy after that note, so a script resynchronises on the next one. game.tick carries hitCount, counted natively and independent of delivery.
  • Histories are read on demand, not accumulated from events: game.hits(…) and game.keys(…) return what happened, bounded.
  • game.tick arrives 30 times a second, only while a chart plays and is not paused. A late tick is skipped. game.beat arrives at the exact time of each beat on the song clock, without waiting for a tick.
  • The entities of a play (song, chart, judgements, playfield) are built before the play starts and shared read-only, so they are readable from game.songStart on.
  • Events reach scripts in the order skin, map script, mods.

Permissions

Three permissions exist. A script names them in permissions: [ … ] of its declaration.

Permission What it allows Who may declare it How the player removes it
stage stage.*: native drawing over the playfield, from typed elements, with no shader. Mods, skins, map scripts A switch on the mod's row in the Mods page. Without it the calls throw `X` needs the `stage` permission: declare `permissions: ["stage"]`, and when the player removed it `X`: the player turned off native drawing for `id`.
network downloads.register and downloads.registerBridge: declare where maps can be downloaded from. The game makes every request. Mods A switch on the mod's row. The sources it declared leave the list at once.
skinImport skinImport.*: converting a skin the player picks. Mods Turning the mod off. Without the permission: `skinImport` needs the `skinImport` permission: declare `permissions: ["skinImport"]`.

A mod installed from the catalogue starts turned off, so the player sees what it asks before it runs. Nothing else needs a permission: HUD nodes, storage, reading the game, registering sets are always allowed to a mod.

Errors

A script error never crashes the game. What happens depends on when it happens and on the kind.

When A mod A skin or a map script
The declaration is invalid, or the top-level code throws The mod does not load. State Failed, with the reason. Other mods load. The script does not run. The skin falls back to the engine's defaults; the play goes on with the skin alone for a map script.
setup throws, or overruns 30 ms The mod does not load. Same.
A handler throws, or overruns 30 ms An error message. The script stays on. Same.
3 failures in a row, or 3 timeouts in all Disabled for the rest of the session: the HUD is removed, its judgement sets, ratings, actions and modifiers are withdrawn, its storage is written. It comes back after Reload or a restart. Disabled until the next map launch: the HUD, or the map script's layer, goes away and the playfield stays as the others describe it.

The messages are exceeded the 30ms execution budget N times and failed 3 times in a row; last error: …. A mod that declares itself wrongly keeps its first error even if the script catches the exception. Messages show on the Mods page under the id of the script, and a notice at launch reports a skin or a map script that did not run.

What a host call throws is a normal JavaScript exception. You may catch it: try { … } catch (error) { log.warn(String(error)); }. A caught exception is not counted as a failure. Only an exception that leaves the handler is.

Determinism

  • The result of a play is native. Judging, scoring and replays are computed by the game from the inputs. A script influences them only through declarations the game validates: a judgement set, a gameplay modifier, a chart transform. A map script cannot change a result at all.
  • A replay does not run scripts. It records the hash of the map script that drew the play (mapScript). It replays identically with or without the script.
  • Scripts are not deterministic. Date, performance.now() and Math.random() exist and are not seeded. Do not draw anything that must be the same twice with them.
  • A transform is seeded. chart.getNotes() returns a seed, derived from the play, and the editor's input.random() is seeded too: use those when a pattern must repeat. Chart transform explains.

Units

What Unit
Times in events and entities (timeUs, offsetUs, durationUs, startUs) Microseconds of song time. Negative during a pre-roll.
Durations of animations and transitions (transitionMs, durationMs, …FadeMs), settings (scrollTimeMs, audioOffsetMs) Milliseconds. The name ends in Ms.
Lengths: sizes, widths, gaps, radii, font sizes Fractions of the screen height. At 1080p, 0.05 is 54 pixels. The shape of a node therefore does not depend on the aspect ratio.
Positions x, y (HUD layout, playfield) Fractions of the parent: the width for x, the height for y. The screen at the top level.
Angles Degrees, clockwise.
Colours Hex text: #rgb, #rgba, #rrggbb, #rrggbbaa. Colour options of an element also accept theme:<role>, which follows the player's theme.

A name tells you the unit: Us, Ms. An option that is neither is a fraction.

Ids

There are several ids, with different rules.

What Rule
Mod id, skin id 1 to 64 characters among a-z, 0-9, ., -, _. Starts and ends with a letter or a digit. No ... Usable as a Windows file name. Shared by mods and the skin: the first one loaded keeps it, the next fails with another mod already uses the id `X`: the folder `Y` loaded first and keeps it. No prefix is reserved: the shipped mods use pvng..
Skin folder name 1 to 64 characters, a valid Windows file name, not starting with .. It is the name of the skin on this machine.
What a mod registers: judgement set, tier, scroll speed system, rating, library filter, tab, key action, gameplay modifier Stricter: 1 to 32 characters among a-z, 0-9 and -. Other packages name it <mod id>/<id>.
HUD element name (provides) 1 to 32 characters among a-z, 0-9, -, starting with a letter. Others configure it as "<mod id>/<element>".
HUD node id 1 to 64 characters, no control characters. Unique inside the mod.
Storage key 1 to 128 characters.

A judgement set id and the id of one of its tiers are in the strict group, so com.example.set is a valid mod id but not a valid set id.

What is forbidden

A script runs in a sandbox. It is meant to be safe to install from a stranger.

  • No DOM, no web view. A script never touches the interface's page, its JavaScript or its IPC. It describes typed nodes; the overlay draws them as text and shapes. No HTML, no raw CSS, no script.
  • No network. fetch and sockets do not exist. A mod with the network permission only declares data (hosts, URL templates, JSON paths) and the game makes the requests, on hosts it shows to the player.
  • No files. A script reads only the files of its own folder (fonts, PNG images), served by the game after validation, and its own storage. It cannot read or write anywhere else.
  • No timers. setTimeout and setInterval exist as imports of manivelle:env, but the host never runs them: their callbacks never fire. Use game.tick, game.beat or any event. async handlers are allowed, but nothing makes them wait.
  • No imports from outside the folder, no import() call (dynamic import() is not supported; use a static import), and a skin imports nothing at all.
  • No console. It writes to the game's standard error, not to the Mods page. Use log.info and log.warn.
  • No hot reload. ctx.hot is declared in the SDK, but the game does not call it: Reload restarts everything. See Tooling.
  • No shaders and no free drawing. Native effects are typed elements with bounded properties (stage.*).

These limits are the point of the design: they are what lets the game run code a player downloaded.

Source in the game repository: crates/modding/src/api.rs,crates/modding/src/host.rs,crates/modding/src/runtime.rs,crates/modding/src/manifest.rs,crates/modding/src/events.rs,crates/modding/src/judgement.rs,crates/skin/src/color.rs,content/mods/sdk/modding.d.ts