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

Storage, files, JSON and HTTP

Saving data, files, JSON, web requests and maths helpers.

storage

A small persistent key/value store per script.

4 entries

storage.get

function
storage.get(key [, default]) -> any

A stored value (saved in scripts/data/<script>/store.json, 1024 keys / 1 MiB).

Arguments
NameTypeDescription
keystring
defaultoptionalany
Returns
  • any
Example
Lua
local n = storage.get("kills", 0)

storage.set

function
storage.set(key, value) -> boolean

Stores a JSON-compatible value (tables nested at most 63 deep, else an error); false when over the size limit. Written at unload and every 5 s.

Arguments
NameTypeDescription
keystring
valueany
Returns
  • boolean
Example
Lua
storage.set("kills", n + 1)

storage.remove

function
storage.remove(key) -> boolean

Removes a key.

Arguments
NameTypeDescription
keystring
Returns
  • boolean
Example
Lua
storage.remove("kills")

storage.keys

function
storage.keys() -> string[]

All stored keys.

Returns
  • string[]
Example
Lua
for _, k in ipairs(storage.keys()) do end

files

Files inside the script's own data folder.

6 entries

files.read

function
files.read(path) -> string | nil, string

Reads a file from scripts/data/<script>/ (paths are relative to it).

Arguments
NameTypeDescription
pathstring
Returns
  • string | nil, string
Example
Lua
local text = files.read("notes.txt")

files.write

function
files.write(path, data) -> boolean | nil, string

Writes a file in the data folder (8 MiB per script). No ':' / device names / trailing dots; store.json is reserved.

Arguments
NameTypeDescription
pathstring
datastring
Returns
  • boolean | nil, string
Example
Lua
files.write("notes.txt", "hi")

files.append

function
files.append(path, data) -> boolean | nil, string

Appends to a file in the data folder.

Arguments
NameTypeDescription
pathstring
datastring
Returns
  • boolean | nil, string
Example
Lua
files.append("log.txt", line .. "\n")

files.exists

function
files.exists(path) -> boolean

Whether a file exists.

Arguments
NameTypeDescription
pathstring
Returns
  • boolean
Example
Lua
if files.exists("notes.txt") then end

files.list

function
files.list([dir]) -> string[] | nil, string

Entries of a folder in the data folder (folders end with /).

Arguments
NameTypeDescription
diroptionalstring
Returns
  • string[] | nil, string
Example
Lua
for _, f in ipairs(files.list()) do end

files.remove

function
files.remove(path) -> boolean | nil, string

Deletes a file in the data folder.

Arguments
NameTypeDescription
pathstring
Returns
  • boolean | nil, string
Example
Lua
files.remove("notes.txt")

json

JSON encode and decode.

3 entries

json.encode

function
json.encode(value [, pretty]) -> string

Encodes tables, strings, numbers, booleans (depth <= 64). Empty tables encode as [].

Arguments
NameTypeDefaultDescription
valueany
prettyoptionalbooleanfalse
Returns
  • string
Example
Lua
local s = json.encode({a = 1})

json.decode

function
json.decode(text) -> any | nil, string

Decodes JSON (at most 1 MiB, depth <= 64); null becomes json.null.

Arguments
NameTypeDescription
textstring
Returns
  • any | nil, string
Example
Lua
local t = json.decode('{"a": 1}')

json.null

constant

The JSON null value.

Returns
  • lightuserdata

http

Asynchronous HTTP(S) requests.

3 entriesAllow internet connections

Everything here needs Allow internet connections in Scripts > Security (off by default).

http.get

function
http.get(url [, opts, callback]) -> integer | nil, string

Asynchronous GET; opts = {headers = {...}}. callback(res) runs on a later frame with res = {status, body, headers, error}. http/https only, public addresses only (no localhost / LAN / link-local), 5 redirects, 10 s timeout, 2 MiB.

Arguments
NameTypeDescription
urlstring
optsoptionaltable
callbackfunction
Returns
  • integer | nil, string
Example
Lua
http.get("https://example.com/api", function(res) log.info(res.status) end)

http.post

function
http.post(url, body [, opts, callback]) -> integer | nil, string

Asynchronous POST.

Arguments
NameTypeDescription
urlstring
bodystring
optsoptionaltable
callbackfunction
Returns
  • integer | nil, string
Example
Lua
http.post(url, json.encode({a = 1}), {headers = {["Content-Type"] = "application/json"}}, function(res) end)

http.request

function
http.request(spec, callback) -> integer | nil, string

spec = {url=, method="GET"|"POST"|"PUT"|"PATCH"|"DELETE"|"HEAD", body=, headers=}.

Arguments
NameTypeDescription
spectable
callbackfunction
Returns
  • integer | nil, string
Example
Lua
http.request({url = url, method = "PUT", body = "x"}, function(res) end)

math

Helpers added to the standard math library.

3 entries

math.clamp

function
math.clamp(v, lo, hi) -> number

Clamps v to [lo, hi].

Arguments
NameTypeDescription
vnumber
lonumber
hinumber
Returns
  • number
Example
Lua
math.clamp(x, 0, 1)

math.lerp

function
math.lerp(a, b, t) -> number

Linear blend.

Arguments
NameTypeDescription
anumber
bnumber
tnumber
Returns
  • number
Example
Lua
math.lerp(0, 100, 0.5)

math.round

function
math.round(v [, step]) -> number

Rounds to a multiple of step.

Arguments
NameTypeDefaultDescription
vnumber
stepoptionalnumber1
Returns
  • number
Example
Lua
math.round(3.14159, 0.01)