Plugin windows
A plugin can open windows on players' screens: a shop, a settings page, a vote, an inventory. The plugin describes the window as a Luau table, and the game builds it in its own style, at any resolution and with the player's menu scale. What the player does in it (a click, a change, a text) comes back to the plugin. No code of the plugin runs in the game for this.
cs.window(p, "shop", {
title = "Shop",
size = { 640, 420 },
body = { kind = "column", gap = 12, children = {
{ kind = "grid", columns = 3, children = {
{ kind = "slot", id = "ak", image = "shop/ak.png", name = "AK-47", price = 2700, key = 1 },
{ kind = "slot", id = "m4", image = "shop/m4.png", name = "M4A1", price = 3100, key = 2 },
} },
{ kind = "row", align = "end", children = {
{ kind = "text", id = "money", text = "$800", grow = 1 },
{ kind = "button", id = "close", text = "Done" },
} },
} },
})
cs.on_window("shop", function(p, ev)
if ev.kind == "click" and ev.id == "close" then
cs.window_close(p, "shop")
elseif ev.kind == "click" then
-- ev.id is "ak" or "m4": buy it, then show the money left
cs.window_set(p, "shop", "money", { text = "$" .. p.money })
end
end)
The functions
| Function | What it does |
|---|---|
cs.window(to, id, window) | Opens the plugin's window id for a player, a list, or everyone (nil). The same id again replaces it in place. |
cs.window_set(to, id, element, changes) | Changes one element by its id, without building the window again ("": the window's title). children replaces what a container holds, rows a table's rows. |
cs.window_update(to, id, changes) | Changes several at once: { money = { text = "$800" }, buy = { enabled = false } }. |
cs.window_close(to, id) | Closes it ("": all of the plugin's windows). Its handler does not hear this close. |
cs.window_open(p, id) | Whether it is open for the player. |
cs.window_values(p, id) | Its inputs' values as the player left them: { [id] = value }, nil when it is not open. |
cs.window_style(name, fields) | Names a style: fields an element with style = name has unless it gives them itself (see "Styles"). |
cs.on_window(id, fn) | The handler: fn(p, ev), with ev = {window, id, kind, value, values}; or a table of them (see "Events"). |
The window
| Field | Default | Meaning |
|---|---|---|
title | none | The title bar's text. |
size | {600, 400} | {w, h} in units of a 1080-line screen (up to 1920 wide); the game scales it to the screen's height and the player's menu scale, and makes it smaller where it would not fit (a narrow screen, a big menu scale). {0, 0}: as large as what it holds. |
anchor | center | Where it sits: top_left, top, top_right, left, center, right, bottom_left, bottom, bottom_right. Wide screens keep it inside a 16:9 area. |
offset | {0, 0} | {x, y} from the anchor, in the same units. |
modal | true | The cursor shows and the game's keys stop while it is open. One modal window at a time: opening another closes the first (its handler hears close). false makes a panel that leaves the game playable (up to 4 per player; a fifth closes the oldest). |
close | true | A close button, and Escape closes it. false: only the plugin closes it. |
close_on_death | false | Closes it when the player dies. |
hud | false | A readout on the HUD (see "HUD windows"): no cursor, no clicks, nothing to close, under the other windows. Up to 8 per player. |
frame | true | false: no box and no title bar, only what it holds. |
back, border, border_width, radius, back_image, back_slice, padding | the game's | Its own look, as an element's (see "Looks"). |
fade | 0 | Seconds it takes to appear when it opens (up to 5). |
body | required | The element the window holds. |
Elements
Every element is a table with a kind. These fields work on all of them:
| Field | Meaning |
|---|---|
id | Its name: events carry it, cs.window_set finds the element by it. Unique in the window, 1 to 32 characters. An element without an id sends no events. |
enabled | false: dimmed, and its events never reach the plugin. |
visible | false: hidden, and no events either. |
color, back | Its text (or bar) colour and its background, #rrggbb or #rrggbbaa. |
size | {w, h}: its smallest size. |
max_size | {w, h}: its largest (0: no limit that way); what is beyond is cut. |
grow | Takes the space left, shared by the grow values. |
tooltip | A text shown when the mouse rests on it. |
style | A style's name (cs.window_style), or a list of up to 8: their fields, later ones over earlier, its own over all. |
border, border_width, radius, back_image, back_slice, hover, pressed, font | Its look (see "Looks"). |
fade | Seconds it takes to appear, when built or shown again (up to 5). |
blink | Blinks with this period in seconds (0.2 to 5, a shorter one is 0.2; 0 stops it). |
place, offset | In a stack: where (an anchor, as the window's) and how far from there. |
send | On a button, slot, clickable container, table, field or textarea: up to 16 inputs' ids whose values its events carry (see "Forms"). |
drag, drop | On a slot, image or container with an id: it may be dragged; it takes what is dragged onto it (see "Drag and drop"). |
| Kind | Its own fields | Events |
|---|---|---|
panel, column | children, gap, padding, align (start, center, end), click (true: the whole of it is clicked, lit under the mouse) | click with click |
row | the same, side by side | click with click |
stack | the same, its children over each other, each filling it, or (place) at a corner or side with its own size: a badge on a picture | click with click |
grid | children, columns (1 to 12), gap, padding: the columns share the width | |
list | children, height, gap, padding: scrolls; horizontal: across | |
table | columns: a list of {title, width, grow, align, color} (up to 32); rows: a list of rows, each a list of cells (a text, a number, or an element); height (scrolls), click_rows, sortable, sorted (the column marked, from 1), descending, selected (the row marked, from 1). Every row counts as an element | click: the row, from 1 (click_rows); sort: the column, from 1 (sortable) |
text | text (up to 1024 bytes), font_size (8 to 72), align, wrap, bold, ellipsis (cut to fit), rich ([b], [i], [color=#rrggbb], [size=8..72] kept; any other [ shown as typed) | |
image | image and the options of cl.draw.image: fit, filter, slice, frames, frame; with a size, its shape is kept when it is made wider (a grid's column). Or avatar: a player's CSID: ID, its profile picture from the account service (a silhouette while it comes, or without one), drawn with fit = "cover" unless given another | |
button | text, image, key (1 to 9: the number key presses it), font_size (8 to 72), flat (true: no box, its text lit under the mouse, as a name in a table that opens something), align (start, center, end: its text; cut with an ellipsis when it does not fit) | click |
slot | An item: image, name, count, price, locked, selected, key | click |
bar | value, max, label, smooth (seconds a change takes to move it there), vertical | |
slider | min, max, step, value, label | change: the number |
number | the same, typed or stepped with its arrows (step 1 unless given) | change: the number |
check | text, value | change: true or false |
toggle | the same, drawn as a switch | change: true or false |
field | value, placeholder, max_length (up to 256), password, live (true: change also as it is typed, 0.3 s after the last key: a search) | submit on Enter, change as it loses focus: the text |
textarea | text of many lines: value, placeholder, max_length (characters, up to 1024; 256 unless given), height, live | change as it loses focus (or typed, live): the text |
color | value (#rrggbb), alpha (true: #rrggbbaa) | change when its picker closes: the colour |
select | options: a list of {id, text} (up to 64, ids unique, no control characters), value: the id chosen. multiple: shown as a list, many chosen, value a list of ids; height | change: the option's id (multiple: the list of ids) |
choice | the same, its options side by side, one of them lit | change: the option's id |
line | vertical: a thin line across, or down | |
tabs | tabs: a list of {title, body}; selected (from 1) | change: the tab, from 1 |
space | (its size alone) |
Pictures are PNG or JPG files of the plugin's content (content/). Those precached in a map_start handler (cs.precache) are downloaded with the map and checked like the rest of it (docs/plugins_api.md, "Content"). A window may also show pictures that were not precached, up to 1 MB each: the player's game asks the server for each one it lacks when a window first shows it, and draws it when it comes. So a window can page through many pictures (a map list with each map's photo) without the players downloading them all; the server sends only pictures that a window showed that player. These are kept with the player's downloads and are not checked by hash, so give a changed picture a new name. A missing one draws nothing and is told once in the player's console.
Events
ev.kind is click, sort, change, submit, drop or close. A close comes when the player closes the window (Escape or its button), when another modal window replaces it, when the player dies with close_on_death, and when the player leaves; never for cs.window_close. A map change closes every window without events (the players connect again).
Only what fits the window as the server sent it reaches the handler: an element that is there, enabled and shown, of a kind that sends that event, with a value in its bounds (a slider between its min and max, rounded to its step; a tab that exists; a field's text within its max_length; a dropped element dragged from one with drag). A player sends at most 30 events a second; more are dropped. A HUD window sends none. The handler can trust these, but a buy must still check the player's money and the rules: the window is only what the player sees.
The handler may be a table instead of a function, by the event's kind and then the element's id ("*": any other):
cs.on_window("tops", {
click = { board = open_row, back = go_back, ["*"] = open_player },
sort = function(p, ev) sort_by(p, ev.value) end,
change = { search = search },
close = forget,
})
Forms: the values a click carries
A button (or a slot, a clickable container, a table, a field's Enter) with send carries the values of those inputs as the player left them, in ev.values. The game sends a field still being typed in first, so the values are the ones on the screen:
cs.window(p, "profile", { title = "Profile", body = { kind = "column", children = {
{ kind = "field", id = "name", value = p.name },
{ kind = "number", id = "age", min = 1, max = 99, value = 20 },
{ kind = "toggle", id = "public", text = "Public", value = true },
{ kind = "select", id = "maps", multiple = true, options = map_options() },
{ kind = "button", id = "save", text = "Save", send = { "name", "age", "public", "maps" } },
} } })
cs.on_window("profile", { click = { save = function(p, ev)
save_profile(p, ev.values.name, ev.values.age, ev.values.public, ev.values.maps)
end } })
Each value was checked as its own change was: a number within its bounds, an option that is there. cs.window_values(p, id) gives all of a window's inputs at any time. An event too long for one command (a long text) goes to the server in pieces and is joined there, up to 8 KB.
Drag and drop
A slot, an image or a container with an id and drag = true may be dragged: a picture of it follows the mouse. One with drop = true takes it: its handler hears drop, with ev.id the one dropped on and ev.value the one dragged. Only elements of the same window, both enabled and shown, and never one onto itself. An inventory moves its items so:
{ kind = "slot", id = "bag1", image = "items/medkit.png", drag = true, drop = true }
...
cs.on_window("bag", { drop = function(p, ev) swap_items(p, ev.value, ev.id) end })
Looks
Every element and the window itself may change how it looks; the game's own look is the default.
| Field | Meaning |
|---|---|
back | Its background colour. |
border, border_width | Its border's colour and width (0 to 8; 1 when only a colour is given). |
radius | Its corners' roundness (0 to 32). |
back_image, back_slice | A picture as its background instead; back_slice {left, top, right, bottom} keeps those edges unstretched (a 9-slice box). |
hover, pressed | {back, color, border}: its look under the mouse, and pressed (buttons; hover on clickable containers too). |
font | regular, medium, semibold or bold: the game's font in that weight. |
padding | A number, or {left, top, right, bottom}. |
Looks apply to containers, buttons, slots, texts, fields and bars. Only the game's fonts can be named: a font file from a server would be read by the font engine on every player's computer, which is not done.
Styles
A plugin names the looks it uses again and again once, and its elements take them by name:
cs.window_style("danger", { back = "#3a1010", border = "#ff5050", radius = 6, hover = { back = "#5a1818" } })
cs.window_style("big", { font_size = 30, font = "bold" })
{ kind = "button", id = "kick", text = "Kick", style = "danger" }
{ kind = "text", text = "Round 3", style = { "danger", "big" }, radius = 0 }
A style holds any fields but kind, id, send and what an element holds (children, rows, tabs, options, columns). They are checked where they are used, as the element's own. A style is kept until the plugin stops (nil removes it); at most 128. A piece made again and again with different texts (a card for each item) is best a Luau function that returns the element.
HUD windows
hud = true makes a readout on the HUD: a timer, a score, a bar. It takes no cursor and no clicks, has nothing to close, and is drawn under the other windows; with frame = false and size = {0, 0} it is only what it holds. Change it with cs.window_set as any window:
cs.window(p, "timer", { hud = true, frame = false, size = { 0, 0 }, anchor = "top", offset = { 0, 40 },
body = { kind = "row", gap = 8, children = {
{ kind = "text", id = "time", text = "00:00", font_size = 32, font = "bold" },
{ kind = "bar", id = "left", value = 100, max = 100, smooth = 0.3, size = { 160, 12 } },
} } })
cs.window_set(p, "timer", "time", { text = "00:01" })
Texts in the player's language
Any text of a window (a title, a text, a label, a tooltip, a placeholder, an option's or a column's text) may be { t = key, args = {...} }: the plugin's text for that key in each player's language (its lang/ files, as Player:t), with {1}, {2}... from a list and {name} from the names of args:
cs.window(nil, "vote", { title = { t = "vote.title" }, body = { kind = "text",
text = { t = "vote.left", args = { 30, map = "de_dust2" } } } })
When a player changes language the server sends the window again in it, with the changes made since and the inputs as the player left them; the scroll, the tab and the field being typed in stay. (A window opened without such texts and given one after other changes keeps the language it had then.)
Inspecting a window
window_inspect 1 in the game's console outlines the plugin window's element under the mouse, with its id, kind and size in HUD units; window_inspect 0 stops it. cs_plugin preview opens a plugin on a local server and reloads it each time its files are saved, so a window can be shaped while it is on the screen.
Changing an open window
Opening a window again with the same id builds it again, but the lists' and tables' scroll, the tab chosen and the field being typed in (its text and caret) stay, so a page of a board can be shown by sending it whole. Smaller changes cost less with cs.window_set and cs.window_update:
cs.window_set(p, "tops", "board", { rows = page_rows(p, 2), selected = 0 })
cs.window_update(p, "tops", { page = { text = "2 / 5" }, prev = { enabled = true } })
children and rows are checked as cs.window checks them; the ids inside the old ones go, and the new ones may not take an id the window already has. The SDK's cs.d.luau types the windows (WindowSpec, WindowElement, WindowChanges), so cs_plugin check finds a field written wrong.
Checks and limits
cs.window checks the whole window first and fails with where it is wrong, so a wrong window is never half shown:
cs.window: body.children[2].price: a number from 0 to 1e+09
A window has at most 512 elements and 16 levels, and is at most 32 KB as sent. Windows travel over the plugin's reliable message queue; cs.window_set sends only the change, so updating a counter every second is cheap.
Instead of the buy menu
The buy_menu event comes before a buy menu opens (the buy key, the equipment key, or their commands), once the buy zone, the buy time and the team allow it. Blocking it and opening a window replaces the menu; the purchases stay the plugin's to check (p.can_buy, the money):
cs.on("buy_menu", function(ev)
if ev.menu == "buy" then
ev:block()
cs.window(ev.player, "shop", shop_window(ev.player))
end
end, { before = true })
To keep the game's menu and only add items to it, use cs.register_buy_item instead.
Loading pictures
A plugin can show pictures on the server's loading screen, cross-fading under the map's name and the bar while players load:
cs.loading_screen({ images = { "myserver/loading1.jpg", "myserver/loading2.jpg" }, seconds = 6, fade = 0.8 })
cs.loading_screen({ images = { "myserver/dust2.jpg" } }, { map = "de_dust2" }) -- that map's own
| Field | Default | Meaning |
|---|---|---|
images | required | 1 to 8 PNG or JPG files of the plugin's content, each at most 1920x1080, 2 MB together. |
seconds | 6 | How long each shows, 2 to 60. |
fade | 0.8 | The cross-fade, in seconds (0 to 5). |
fit | cover | cover fills the screen (cut at the edges), contain shows it whole. |
order | loop | loop in order, or random. |
A map's own list wins over the server's on that map; nil takes the plugin's list away. A list counts from the next map start, when its pictures are precached. Players' games download them before anything else and show each as it arrives; they keep them (by server and map), so the next visit shows them from the first frame. Players can turn servers' loading pictures off in the settings.
Keys
While a modal window is open: Escape closes it (when close is not false), 1 to 9 press its keyed buttons and slots, a field takes the typing once clicked (Escape leaves it). The game's own keys wait until the window closes. Panels take no keys; the mouse reaches them while the cursor is free (another window, the chat).