On this page

← All documentation

Game state, storage, logging and keys

game.*, playfield.*, lanes.*, log.*, ui.*, storage.* and controls.*.

Functions

controls.register

function controls.register(input: ModActionDeclaration): void;

Declares a key action that appears in the game's Controls screen under the mod's name. Mods only, from setup (otherwise "controls.register is only available in a mod's setup"). id follows the usual id rule (a-z 0-9 -, at most 32 characters; "action id is registered twice"), name at most 64 characters, defaultKey a physical key code of the game's key list such as KeyK or Space ("action defaultKey must be an accepted physical key code, got …"). A mod registers at most 32 actions. The player may rebind it; the binding is stored under <modId>/<id>. Presses arrive as the controls.action event.

controls.register({ id: "mark", name: "Mark this moment", defaultKey: "KeyM" });

game.hits

function game.hits(input: HitQuery): HitList;

The latest real hits (judgements that are not misses), oldest first: offsetMs is input minus target in milliseconds (negative early, positive late) and tier an index into game.judgements().tiers. limit is 1 to 256 (50 by default). player is 0 (the left side, the watched play) or 1 (the right side of a side-by-side replay), otherwise "player must be 0 (left side) or 1 (right side), got N". The history is native and never lost, unlike the game.judgement event; game.tick carries hitCount so a script only queries when it changed. Hold heads and releases are separate hits.

const recent = game.hits({ limit: 20 });
const mean = recent.reduce((sum, hit) => sum + hit.offsetMs, 0) / Math.max(1, recent.length);

game.judgements

function game.judgements(input: void): JudgementConfig | null;

The judgement configuration of the play: preset name and tiers (index, id, name, colour, early/late windows in ms when the set has them, breaksCombo). Returns null outside a play. Tier indexes in game.judgement, game.hits and counts arrays refer to this list; the last tier is the miss.

const config = game.judgements();
const names = config?.tiers.map((tier) => tier.name) ?? [];

game.keys

function game.keys(input: KeyQuery): KeyState;

The state of the keys of the play: per lane whether it is held, how many presses it counted, and the key (KeyD) and gamepad input (pad:a) it is bound to (null when unbound), plus the latest presses with their song time. limit is 0 to 256 (64 by default; 0 returns only the lanes). player as in game.hits. Only lanes 0 to 15 are recorded. play identifies the history: a new one starts with each play, seek or section restart, so a script that sees it change restarts its own counts. total changes with every press. Read-only and native, independent of the event queue: it needs no permission.

const keys = game.keys({ limit: 0 });
const held = keys.lanes.filter((lane) => lane.pressed).length;

game.notes

function game.notes(input: NoteQuery): NotePage;

Reads the chart in pages: notes whose time is in [fromUs, toUs), optionally of one column, sorted by time. limit is 1 to 256 (64 by default; "limit must be in [1, 256], got N"). When more notes remain, next is a cursor to pass as cursor for the following page. Times are song time in microseconds, hold ends in endUs. Readable from game.songStart on.

let cursor: number | null | undefined;
let total = 0;
do {
  const page = game.notes({ fromUs: 0, toUs: 10_000_000, column: 0, cursor });
  total += page.notes.length;
  cursor = page.next;
} while (cursor != null);
log.info(`${total} notes in column 0 during the first 10 s`);

game.player

function game.player(input: void): PlayerState;

A snapshot of the player's progress in the current play: playing, paused, the song time (extrapolated from the last clock event), combo, maxCombo, counts per tier of game.judgements() (miss last), judged (their sum) and accuracy in percent (100 before the first judgement). Cheap enough for a game.tick handler. Outside a play it reports playing: false.

ctx.on("game.tick", () => {
  const player = game.player();
  if (player.combo > 0 && player.combo % 100 === 0) log.info(`combo ${player.combo}`);
});

game.playfield

function game.playfield(input: void): Playfield | null;

Where the playfield is on screen, or null before the first play: keys, one lanes entry per column (x of the centre as a fraction of the screen width, width as a fraction of the screen height), sides (the box of each playfield: one in solo, two in a side-by-side comparison; place per-side elements from it), hitY and spawnY (fractions of the screen height) and scrollTimeUs, the time a note takes from spawn to hit line. It changes with the window and the layout: listen to game.playfieldChange instead of caching it.

const field = game.playfield();
const firstLane = field?.lanes[0];

game.settings

function game.settings(input: void): ModSettings | null;

