A map script is a TypeScript file that belongs to one map. While the map plays, it moves the playfield, turns it, scales it, shifts its lanes and changes its colors and images to the rhythm of the song, like the modcharts of ITG. A map without a script keeps the playfield of the skin.
A map script is not a mod, which the player installs for every play, and not a skin, which describes the base look of the playfield. It is a third layer on top of the skin. Everything it does is visual: judging and score never depend on it, and a replay records only its fingerprint, see replays.
A complete script
first-light sways the playfield from one side to the other, leaning into each swing, one swing every two measures. The lanes glow on every beat, the downbeat brightest. Every move is a transition the game animates from the song clock; the script runs once per beat.
/// <reference path="../../../mods/sdk/modding.d.ts" />
// First light, an example map script: the playfield sways from side to side, leaning
// into each swing, one swing every two measures, and the lanes glow on every beat, the
// downbeat brightest. Copy it next to a chart as `script.ts` (every difficulty of the
// folder) or as `<chart file name>.script.ts` (that difficulty only).
//
// Every move is a transition the game animates itself from the song clock: the script
// only runs once per beat.
/** Swing to each side, in fractions of the screen width. */
const SWAY = 0.1;
/** Lean at the end of a swing, in degrees clockwise. */
const LEAN = 7;
/** Measures one swing lasts. */
const SWING_MEASURES = 2;
/** Lane color between beats, and its glow on the downbeat and on the other beats. */
const LANE = "#454d61";
const DOWNBEAT = "#a67cff";
const BEAT = "#7486b8";
/** Meter of the timing point in effect at `timeUs`; 4 before the first one. */
function meterAt(timing: TimingPoint[], timeUs: number): number {
let meter = 4;
for (const point of timing) {
if (point.timeUs > timeUs) break;
meter = point.meter;
}
return meter;
}
export default defineChart({
apiVersion: 2,
setup(play: PlayContext) {
playfield.set({ laneColor: LANE });
let side = 1;
let downbeats = 0;
ctx.on("game.beat", (beat: Beat) => {
const beatMs = 60000 / beat.bpm;
const downbeat = beat.meterBeat === 0;
if (downbeat && downbeats++ % SWING_MEASURES === 0) {
side = -side;
const swingMs = SWING_MEASURES * meterAt(play.song.timing, beat.timeUs) * beatMs;
playfield.update({
patch: { x: 0.5 + side * SWAY, rotation: side * LEAN },
transitionMs: Math.min(10000, swingMs),
easing: "easeInOut",
});
}
// A glow at once, then back by the next beat: two paces, two transitions.
playfield.update({ patch: { laneColor: downbeat ? DOWNBEAT : BEAT } });
playfield.update({ patch: { laneColor: LANE }, transitionMs: 0.9 * beatMs, easing: "easeIn" });
});
},
});
A second script, lane-wave, bobs the lanes in opposite directions on every beat and flashes an image behind them on every downbeat. It ships with its image, next to it:
/// <reference path="../../../mods/sdk/modding.d.ts" />
// Lane wave, an example map script: on every beat the lanes bob up and down, neighbours in
// opposite directions, and a soft light (`flash.png`, a PNG next to the script) rises behind the
// lanes on each downbeat and fades over two beats. Copy this folder next to a chart (the PNG
// goes with the script).
//
// `flash.png` is named in `setup`, so it is loaded before the play; a handler could only use
// an image that `images` declares as well.
/** How far a lane moves, in screen heights. */
const BOB = 0.02;
const LIGHT = "#ffffff70";
const DARK = "#ffffff00";
export default defineChart({
apiVersion: 2,
images: ["flash.png"],
setup(play: PlayContext) {
playfield.set({ backgroundImage: "flash.png", backgroundColor: DARK });
ctx.on("game.beat", (beat: Beat) => {
const beatMs = 60000 / beat.bpm;
for (let column = 0; column < play.columns; column++) {
const up = (beat.index + column) % 2 === 0;
lanes.set({ column, lane: { offsetY: up ? -BOB : BOB }, transitionMs: 0.9 * beatMs, easing: "easeInOut" });
}
if (beat.meterBeat === 0) {
playfield.update({ patch: { backgroundColor: LIGHT } });
playfield.update({ patch: { backgroundColor: DARK }, transitionMs: 2 * beatMs, easing: "easeOut" });
}
});
},
});
To try either, copy the script into the folder of a map, named script.ts, and play. No restart and no build: the file is read at every launch.
Files
In the folder of a map, next to its charts and its audio:
| File | Used for |
|---|---|
script.ts |
Every difficulty of the folder. |
<chart file name>.script.ts |
That difficulty only. It wins over script.ts: Artist - Title [Hard].script.ts next to Artist - Title [Hard].osu. |
The file is read again at every map launch. It must be UTF-8 text of at most 1 MiB; otherwise the launch reports <file> exceeds 1048576 bytes or <file> is not UTF-8 text. Its images are the PNG files of the same folder.
The player can turn every map script off: Settings, Game, Allow map scripts (allowMapScripts, on by default, enforced in Rust). When it is off, no script is read and the map plays with the skin alone. Safe mode also never runs a map script.
defineChart
declare function defineChart(input: ChartDefinition): ChartManifest;
| Field | Rule |
|---|---|
apiVersion |
Required. Must be the game's, 2: the map script requires modding API version X; this game provides version 2. |
permissions |
Optional. ["stage"] for native drawing. |
images |
Optional. PNG files of the map folder that handlers put on the playfield during the play, at most 64, besides those setup names. |
setup |
Optional. setup(play: PlayContext). |
A map script has no id: it is the map's. Call defineChart exactly once, at the top level of the file, as the default export. Calling it twice, or inside a function, throws defineChart must be called exactly once or defineChart can only be called at the top level of the map script, and a file that never calls it fails with the map script must declare itself with `export default defineChart({ … })` at its top level.
setup receives the same PlayContext as the setup of a skin: mode, layout, number of columns, window size, the song, the judgement tiers. Name its parameter play, not ctx, which would hide ctx.on.
The script runs in the same engine as the skin and the mods, on the mods thread, loaded fresh at every launch, after the skin. It can use:
playfield.set,playfield.updateandlanes.set: the calls of skins, with the same fields, bounds and transitions (see the skin guide and the field reference);game.song()(withtiming, the tempo points),game.notes(…),game.player(),game.judgements(),game.playfield(),game.settings(), andlog.info/log.warn;- the game events, with
ctx.on:game.songStart,game.beat,game.tick,game.judgement,game.pause,game.resume,game.songEnd,game.playfieldChange,game.settingsChange(the events reference lists them all).
It has no HUD and no storage: hud.* and storage.* throw `<fn>` is not available to map scripts: they only change the playfield. It has no network and no file but the images of its folder, decoded before the play. Calling playfield.set from a mod throws `playfield.set` is only available to skins and map scripts.
With permissions: ["stage"] the script also draws in the native renderer with stage.* (see native drawing): the elements placed on the playfield follow its moves, rotations and zooms. Its sprites and emitters can only name images declared in defineChart({ images }). Without the permission every stage.* call throws. Turning Allow map scripts off also removes this drawing, since the script is not read.
What the script changes
The script describes one layer of the playfield, drawn over the skin's. Every value it gives wins over the skin's for as long as it gives it; the others stay the skin's. For one lane, the chain is: the lane of the script (lanes[i]), the script's common values, the lane of the skin, the skin's common values, then the engine's default. A laneWidth given by the script wins over the skin's total width.
Above both sits the player's own configuration, which can override the script too. The three layers, their order and their different bounds are in the skin guide.
Every playfield field is available. Three serve effects best:
| Field | Bounds | Default | Does |
|---|---|---|---|
rotation |
−3600 to 3600 | 0 | Rotates the whole playfield around the centre of the lanes box, in degrees clockwise. The notes follow their lanes. |
zoom |
0 to 4 | 1 | Scales the whole playfield around the same centre. |
lanes[i].offsetX |
−2 to 2 | 0 | Shifts one lane (background, receptor, notes, holds) sideways, in screen heights, to the right when positive. offsetY does the same vertically. |
The renderer applies them in the shader: each quad turns around its own centre, placed where the rotation and scale of the playfield put it. The game.playfield() entity that mods read stays the layout of the launch: lane shifts included, rotation and zoom not.
Transitions
playfield.update({ patch, transitionMs, easing }) and lanes.set({ column, lane, transitionMs, easing }) change values at once or over time: transitionMs is 0 to 10000 and easing is linear, easeIn, easeOut or easeInOut. A value outside 0 to 10000 throws transitionMs: <v> is outside [0, 10000].
The render thread animates each value separately, from the song clock: a 100 ms flash of color does not cut off a 2 s sway started before, whether the two come from the script or from the skin. A pause freezes the transitions.
Changes of one handler (one step of the mods thread) with the same transition are merged; those with different transitions stay apart and apply in order. So you can light a color and bring it back in the same handler:
playfield.update({ patch: { laneColor: "#8a5cf6" } });
playfield.update({ patch: { laneColor: "#454d61" }, transitionMs: 200, easing: "easeOut" });
Colors and numbers animate. Images, the anchor and the scroll direction change at once.
Tempo and time
play.song.timing(andgame.song()?.timing): the chart's tempo points, sorted,{ timeUs, bpm, beatUs, meter }, at most 1024.game.beat:{ index, timeUs, bpm, meterBeat }on every beat, at its exact time on the song clock, not rounded to ticks.indexcounts beats from the first tempo point,meterBeatis 0 on the first beat of a measure. Beats only arrive while the clock advances; after a pause, the next one resumes.game.tick: the song time, at the tick rate of themodsthread (30 Hz).
A good script starts transitions on beats and lets the renderer animate them, so that it runs only a few times per second. Times are in microseconds (timeUs) except where a field says milliseconds.
Images
The images of a script are PNG files of its folder: those setup names (backgroundImage: "flash.png") and those defineChart({ images }) declares. All are decoded before the play, with the limits of skins: 4 MiB, 2048 px per side, reduced to 640 px, at most 256 images for the layer. A declared image that is missing or not a PNG makes the script fail. During the play, a change that names an image that was not loaded throws. An image of the script that does not fit in the atlas leaves the skin's image in its place.
Budgets and errors
A map script is treated like a downloaded mod: 30 ms of execution per call (loading, setup, each handler), 16 MiB of QuickJS memory, and it is disabled after 3 overruns or 3 errors in a row.
| What happens | Result |
|---|---|
Unreadable file, defineChart missing or invalid, apiVersion different, declared image missing, setup throws or is over budget |
The error is reported (launch banner, mods log) and the map plays with the skin alone. |
| The script is disabled during the play | Its layer disappears and the skin's playfield stays. |
Value out of bounds or invalid in setup |
Brought into the bounds or replaced by the skin's value, and reported, as for a skin. |
| The same mistake in a handler during the play | The call throws in your script (<field>: <v> is outside [min, max], lane N does not exist: there are at most 32 lanes, and the others listed in the skin guide). |
No answer from the mods thread in 1 s |
The map starts with the engine's default look and no script. |
To see a script run as in a real play, use prism.exe --bench-gameplay 10 --map <path to the .osu>: it prints PRISM_BENCH map script: … with the path, the state, the fingerprint and the problems. prism.exe --smoke-test does the same, as PRISM_SMOKE map script: …, for the map selected at startup.
Replays
A replay records mapScript: the BLAKE3 fingerprint (hexadecimal) of the source of the script that drew the play, null when there was none or it could not start. Judging does not depend on the script, so a replay plays back identically with or without it.
Loading without slowing the game
map launch (main thread)
└─ context of the play; chart allowed if the setting permits
└─ helper thread
the map script: <chart>.script.ts, else script.ts
two queues of changes: skin and script (skin::patch_channel)
prepare_play ──> mod thread: skin.ts loaded fresh, setup(play),
then the script loaded fresh, setup(play);
reply: the two PlayfieldSpec
skin::prepare(skin layer, script layer, mods' stage images):
validation, PNG decoding, one atlas
└─ main thread: the prepared playfield, then the start
render thread: one texture upload before the first frame;
each frame: changes from both layers, transitions, drawing
Nothing is allocated for a frame that has no change.
Pitfalls
- Naming the
setupparameterctx: it hides the globalctx.on. - Moving the playfield on every
game.tick: each call costs against the 30 ms budget. Start a transition on a beat instead. - Using an image in a handler without declaring it in
images: it was not loaded. - Using
hud.*orstorage.*: they throw in a map script. - Expecting your values to be the last word: the player's configuration wins, and a player who set
rotationin the editor keeps it. - Relying on the script for anything but looks: it can be switched off, and then judging and score are the same.
See also
- Writing a skin: the layer below, and the shared calls.
- Field reference: every value a script can set.
- The SDK, game and playfield and events.
- Your first map script.