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

Bomb timer

The C4 countdown, drawn right over the planted bomb.

Needs: Allow game scripting.

Lua
-- @name Bomb timer
-- @description The C4 countdown over the planted bomb. Needs "Allow game scripting".
local enabled = ui.checkbox("Enabled", true)
local col     = ui.color("Colour", color(255, 200, 60))

events.on("paint", function()
    -- entities and game are nil without "Allow game scripting"
    if not enabled:get() or not entities or not game.connected() then return end
    local c4 = entities.bomb()
    if not c4 or not c4.ticking then return end
    local x, y = game.world_to_screen(c4.origin)
    if not x then return end            -- not on screen
    draw.text(x, y, string.format("C4 %.1f s", c4.time_left), col:get(), {align = "center", outline = true})
end)

How it works

  • entities.bomb() gives a table about the C4, or nil when there is none in the match.
  • ticking is true from the plant until it is defused or explodes. time_left is the seconds until it blows.
  • game.world_to_screen places the text on the bomb itself. When the bomb is behind you it gives nil and nothing is drawn.

Try this

The bomb table has more in it. This version shows a running defuse too, green when it will finish in time and red when it will not. Without a defuse the text turns red in the last 5 seconds.

Lua
local col = ui.color("Colour", color(255, 200, 60))

events.on("paint", function()
    if not entities or not game.connected() then return end
    local c4 = entities.bomb()
    if not c4 or not c4.ticking then return end
    local x, y = game.world_to_screen(c4.origin)
    if not x then return end

    local text = string.format("C4 %.1f s", c4.time_left)
    local c = col:get()
    if c4.being_defused then
        text = text .. string.format("  defuse %.1f s", c4.defuse_left)
        c = c4.defuse_left <= c4.time_left and color(120, 220, 90) or color(255, 80, 80)
    elseif c4.time_left < 5 then
        c = color(255, 80, 80)
    end
    draw.text(x, y, text, c, {align = "center", outline = true})
end)

defuse_length is how long a full defuse takes, and c4.site tells you the bomb site.