The player's settings a script may read, or null before the game sent them (it sends them once at startup): volume 0 to 1, showFps, scrollTimeMs, audioOffsetMs, and ratingSystem, the difficulty scale selected in the library (not the active judgement set). Read-only. game.settingsChange fires when one changes.

const settings = game.settings();
if (settings?.showFps) log.info("FPS display is on");

game.song

function game.song(input: void): Song | null;

Returns the Song being played (title, artist, difficulty, keys, duration, ratings, BPM range, timing of at most 1024 points), or null before the first play. Available to every kind of script from setup on; at load it throws "game.song is not available while the mod loads". It is set by game.songStart.

const song = game.song();
if (song) log.info(`${song.artist} - ${song.title} (${song.keys}K)`);

lanes.set

function lanes.set(input: LaneUpdate): void;

Sets the look of one lane: column (from 0) and lane, the per-lane fields (sprites, offsets, key and stage lights…), merged over earlier calls for that column. In setup it is part of the starting state; during a play colours and offsets move to the new value over transitionMs (0 to 10000; instant when absent) with easing. Same availability and errors as playfield.update (skins and map scripts, setup or a handler during a play).

lanes.set({ column: 0, lane: { noteOffsetY: 0.01 }, transitionMs: 200 });

log.info

function log.info(input: string): void;

Writes an informational line to the Mods page under the script's id. Works in every phase, including while the file loads, and for every kind of script. Text over 1024 characters is cut and ends with …. The message queue holds 256 lines; further lines are dropped. Use it instead of console.log, which does not reach the Mods page.

log.info(`loaded ${mod.id}`);

log.warn

function log.warn(input: string): void;

Same as log.info with the level of a warning, shown highlighted on the Mods page. Use it for a recoverable problem the player should notice, not for progress notes.

log.warn("the chart has no timing point; beat effects are off");

playfield.set

function playfield.set(input: PlayfieldSpec): void;

Sets playfield properties for a skin or a map script (mods get "playfield.set is only available to skins and map scripts"). Call it in setup: fields are merged over what earlier calls set, so calls can be split. During a play it applies at once, with no transition (use playfield.update to animate); outside setup and a play it throws "playfield.set only works in the skin's setup and in its handlers during a play". Values are validated against the field bounds (x, y in 0 to 1 of the screen, lanes at most 32 entries…); an invalid value throws with the field name. A live image must be one the package loaded at launch (declared in images).

playfield.set({ x: 0.5, scroll: "up", zoom: 1.1 });

playfield.setNoteMask

function playfield.setNoteMask(input: NoteMask): void;

Sets how the playfield is drawn for the play that is about to start: a cover on the spawn side of the notes' region (sudden), a cover on the receptor side of every lane (hidden) and a lift of the receptor line (lift), all in percent of the height of the lanes' box. Only the mods that declared a noteMask modifier call it, and only inside their playfield.noteMask handler; elsewhere it throws playfield.setNoteMask is only available inside a `playfield.noteMask` handler.

