On this page

← All documentation

A first map script

A map script that animates the playfield with the music: where the file goes next to a chart, what it may change, its budgets, and how replays treat it.

A map script belongs to one map. It moves the playfield while the map plays: it sways, rotates, zooms, shifts columns and changes colours with the music, like an ITG modchart. Judging and scoring never depend on it. A map without a script plays with the skin's playfield.

The script

first-light makes the playfield sway from side to side, leaning into each swing, one swing every two measures. The lanes glow on every beat, brightest on the downbeat.

/// <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" });
    });
  },
});

How it works:

  • export default defineChart({ apiVersion: 2, setup }) declares the script. A map script has no id and no name: it is identified by its map. apiVersion must equal the game's (2).
  • setup(play) receives the same PlayContext as a skin: columns, song (with song.timing, the tempo points), the screen size, the judgement tiers. It runs at every launch of the map, on a fresh copy.
  • playfield.set and playfield.update change the playfield. update takes a patch and can animate it: transitionMs and easing.
  • The handler runs once per beat, not per frame. It only starts transitions: the renderer animates every value on its own from the song clock, so the motion stays smooth whatever the script's pace.
  • Two playfield.update calls in one handler with different transitions stay separate and apply in order: a glow at once, then a fade back by the next beat.

Where it goes

Next to the chart, in the map's folder, beside the audio:

File Serves
script.ts Every difficulty of the folder.
<chart file name>.script.ts That difficulty only. It wins over script.ts.

For Artist - Title [Hard].osu, the specific script is Artist - Title [Hard].script.ts.

The file is read again at every launch of the map: edit, save, start the map. No restart of the game. It must be UTF-8 text of at most 1 MiB. Images it shows are PNG files of its own folder, named in setup or listed in defineChart({ images: [ … ] }) (64 at most), and decoded before the play starts.

The player can switch every map script off with Settings → Game → Allow map scripts (setting allowMapScripts, on by default). The safe mode switches them off too.

What it may change

It may It may not
Call playfield.set, playfield.update and lanes.set: every playfield field, and per lane, with the same bounds as a skin. Call hud.* or storage.*: they throw.
Read the game: game.song(), game.notes(), game.player(), game.judgements(), game.playfield(), game.settings(). Register anything: judgement sets, ratings, tabs, modifiers… Only mods do.
Listen to game.songStart, game.beat, game.tick, game.judgement, game.pause, game.resume, game.songEnd, game.playfieldChange, game.settingsChange. Reach the network, or read a file other than the images of its folder.
Draw natively with stage.*, if it declares permissions: ["stage"]. Change the notes, the judgement or the score.

The script describes a layer over the skin's. Each value it gives wins over the skin's while it gives it. The player's own configuration wins over both. For a lane, the order is: the script's lane, the script's common values, the skin's lane, the skin's common values, the engine's default.

Time is in microseconds in events (beat.timeUs) and in milliseconds in transitions (transitionMs, 0 to 10000). game.beat fires at the exact time of each beat on the song clock and stops during a pause. game.tick fires 30 times a second.

Budgets

A map script is treated like a downloaded mod:

Budget Value
Run time of one call (load, setup, each handler) 30 ms
Memory 16 MiB
Stop 3 timeouts, or 3 errors in a row
Wait at launch for the setup of the skin and the script 1000 ms, then the map plays with the engine's defaults and without the script
Source file 1 MiB
Tempo points in song.timing 1024

A script that is stopped during the play loses its layer: the skin's playfield stays. It comes back at the next launch.

Replays

A replay records mapScript: the BLAKE3 hash (hexadecimal) of the source of the script that drew the play. It is null without a script, or when the script could not start. Judging does not depend on the script, so a replay plays back identically with or without it. Changing one character of the script changes the hash.

Try it

  1. Copy script.ts next to a chart of a map in your library.
  2. Start the map from the game. The playfield sways.
  3. Or run a diagnostic play that skips the menus. Use a throwaway data folder so your own data is untouched:
$env:PRISM_DATA_DIR = "$env:TEMP\prism-scratch"
prism.exe --bench-gameplay 10 --map "C:\path\to\Artist - Title [Hard].osu"

It plays the map as a real launch would and prints a line PRISM_BENCH map script: … with the path, the state (running or not running), the hash and the problems. --smoke-test prints the same under PRISM_SMOKE for the map selected at start. See Tooling.

When something is wrong

The play always goes on, with the skin alone. The error is reported in a notice at launch and in the mods' log.

What is wrong Message
Wrong apiVersion the map script requires modding API version 1; this game provides version 2
No declaration the map script must declare itself with `export default defineChart({ … })` at its top level
Declared twice defineChart must be called exactly once
A mod's call in a map script `hud.text` is not available to map scripts: they only change the playfield
Too big, or not text <file> exceeds 1048576 bytes, <file> is not UTF-8 text
A playfield change outside a play the playfield only changes while a chart plays: call it from a handler between `game.songStart` and `game.songEnd`
A declared image is missing or is not a PNG The script does not start.

Concepts explains when a script is stopped and for how long.

Next

  • Every rule, every field, transitions and images: Map scripts.
  • The playfield fields and their bounds: Skin fields.
  • playfield.update, lanes.set and the game.* reads: SDK reference.

Source in the game repository: content/examples/map-scripts/first-light/script.ts,crates/modding/src/chart.rs,crates/modding/src/manifest.rs,crates/modding/src/api.rs,crates/modding/src/host.rs,apps/desktop/src/app/skins.rs,apps/desktop/src/app/playback.rs,crates/modding/tests/docs_examples_start.rs