evasivedocs
CS2 scriptingLua 5.4.9 · 25 guides · 292 API entries
Tutorials

Your own settings

In this tutorial you build a crosshair that users shape from the menu: colour, style, size, a key to hide it and a reset button. Every control you add shows up in Scripts > Settings in a box with your script's name, and its value is saved in the user's config.

The ui functions need no permission.

1. A checkbox and a colour

Create controls once, at the top level of the script. Never create them inside a handler.

Lua
local enabled = ui.checkbox("Enabled", true)
local col = ui.color("Colour", color(120, 255, 120))

events.on("paint", function()
    if not enabled:get() then return end
    local sw, sh = draw.screen_size()
    draw.circle_filled(sw / 2, sh / 2, 3, col:get())
end)

Each ui function returns a control. Read its value with :get(). ui.color puts its swatch on the right of the row above it, here next to Enabled.

2. A style and a size

Lua
local style = ui.combo("Style", {"Cross", "Dot", "Circle"}, 1)
local size = ui.slider("Size", 2, 30, 8, {suffix = "px"})
local thick = ui.slider("Thickness", 1, 4, 2, {decimals = 1, suffix = "px"})

ui.combo gives back the position of the chosen item, starting at 1. ui.slider(label, min, max, default, opts) takes up to 4 decimals and a suffix for the number.

3. Show only what applies

The gap only matters for the cross. :visible(false) hides a control, and :on_change(fn) runs when the value changes, whether from the menu, a profile load or :set:

Lua
local style = ui.combo("Style", {"Cross", "Dot", "Circle"}, 1)
local gap = ui.slider("Gap", 0, 10, 3, {suffix = "px"})

gap:visible(style:get() == 1)
style:on_change(function(v)
    gap:visible(v == 1)
end)

Call it once at the top as well, so the control starts in the right state.

4. A key and a button

Lua
local hide_key = ui.keybind("Hide while held", vk.mbutton, "hold")
ui.button("Reset size", function()
    print("size reset")
end)

A keybind has a mode: "hold", "toggle" or "always". Don't read the key yourself, ask hide_key:active(). It is true while the key is held (hold), after a press until the next one (toggle), or all the time (always). It is false while the menu is open or a text field has focus, so typing never triggers it.

A button's function runs on the next frame, inside the script's time budget.

The finished script

Lua
-- @name Crosshair
-- @description A crosshair you shape from the menu.

local enabled = ui.checkbox("Enabled", true)
local col = ui.color("Colour", color(120, 255, 120))
local style = ui.combo("Style", {"Cross", "Dot", "Circle"}, 1)
local size = ui.slider("Size", 2, 30, 8, {suffix = "px"})
local gap = ui.slider("Gap", 0, 10, 3, {suffix = "px"})
local thick = ui.slider("Thickness", 1, 4, 2, {decimals = 1, suffix = "px"})
ui.separator("Extras")
local parts = ui.multi_combo("Show", {"Outline", "Centre dot"}, {true, false})
local hide_key = ui.keybind("Hide while held", vk.mbutton, "hold")
ui.button("Reset size", function()
    size:set(8)
    gap:set(3)
    thick:set(2)
end)

gap:visible(style:get() == 1)
style:on_change(function(v)
    gap:visible(v == 1)
end)

local function cross(cx, cy, s, g, t, c)
    draw.line(cx - g - s, cy, cx - g, cy, c, t)
    draw.line(cx + g, cy, cx + g + s, cy, c, t)
    draw.line(cx, cy - g - s, cx, cy - g, c, t)
    draw.line(cx, cy + g, cx, cy + g + s, c, t)
end

events.on("paint", function()
    if not enabled:get() or hide_key:active() then return end
    local sw, sh = draw.screen_size()
    local cx, cy = math.floor(sw / 2), math.floor(sh / 2)
    local s, g, t, c = size:get(), gap:get(), thick:get(), col:get()
    local show = parts:get()
    local shadow = color(0, 0, 0, 200)

    if style:get() == 1 then
        if show[1] then cross(cx, cy, s, g, t + 2, shadow) end
        cross(cx, cy, s, g, t, c)
    elseif style:get() == 2 then
        if show[1] then draw.circle_filled(cx, cy, s / 2 + 1, shadow) end
        draw.circle_filled(cx, cy, s / 2, c)
    else
        if show[1] then draw.circle(cx, cy, s, shadow, t + 2) end
        draw.circle(cx, cy, s, c, t)
    end
    if show[2] then draw.circle_filled(cx, cy, 1.5, c) end
end)

ui.multi_combo gives a table of booleans in the order of its items, so show[1] is Outline. ui.separator("Extras") draws a titled line between groups of controls.

Every control

Function :get() gives
ui.checkbox(label, default) true or false
ui.slider(label, min, max, default, opts) a number
ui.combo(label, items, default) the chosen position, from 1
ui.multi_combo(label, items, default) a table of booleans
ui.color(label, default) a colour
ui.keybind(label, key, mode) the key code. Use :active() for the state.
ui.text_input(label, default, max_len) a string
ui.button(label, fn) nothing to read. fn runs when pressed.
ui.label(text) a line of text. :set changes it.
ui.separator(title) a titled line

Every control also has :set(value), :visible(shown), :on_change(fn) and :name(). ui.accent() is not a control: it gives the menu's accent colour.

Labels are ids

The label is how evasive tells your controls apart and how it finds their saved values. Two controls with the same label fail with duplicate control id. To show the same text twice, add ## and anything after it. Only the part before ## is shown:

Lua
local box_col = ui.color("Colour##box", color(255, 255, 255))
local name_col = ui.color("Colour##name", color(200, 200, 200))

If you rename a label, the old saved value is not carried over.

Limits

A script can have up to 256 controls, and a combo up to 64 items.

Next

Input and keys reads the keyboard and the mouse directly.