Every part is optional, and an absent part is drawn as the skin draws it. sudden and hidden are { size, feather }: size from 10 to 100, feather from 0 to 50 (the part of the cover's far edge that fades out). lift goes from 0 (not raised) to 50 and never carries the receptor line past the spawn edge. Numbers are rounded to the nearest whole percent. A number outside its range, NaN or infinite, an unknown field or a wrong type throws in your script with the reason; the first refusal drops the whole modification of that mod even when the script catches the exception, and the play is drawn without it with a notice. With several calls in one handler the last valid one wins. Two mods of one play cannot set the same part: the later one in load order is dropped with a notice.

The numbers are copied once, when the play starts, into what the renderer draws and the replay records (chartMods.visual); nothing of your script runs while the play is drawn, and a mask never changes the notes or the judging, so the play stays ranked.

ctx.on("playfield.noteMask", (request) => {
  if (request.id !== "curtain") return;
  playfield.setNoteMask({ sudden: { size: request.params.height, feather: 10 } });
});

See Setting the playfield mask.

playfield.update

function playfield.update(input: PlayfieldUpdate): void;

Changes the playfield during a play: patch fields replace the current ones; numbers and colours move there over transitionMs (0 to 10000, instant when absent) with easing (linear by default, easeIn, easeOut, easeInOut); images switch at once, and each must be one the skin loaded at launch. Skins and map scripts only, from a handler between game.songStart and game.songEnd: otherwise "the playfield only changes while a chart plays: call it from a handler between game.songStart and game.songEnd". In setup it merges like playfield.set. Changes are queued and applied at the end of the step.

ctx.on("game.beat", (beat) => {
  if (beat.meterBeat === 0) {
    playfield.update({ patch: { zoom: 1.05 }, transitionMs: 120, easing: "easeOut" });
  }
});

storage.clear

function storage.clear(input: void): void;

Deletes every key of this script's storage. Same availability as storage.get. It cannot be undone.

storage.clear();

storage.get

function storage.get(input: string): unknown | null;

Reads a value saved by this script, or null when the key is absent. Mods and skins only, after load (a map script gets "storage.get is not available to map scripts: they only change the playfield"). The file is mod-storage/<id>.json (skins: mod-storage/skins/<id>.json); it survives restarts and uninstalling the mod keeps it. A corrupt or oversized file makes setup fail with "storage <path>: …".

const best = storage.get("best");
const bestScore = typeof best === "number" ? best : 0;

storage.keys

function storage.keys(input: void): string[];

Lists the saved keys in alphabetical order. Same availability as storage.get.

const saved: string[] = storage.keys();

storage.remove

function storage.remove(input: string): void;

Deletes a key. Removing an absent key does nothing. Same availability as storage.get.

storage.remove("history");

storage.set

function storage.set(input: StorageEntry): void;

Saves a JSON value under a key (one argument: { key, value }). Keys have 1 to 128 characters ("storage keys must have 1 to 128 characters"); at most 256 keys ("the storage holds at most 256 keys"); at most 262144 bytes (256 KiB) of key bytes plus JSON text ("the storage holds at most 262144 bytes; this value would bring it to N"). Replacing a key counts only the difference. Writes reach the disk at most once per second and when the mod stops. Same availability as storage.get.

storage.set({ key: "best", value: 98.4 });
storage.set({ key: "history", value: [{ at: 1, accuracy: 98.4 }] });

ui.dismiss

function ui.dismiss(input: void): void;

Closes the mod's own popup. Does nothing when none is open. Mods only ("only mods dismiss menu popups").

ui.dismiss();

ui.popup

function ui.popup(input: PopupDefinition): void;

Shows (or updates) the mod's popup on the game's menu screens: a title, a detail line, a progress bar (done of total) and up to four action buttons. A mod has one popup; calling again with other content replaces it, and the same content does nothing. Mods only ("only mods display menu popups"); at most eight mods may have a popup open at once ("at most eight mod popups may be open"). Bounds: title 1 to 96 characters, detail at most 512, done ≤ total ≤ 10 000 000, at most 4 actions, each with an id of 1 to 32 characters from a-z 0-9 _ (not dismiss, which is the player's own close button) and a label of 1 to 64 characters. Breaking them throws "popup title, detail, progress or actions exceed their bounds" or "invalid or duplicate popup action". A click arrives as ui.action.

ui.popup({
  title: "Rating backfill",
  detail: "Computing ratings",
  done: 120,
  total: 800,
  actions: [{ id: "cancel", label: "Cancel" }],
});

Types

Definitions read from the SDK. A type used here that is defined on another page is listed under Defined elsewhere.

type ModActionDeclaration = {
  id: string;
  name: string;
  defaultKey: string;
};

type HitQuery = { limit?: number | null; player?: number | null; };

type HitList = Hit[];

type Hit = { offsetMs: number; tier: number; };

type KeyQuery = { limit?: number | null; player?: number | null; };

type KeyState = {
  play: number;
  total: number;
  lanes: KeyLane[];
  presses: KeyPress[];
};

type KeyLane = {
  column: number;
  pressed: boolean;
  count: number;
  key?: string | null;
  pad?: string | null;
};

type KeyPress = { column: number; timeUs: number; };

type NoteQuery = {
  fromUs: number;
  toUs: number;
  column?: number | null;
  cursor?: number | null;
  limit?: number | null;
};

type NotePage = { notes: Note[]; next?: number | null; };

type Note = {
  index: number;
  column: number;
  timeUs: number;
  endUs?: number | null;
};

type PlayerState = {
  playing: boolean;
  paused: boolean;
  timeUs: number;
  combo: number;
  maxCombo: number;
  counts: number[];
  judged: number;
  accuracy: number;
};

type LaneUpdate = {
  column: number;
  lane: LaneSpec;
  transitionMs?: number | null;
  easing?: Easing | null;
};

type LaneSpec = {
  note?: string | null;
  receptor?: string | null;
  receptorPressed?: string | null;
  holdBody?: string | null;
  holdEnd?: string | null;
  stageLight?: string | null;
  stageLightColor?: string | null;
  stageLightSize?: SpriteSize | null;
  stageLightOffsetY?: number | null;
  stageLightOn?: boolean | null;
  stageLightOpacity?: number | null;
  stageLightScale?: number | null;
  stageLightFadeMs?: number | null;
  keyLightOn?: boolean | null;
  keyLightOpacity?: number | null;
  keyLightScale?: number | null;
  keyLightFadeMs?: number | null;
  noteOffsetY?: number | null;
  receptorOffsetX?: number | null;
  receptorOffsetY?: number | null;
  holdEndOffset?: number | null;
  holdMatchNoteWidth?: boolean | null;
  holdWidthScale?: number | null;
  holdEndSize?: SpriteSize | null;
  holdEndScale?: number | null;
  holdEndFlip?: boolean | null;
  holdEndRound?: boolean | null;
  holdEndFade?: number | null;
  holdEndFadeStart?: number | null;
  holdEndFadeMs?: number | null;
  holdEndHiddenMs?: number | null;
  noteTintStrength?: number | null;
  receptorTintStrength?: number | null;
  laneTintStrength?: number | null;
  lightTintStrength?: number | null;
  holdMissedStyle?: HoldMissedStyle | null;
  holdMissedColor?: string | null;
  holdMissedOpacity?: number | null;
  laneImage?: string | null;
  laneColor?: string | null;
  noteColor?: string | null;
  receptorColor?: string | null;
  pressedColor?: string | null;
  holdBodyColor?: string | null;
  holdEndColor?: string | null;
  offsetX?: number | null;
  offsetY?: number | null;
  width?: number | null;
  noteSize?: SpriteSize | null;
  receptorSize?: SpriteSize | null;
  hitSound?: string | null;
};

type SpriteSize = { width: number; height: number; };

type HoldMissedStyle = "hide" | "tint";

type Easing = "linear" | "easeIn" | "easeOut" | "easeInOut" | "easeInCubic" | "easeOutCubic" | "easeInOutCubic" | "easeOutExpo" | "easeOutBounce";

type PlayfieldSpec = {
  x?: number | null;
  y?: number | null;
  anchor?: PlayfieldAnchor | null;
  rotation?: number | null;
  zoom?: number | null;
  laneWidth?: number | null;
  width?: number | null;
  laneGap?: number | null;
  laneHeight?: number | null;
  receptorY?: number | null;
  scroll?: ScrollDirection | null;
  noteSize?: SpriteSize | null;
  receptorSize?: SpriteSize | null;
  holdWidth?: number | null;
  holdEndSize?: SpriteSize | null;
  holdEndOffset?: number | null;
  holdMatchNoteWidth?: boolean | null;
  holdWidthScale?: number | null;
  holdEndScale?: number | null;
  holdEndFlip?: boolean | null;
  holdEndRound?: boolean | null;
  holdEndFade?: number | null;
  holdEndFadeStart?: number | null;
  holdEndFadeMs?: number | null;
  holdEndHiddenMs?: number | null;
  noteOffsetY?: number | null;
  receptorOffsetX?: number | null;
  receptorOffsetY?: number | null;
  stageLightSize?: SpriteSize | null;
  stageLightOffsetY?: number | null;
  hitLight?: string | null;
  hitLightOn?: HitLightOn | null;
  hitLightFollowJudgement?: boolean | null;
  stageLightOn?: boolean | null;
  stageLightOpacity?: number | null;
  stageLightScale?: number | null;
  stageLightFadeMs?: number | null;
  keyLightOn?: boolean | null;
  keyLightOpacity?: number | null;
  keyLightScale?: number | null;
  keyLightFadeMs?: number | null;
  hitLightOpacity?: number | null;
  hitLightScale?: number | null;
  holdLightOpacity?: number | null;
  holdLightScale?: number | null;
  hitLightFrames?: number | null;
  hitLightFps?: number | null;
  hitLightSize?: SpriteSize | null;
  hitLightOffsetY?: number | null;
  hitLightColor?: string | null;
  holdLight?: string | null;
  holdLightFrames?: number | null;
  holdLightFps?: number | null;
  holdLightSize?: SpriteSize | null;
  holdLightOffsetY?: number | null;
  holdLightColor?: string | null;
  mineLight?: string | null;
  mineLightFrames?: number | null;
  mineLightFps?: number | null;
  mineLightSize?: SpriteSize | null;
  mineLightOffsetY?: number | null;
  mineLightColor?: string | null;
  mineLightOpacity?: number | null;
  mineLightScale?: number | null;
  textureFilter?: TextureFilter | null;
  noteTintStrength?: number | null;
  receptorTintStrength?: number | null;
  laneTintStrength?: number | null;
  lightTintStrength?: number | null;
  holdMissedStyle?: HoldMissedStyle | null;
  holdMissedColor?: string | null;
  holdMissedOpacity?: number | null;
  laneCover?: boolean | null;
  laneCoverColor?: string | null;
  laneCoverOpacity?: number | null;
  laneCoverSize?: number | null;
  laneCoverFeather?: number | null;
  noteSnapColors?: boolean | null;
  noteSnapMax?: NoteSnapMax | null;
  noteSnapStrength?: number | null;
  noteSnapTintMode?: NoteSnapTintMode | null;
  noteSnapEnd?: NoteSnapEnd | null;
  noteSnapColor1?: string | null;
  noteSnapColor2?: string | null;
  noteSnapColor3?: string | null;
  noteSnapColor4?: string | null;
  noteSnapColor6?: string | null;
  noteSnapColor8?: string | null;
  noteSnapColor12?: string | null;
  noteSnapColor16?: string | null;
  noteSnapColor24?: string | null;
  noteSnapColor32?: string | null;
  noteSnapOther?: string | null;
  measureLines?: boolean | null;
  measureLineColor?: string | null;
  measureLineOpacity?: number | null;
  measureLineThickness?: number | null;
  measureLineLength?: MeasureLineLength | null;
  measureLineOvershoot?: number | null;
  measureLineEvery?: number | null;
  beatLines?: boolean | null;
  beatLineColor?: string | null;
  beatLineOpacity?: number | null;
  beatLineThickness?: number | null;
  borderWidth?: number | null;
  borderColor?: string | null;
  judgementLine?: boolean | null;
  judgementLineColor?: string | null;
  judgementLineThickness?: number | null;
  judgementLineOffsetY?: number | null;
  judgementLineAt?: JudgementLineAt | null;
  judgementWindowThickness?: number | null;
  judgementWindowOpacity?: number | null;
  judgementWindowDashed?: boolean | null;
  backgroundColor?: string | null;
  backgroundImage?: string | null;
  backgroundTint?: string | null;
  mine?: string | null;
  mineColor?: string | null;
  laneColor?: string | null;
  noteColor?: string | null;
  receptorColor?: string | null;
  pressedColor?: string | null;
  holdBodyColor?: string | null;
  holdEndColor?: string | null;
  note?: string | null;
  receptor?: string | null;
  receptorPressed?: string | null;
  holdBody?: string | null;
  holdEnd?: string | null;
  laneImage?: string | null;
  stageLight?: string | null;
  stageLightColor?: string | null;
  hitSound?: string | null;
  lanes?: LaneSpec[] | null;
};

type PlayfieldAnchor = "topLeft" | "top" | "topRight" | "left" | "center" | "right" | "bottomLeft" | "bottom" | "bottomRight";

type ScrollDirection = "down" | "up";

type HitLightOn = "all" | "off";

type TextureFilter = "linear" | "nearest";

type NoteSnapMax = "1/1" | "1/2" | "1/3" | "1/4" | "1/6" | "1/8" | "1/12" | "1/16" | "1/24" | "1/32";

type NoteSnapTintMode = "multiply" | "replace";

type NoteSnapEnd = "head" | "own" | "off";

type MeasureLineLength = "playfield" | "lanes";

type JudgementLineAt = "center" | "top" | "bottom";

type NoteMask = {
  sudden?: NoteMaskCover | null;
  hidden?: NoteMaskCover | null;
  lift?: number | null;
};

type NoteMaskCover = { size: number; feather: number; };

type PlayfieldUpdate = {
  patch: PlayfieldSpec;
  transitionMs?: number | null;
  easing?: Easing | null;
};

type StorageEntry = { key: string; value: unknown; };

type PopupDefinition = {
  title: string;
  detail: string;
  done: number;
  total: number;
  actions?: PopupAction[];
};

type PopupAction = { id: string; label: string; };

Defined elsewhere

JudgementConfig (declarations), TierInfo (declarations), Playfield (declarations), Lane (declarations), PlayfieldSide (declarations), ModSettings (declarations), Song (declarations), BpmRange (declarations), TimingPoint (declarations)

Source in the game repository: content/mods/sdk/modding.d.ts,content/dev-src/notes/sdk/