evasivedocs
CS2 scriptingLua 5.4.9 · 25 guides · 292 API entries
Start here

How scripts run

A script is not a loop that runs forever. evasive runs the file once when you load it, and after that it only calls the functions you handed it. This page covers those calls, the time and memory a script gets, and what happens when something goes wrong.

Load, then callbacks

When you load a script, its top level runs once. That is where you create your settings, load fonts and images, and register your handlers. The top level has 10 ms to finish.

After that, evasive calls your handlers when something happens. Add one with events.on(name, fn):

Lua
local frames = 0

events.on("tick", function(dt)
    frames = frames + 1
end)

events.on("paint", function()
    draw.text(20, 20, "ticks: " .. frames, color(255, 255, 255))
end)

events.on returns a token. Pass it to events.off(name, token) to remove that handler again, or call events.off(name) to remove every handler of the event. An event can have as many handlers as you like.

You can also define a global function named on_ plus the event name. evasive registers those after the top level has run:

Lua
function on_paint()
    draw.text(20, 40, "drawn by on_paint", color(255, 255, 255))
end

function on_unload()
    print("bye")
end

paint and tick

These two do most of the work, and they are not the same.

  • paint runs on every frame the overlay shows. All draw calls must happen here. Anywhere else they raise draw.* only works inside paint handlers.
  • tick runs 64 times a second, also while the overlay is hidden. It gets dt, the seconds since the last tick. Do your counting, searching and decisions here.

A good script does its work in tick or in event handlers, keeps the results in local variables, and only draws them in paint.

Every event

Event Argument When it fires
paint none Every frame the overlay shows. Draw here.
tick dt 64 times a second, also while the overlay is hidden.
load none After the script loaded or reloaded without an error.
unload none Right before the script is closed.
menu open The menu opened (true) or closed (false).
config_change name You switched to another profile or reloaded the current one.
map_change map The map changed. The name is "" when you disconnect.
round_start none Several players are alive again at once. This is a guess, not a game event.
freeze_start none Freezetime or the team intro began.
round_live none Freezetime ended and the round is live.
local_spawn none You became alive.
local_death none You died.
local_kill e You killed someone. e = {player, pos, local_kill = true}.
local_hit e You hit someone. e = {player, damage, health, hitgroup, headshot, killed}. player can be nil.
player_hurt e A listed player lost health. e = {player, pos, damage, health}.
player_death e A listed player died, not by your hand. e = {player, pos}.
player_sound e A listed player made a sound others hear, like footsteps or landing. e = {player, pos, time}.
weapon_fire e A listed player fired. e = {player, weapon, pos}, where pos is the eye.
grenade_detonate e A grenade went off, a smoke bloomed or a molotov's fire appeared. e = {kind, index, pos, thrower}.
bomb_planted e The C4 was planted. e = {site, pos}.
bomb_defused e The planted C4 was defused.
bomb_exploded e The planted C4 exploded.
combat dt Before the built in aim and trigger run on a new game frame. Only for approved action scripts, see Aim, attack and keys.

In the player events, e.player is a small table, not a full player: {index, name, team, is_enemy}, and is_local in player_sound and weapon_fire. Use entities.player(e.player.index) when you need the rest. All the game events need the Allow game scripting permission.

The time budget

Scripts run on the overlay's own thread, after the ESP and before the menu. Every millisecond a script takes is a millisecond of delay on your screen, so evasive keeps a strict clock.

  • All scripts together get 1 ms per frame by default. You can set 0.25 to 4 ms under Scripts > Other > Script budget.
  • One callback never gets more than 2 ms.
  • A callback that runs past its time is stopped with frame budget exceeded. The script's last good frame is drawn again, so nothing flickers.
  • A script that goes over budget on 30 of the last 60 frames, or for 5 seconds in total, is suspended. It turns red. Load it again to resume it.

To stay inside the budget, do heavy work less often. A search that runs in tick every few ticks is cheaper than one that runs in every paint.

Lua
local slow_result = 0
local counter = 0

events.on("tick", function()
    counter = counter + 1
    if counter % 16 ~= 0 then return end   -- four times a second
    slow_result = slow_result + 1
end)

Memory

Each script can use up to 64 MiB. A script that hits the limit is suspended with memory limit (64 MiB) reached. The usual cause is a table that grows every frame and never gets cleared.

Errors

An error inside a handler stops the script. Its name turns red and the console shows the message with the line number. Load it again once you fixed it.

When you reload a running script and the new version fails to load, the console shows reload failed: and the old version keeps running.

If one call can fail and you want to carry on, wrap it in pcall:

Lua
local ok, err = pcall(function()
    local data = json.decode("{not json")   -- broken JSON gives nil
    print(data.value)                      -- and indexing nil raises an error
end)
if not ok then log.warn("could not read the data: " .. tostring(err)) end

Values that may be nil

Many functions return nil when there is nothing to give: you are not in a match, a point is off screen, or a file is missing. In the reference a return type like number | nil means exactly that. A type like image | nil, string gives nil and a message when it fails.

Lua
events.on("paint", function()
    if not game or not game.connected() then return end
    local me = entities.local_player()
    if not me then return end
    local x, y = game.world_to_screen(me.origin)
    if x then draw.circle(x, y, 4, color(255, 255, 255)) end
end)

Namespaces behind a permission are nil while the switch is off. That is why the example checks game first. See Permissions.

Players only last one frame

A player object from entities.players() or entities.player() is valid only in the frame it was listed in. Using it later raises stale player. Keep p.index instead and look the player up again:

Lua
local target_index = nil

events.on("tick", function()
    if not entities then return end
    local list = entities.players("enemies")
    target_index = list[1] and list[1].index or nil
end)

events.on("paint", function()
    if not target_index then return end
    local p = entities.player(target_index)
    if p and p.alive then
        local x, y = p:screen()
        if x then draw.text(x, y, p.name, color(255, 80, 80), {align = "center"}) end
    end
end)

What Lua you get

Scripts run Lua 5.4 with string, table, math, utf8 and coroutine. os only has os.clock, os.time, os.date and os.difftime. The full io, os and debug libraries come only with the Allow insecure FFI switch.

Some things are left out on purpose. There is no dofile or loadfile, load takes text only, string.dump is gone, and __gc metamethods are refused.

require("name") loads name.lua from the scripts folder or from scripts\lib\. A dot in the name is a folder: require("ui.theme") loads ui\theme.lua.

Limits

What Limit
Time for all scripts per frame 1 ms by default, 0.25 to 4 ms
One callback 2 ms
The top level on load 10 ms
Memory per script 64 MiB
tick rate 64 Hz
Draw commands per frame 8192
Text in one draw.text 1024 bytes
Controls per script 256
Items in one combo 64
Script file size 1 MiB
storage per script 1 MiB, tables nested at most 63 deep
files per script 8 MiB
One Lua pattern in string.find, string.match or string.gsub 4096 bytes
Map rays per logic pass 256
Wallbang checks per logic pass 32
Grenade predictions per logic pass 4
Shader materials per script 8