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

Aim, attack and keys

Most scripts only look. With the right permission and your approval, a script can also turn your view, fire and press keys. This page covers how that works, what it can and cannot do, and how to write such a script.

Reading what aim and trigger do

Before acting yourself, you can watch what evasive's own aim and trigger decide. This only needs Allow game scripting:

Lua
events.on("paint", function()
    if not combat then return end
    local a = combat.aim_state()
    if a and a.target then
        local p = entities.player(a.target)
        local name = p and p.name or "?"
        draw.text(20, 60, "aim: " .. name .. string.format(" %.0f dmg", a.damage or 0), ui.accent(), {shadow = true})
    end
    local t = combat.trigger_state()
    if t then
        draw.text(20, 76, "trigger: " .. tostring(t.state), color(200, 200, 200), {shadow = true})
    end
end)

combat.aim_state() has active, target (a player index), hitbox, hitgroup, point, fov, damage, penetrated, sent and blocked. combat.trigger_state() has state, target, hitgroup, damage, fire_in_ms, hit_chance, fired, penetrated, reject and presses.

What an action script needs

Three things, all of them on purpose:

  1. The line -- @permissions act in the script's header.
  2. Allow scripts to aim, shoot and press keys on in Scripts > Security.
  3. Your approval of this exact version of the file under Action approvals. Any edit to the file needs a new approval.

Until all three are in place, act, aim, attack and keys are nil, and so are input.press, input.down, input.up, input.hold and input.release_all. A script should check and wait quietly:

Lua
-- @name My action script
-- @permissions act

if not act then
    print("waiting: turn on the action switch and approve this version")
    return
end

print("ready to act")

A return at the top level ends the script's setup without an error. It stays loaded, and approving it reloads it with the action functions in place. While Allow insecure FFI is on, action scripts cannot run at all.

Requests, not commands

Every action call returns ok, reason. true, "" means the request was accepted. It does not mean anything moved yet. false comes with a reason, like "menu" when the menu is open or "focus" when CS2 is not the window in front.

Actions go through the same safety checks as evasive's own aim and trigger, and they never happen while:

  • the evasive menu is open, or a text field in it has focus,
  • CS2 is not the foreground window, shows a cursor (console, buy menu) or has chat open,
  • you are dead or not in a match,
  • the script panic is on.

When you move the mouse yourself, script aim lets go at once and aim requests are refused with "hand" for 200 ms. Your hand always wins.

Leases

Aim and held keys are leases. They run out unless you renew them:

  • An aim request lasts 60 ms. Repeat it every tick for as long as you want it.
  • A key held with input.down or keys.down lasts 2 seconds. Call it again to keep holding.

When you are done, let go on purpose with aim.release(), attack.release(), input.up(key), keys.up(cmd) or input.release_all(). Releases are always accepted, also while actions are blocked.

If one of your handlers raises an error, or runs past its own time cap, evasive drops everything the script holds.

Aim

aim.look_at(pos, opts) asks the built in aim to turn your view smoothly toward a world point. aim.turn(d_pitch, d_yaw, opts) turns the view by an angle instead. Both take these options:

Option Meaning
fov How far from the crosshair the point may be, in degrees. Default 10, capped by Script aim max FOV.
aim_time_ms How long the turn takes. Default 40. Never below Minimum aim time, and the weapon's own aim profile time wins if it is longer.
player A player index. Then the player must be alive and not spawn protected, and aim leads their movement.
hitgroup A tag for the request, 0 to 10. The point stays where you put it.
mode "assist" (default) gives way while evasive's own aim has a target. "override" takes over, and needs Scripts may replace the Aim target on.

Script aim turns at most 900 degrees a second, measured on real time, so it moves the same at any frame rate. The first frame of a new request only starts the clock.

Nothing moves while the point is outside fov, the held item is not a firearm, it is reloading or empty, or the weapon's aim profile is off or blocks the shot.

Attack

Call What it does
attack.fire(opts) One primary press. opts.hold_ms from 1 to 200 (default 30), opts.delay_ms from 0 to 1000.
attack.hold(ms) One primary press held for ms.
attack.secondary(ms) One secondary press.
attack.release() Lets go of a queued or held attack.

