Modules

Docs / API / Modules / menu

Menu

Admin & chat

@s2script/sdk/menu

interactive player menus (chat + center backends).

8 exports

Import

import { MenuStyle } from "@s2script/sdk/menu";

Author-time types ship in the npm package; the engine injects the runtime at plugin load. Add @s2script/sdk to your plugin's dependencies.

API reference

Generated from the shipped type definitions.

enum
MenuStyle

The display backend a Menu renders through — the value of Menu.style.

Chat = "chat"
Chat-log backend: numbered lines printed to chat, picked by typing the number.
Center = "center"
CS2 center-screen HTML backend: W/S move the cursor, E selects; re-painted each tick.
enum
MenuCancelReason

Why a menu closed without a selection — the MenuCancelEvent.reason.

Exit = 0
The player chose the Exit control (or close/cancel was called).
Timeout = 1
The display seconds elapsed with no selection.
Disconnect = 2
The player left the server.
NewMenu = 3
A newer menu was shown to the same slot, superseding this one.
interface
MenuSelectEvent

Payload for Menu.onSelect — the item a player picked.

readonly slot: number
0-based player slot that made the selection.
readonly item: number
0-based index of the chosen item within the menu's item list.
readonly info: string
The info string the item was added with — the machine key, returned to your handler.
readonly display: string
The visible label the item was added with.
interface
MenuCancelEvent

Payload for Menu.onCancel — a menu that closed without a selection.

readonly slot: number
0-based player slot whose menu closed.
readonly reason: MenuCancelReason
Why it closed (see MenuCancelReason).
class
Menu

A slot-based interactive menu. Add items, wire Menu.onSelect/Menu.onCancel, then Menu.display to a player slot. The model paginates automatically (7 items per chat page) and picks a renderer by Menu.style, falling back to Chat when the style's renderer is unregistered. Only one menu is active per slot; displaying a new one supersedes the old (MenuCancelReason.NewMenu).

import { Menu, MenuStyle } from "@s2script/sdk/menu";
const m = new Menu("s2script Menu Demo");
m.style = MenuStyle.Center;
m.freezePlayer = true;                 // freeze movement while the WASD menu is open (nav still works)
m.addItem("hp", "Heal to 100");
m.addItem("noclip", "Toggle Noclip");
m.onSelect(e => { console.log(`picked ${e.info} (slot ${e.slot})`); });
m.display(slot, 30);
title: string
Heading shown above the item list.
style: MenuStyle
Which registered renderer to use. Falls back to Chat if the style's renderer is unregistered.
exitButton: boolean
Append an auto Exit control (default true).
freezePlayer: boolean
When true, a renderer that supports it (the CS2 center renderer) freezes the player's movement while the menu is open and restores it on close — buttons still register, so WASD nav still works. The chat renderer ignores it. Default false (movement allowed, the normal behavior).
addItem(info: string, display: string, opts?: { disabled?: boolean }): void
(info, display) — info is returned to onSelect; display is shown.
onSelect(handler: (e: MenuSelectEvent) => void): void
Register the callback fired when a player picks an item. Replaces any previous handler.
onCancel(handler: (e: MenuCancelEvent) => void): void
Register the callback fired when the menu closes without a selection (see MenuCancelReason). Replaces any previous handler.
display(slot: number, seconds?: number): void
Show to a 0-based slot for seconds (0 = until selection/cancel/disconnect).
close(slot: number): void
Close an open menu for slot early.
registerRenderer(name: string, renderer: MenuRenderer): void
Register a display backend under a MenuStyle value (used by the CS2 center renderer).
interface
MenuSession

A live display of one menu to one slot, passed to a MenuRenderer. Owns the page/cursor state and exposes the input verbs a renderer drives.

readonly slot: number
0-based player slot this session is displayed to.
view(): { title: string; lines: MenuLine[]; page: number; pageCount: number; exit: boolean }
The resolved snapshot the renderer paints: heading, per-line state, and 0-based page position.
pickNumber(n: number): void
Chat idiom: apply a number-key press against the current view (1..7 select, 8=Back, 9=Next, 0=Exit).
moveUp(): void
Center idiom: move the cursor up over the current page's nav targets (items + Back/Next/Exit), wrapping.
moveDown(): void
Center idiom: move the cursor down over the current page's nav targets, wrapping.
confirm(): void
Center idiom: activate the current cursor target (select an item, paginate, or exit).
cancel(): void
Close this session with an MenuCancelReason.Exit.
interface
MenuLine

One rendered line of a MenuSession view.

text: string
The line's visible text.
key: string | null
The chat number key ("1".."0") that picks this line, or null for an unselectable/disabled line.
selectable: boolean
True if this line is a selectable item.
cursor?: boolean
True if the center cursor is currently on this line (the renderer highlights it).
control?: string
For a control line, which control it is: "back", "next", or "exit" (absent on item lines).
index?: number
For an item line, its 0-based index into the menu's item list (absent on control lines).
interface
MenuRenderer

A display backend for Menu — registered via Menu.registerRenderer, driven with a MenuSession.

open(session: MenuSession): void
Show the session for the first time (the menu just opened).
update(session: MenuSession): void
Re-paint the session after a page/cursor change.
close(slot: number): void
Tear down the display for slot (the menu closed).
Back to modules All packages

s2script — Source 2 plugin framework

GitHub