CS2 Lua API

utils

The utils module provides time, random numbers, keys, delayed tasks, the console, sound, and lookup of interfaces and signatures in game modules.

utils.key_state(vk: number)#

Whether a key is held.

Parameter Type Description
vk number Windows virtual key code, 0–255

The generic modifier codes 0x10 (Shift), 0x11 (Ctrl), 0x12 (Alt) always return false. Ask for the left and right keys separately: 0xA0/0xA1, 0xA2/0xA3, 0xA4/0xA5.

Returns: boolean.

local font = render.load_font("Verdana", 14, "outline")

events.render:set(function()
    if utils.key_state(0x06) then   -- mouse side button 2
        render.text(vector2(20, 120), color(255, 255, 0, 255), font, nil, "Button held")
    end
end)

utils.random_seed(seed: number)#

Sets the seed of the game random number generator.

Parameter Type Description
seed number seed

Returns: nothing.

utils.random_seed(42)
print(utils.random_int(1, 100))   -- the same number for the same seed

utils.random_int(a: number, b: number)#

A random integer from a to b inclusive. The order of arguments does not matter.

Parameter Type Description
a number bound
b number bound

Returns: number.

local phrases = { "gg", "nice", "wp" }
print(phrases[utils.random_int(1, #phrases)])

utils.random_float(a: number, b: number)#

A random fractional number from a to b. The order of arguments does not matter.

Parameter Type Description
a number bound
b number bound

Returns: number.

local c = color(utils.random_int(0, 255), utils.random_int(0, 255), utils.random_int(0, 255), 255)
print(c, " ", utils.random_float(0, 1))

utils.timestamp()#

Current time in milliseconds (unix).

Returns: a 64-bit integer (cdata). Use tonumber for arithmetic with regular numbers.

local t0 = utils.timestamp()
for i = 1, 100000 do local _ = math.sqrt(i) end
print("Took ", tonumber(utils.timestamp() - t0), " ms")

utils.unix_time()#

Current time in seconds (unix).

Returns: a 64-bit integer (cdata).

print("It is ", tonumber(utils.unix_time()), " seconds since 1970")

utils.strftime(fmt: string, time?: number)#

Formats local time with strftime specifiers (%Y, %m, %d, %H, %M, %S and so on).

Parameter Type Description
fmt string format
time number or a 64-bit integer time in unix seconds; default — now

Returns: string or nil if the format has an unknown specifier.

print(utils.strftime("%Y-%m-%d %H:%M:%S"))
print(utils.strftime("%d.%m", utils.unix_time()))

utils.system_time()#

Local time in parts.

Returns: number, number, number, number — hours, minutes, seconds, milliseconds.

local h, m, s = utils.system_time()
print(string.format("%02d:%02d:%02d", h, m, s))

utils.interface(module: string, name: string)#

Finds a game interface through the module's CreateInterface. The name is compared exactly.

Parameter Type Description
module string module name, for example "engine2.dll" or "engine2" (.dll is added without an extension)
name string interface name with version

Returns: lightuserdata or nil if the module or the interface is not found.

local engine = utils.interface("engine2.dll", "Source2EngineToClient001")
print(engine)

utils.pattern(module: string, signature: string)#

Searches a module for a byte sequence.

Parameter Type Description
module string module name
signature string hex bytes separated by spaces; ? or ?? — any byte

Returns: lightuserdata — the address of the first match, or nil.

local addr = utils.pattern("client.dll", "48 89 5C 24 08 ? 8B")
print(addr or "not found")

utils.username()#

Returns: string — the RezTOP user name.

print("Hello, ", utils.username(), "!")

utils.user()#

RezTOP user data.

Returns: table with the fields:

Field Type Description
name string user name, the same as utils.username()
avatar texture avatar; drawn with render.image
license number subscription end time, Unix time in seconds; 0 — a lifetime subscription or no data
  • avatar is always present. If there is no avatar or it is not loaded yet, it is an empty image: tostring gives "texture(0x0)", and accessing avatar.size gives the error invalid texture.
local u = utils.user()
local font = render.load_font("Verdana", 14, "outline")

local left = "lifetime"
if u.license > 0 then
    left = string.format("%d days", math.floor((u.license - tonumber(utils.unix_time())) / 86400))
end

events.render:set(function()
    render.image(u.avatar, vector2(20, 200), vector2(32, 32), color(255, 255, 255, 255), nil, 16)
    render.text(vector2(60, 208), color(255, 255, 255, 255), font, nil, u.name, " | ", left)
end)

utils.schedule(delay: number, fn: function, ...)#

Calls fn(...) after delay seconds.

Parameter Type Description
delay number delay, seconds
fn function function
... any arguments for fn
  • Tasks are checked once per frame; at most one task runs per frame.
  • An error inside fn is written to the console.
  • The task is removed when the script is unloaded.

Returns: nothing.

-- Repeat every 10 seconds
local function tick()
    print("Another 10 seconds passed, tick ", globals.tickcount)
    utils.schedule(10, tick)
end

utils.schedule(10, tick)

utils.console(...)#

Runs a game console command. Arguments are converted to strings and joined with a space.

Parameter Type Description
... any command parts

Returns: nothing.

utils.console("echo", "[script]", "loaded")   -- runs: echo [script] loaded

utils.playsound(path: string, volume?: number)#

Plays a game sound.

Parameter Type Description
path string game sound path
volume number volume, default 1.0

Returns: nothing.

events.player_death:set(function(e)
    if e.attacker == entity.local_controller() then
        utils.playsound("buttons/arena_switch_press_02", 0.5)
    end
end)