A primary attack is refused with "combat" unless the weapon has ammo, is not reloading and its trigger profile is on with its conditions met. Any attack is refused while evasive's own trigger or grenade helper is attacking ("native"), while you hold the button yourself ("physical"), while another attack is queued ("busy"), or past 8 a second ("rate").

Keys

keys presses whatever key CS2 has bound to a game command, so it works with anyone's binds:

Call What it does
keys.press(cmd, ms) Presses the command's key, 10 to 200 ms. keys.tap is the same.
keys.down(cmd) and keys.up(cmd) Hold on a 2 second lease, and let go. keys.release is the same as keys.up.
keys.hold(cmd, ms) Holds for up to 2000 ms.
keys.jump(), keys.reload(), keys.use(), keys.drop(), keys.inspect() Short presses of those commands.
keys.duck(ms), keys.walk(ms) Hold crouch or walk. The default 30 ms is a tap.
keys.slot(n), keys.lastinv(), keys.next_weapon(), keys.prev_weapon() Weapon switching.
keys.release_all() Lets go of everything the script holds.

Commands are written as in a bind: "+jump", "+duck", "slot3" and so on. A command with no key bound is refused with "unbound".

input.press(vk, ms), input.down, input.up and input.hold do the same for a key code. Some keys are never allowed, like the Windows key, F12 and media keys. System shortcuts such as Alt+Tab are refused, and so is any key you are holding yourself or that another script holds. A script can hold at most 4 keys and 2 modifiers, and make 20 key changes a second.

The combat event

combat fires on every new game frame right before evasive's own aim and trigger run, and only while the script may act. It gets dt and has 0.5 ms per script. It is the best place for aim requests:

Lua
-- @name Look at the bomb
-- @description While the key is held, turn toward the planted C4.
-- @permissions act

local key = ui.keybind("Look at bomb", vk.xbutton1, "hold")
local last_reason = ""
local aiming = false

if not act then
    print("waiting: turn on the action switch and approve this version")
    return
end

events.on("combat", function()
    if not key:active() then
        if aiming then aim.release() end
        aiming = false
        return
    end
    local c4 = entities.bomb()
    if not c4 or not c4.planted then return end
    local ok, why = aim.look_at(c4.origin, {fov = 30, aim_time_ms = 250})
    aiming = ok
    last_reason = ok and "" or why
end)

events.on("paint", function()
    if last_reason ~= "" then
        draw.text(20, 60, "refused: " .. last_reason, color(255, 160, 60), {shadow = true})
    end
end)

The request is renewed every game frame while the key is held and released once when you let go. Nothing moves while the bomb is more than 30 degrees from your crosshair. Remember that an accepted request is not a promise: with a knife in hand aim.look_at still gives true, but the view only turns while you hold a firearm.

A key example

Lua
-- @name Auto reload
-- @description Reloads when the magazine is empty.
-- @permissions act

if not act then
    print("waiting: turn on the action switch and approve this version")
    return
end

local next_try = 0

events.on("tick", function()
    local me = entities.local_player()
    if not me or not me.alive then return end
    local w = me.weapon
    if not w or not w.max_ammo or w.max_ammo <= 0 then return end
    if w.ammo == 0 and not w.reloading and time.now() >= next_try then
        next_try = time.now() + 1
        local ok, why = keys.reload()
        if not ok then log.info("reload refused: " .. why) end
    end
end)

next_try keeps it from asking again every tick while the reload starts.

act.status

act.status() tells the script where it stands: allowed, panic, reason, aim_live, attack_live, accepted, refused, held (the keys it holds), rate and leases. When allowed is false, reason says why.

Lua
events.on("paint", function()
    if not act then return end
    local s = act.status()
    draw.text(20, 40, s.allowed and "actions allowed" or ("blocked: " .. s.reason), color(255, 255, 255), {shadow = true})
end)

Your controls

The Console page of the Scripts tab has two boxes for this:

  • Action activity shows one script's accepted and refused calls, held keys and leases, and has Revoke action approval.
  • Script actions has Stop script actions, which lets go of every key at once and blocks all scripts until you press Reset script panic. It also has Script aim max FOV, Minimum aim time (40 ms by default) and Scripts may replace the Aim target. These limits save at once and never go in configs.

Your panic key from Misc also stops script actions, as long as no other bind uses that key.