Scripting API

From Valve Developer Community
< Counter-Strike 2 Workshop Tools
Revision as of 17:05, 14 October 2025 by TheCreator (talk | contribs) (Fix formatting)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
English (en)Translate (Translate)

This is the cs_script API for Counter-Strike 2.

Classes

Instance

The top level API provided to scripts attached to a point_script entity. Access these functions by importing Instance from "cs_script/point_script".

Methods

Function Signature Description
ClientCommand ClientCommand(playerSlot: number, command: string): void Issue the specified command to the specified client.
ConnectOutput ConnectOutput(target: Entity, output: string, callback: (inputData: { value?: InputValue, caller?: Entity, activator?: Entity }) => any): number | undefined Connect the output of an entity to a callback. The return value is a connection id that can be used in DisconnectOutput
DebugBox DebugBox(config: { mins: Vector, maxs: Vector, duration?: number, color?: Color }): void Draw an axis aligned box in the world. Only works in dev environments.
DebugLine DebugLine(config: { start: Vector, end: Vector, duration?: number, color?: Color }): void Draw a line in the world. Only works in dev environments.
DebugScreenText DebugScreenText(config: { text: any, x: number, y: number, duration?: number, color?: Color }): void Print some text to the game window. Only works in dev environments.
DebugSphere DebugSphere(config: { center: Vector, radius: number, duration?: number, color?: Color }): void Draw a wire sphere in the world. Only works in dev environments.
DisconnectOutput DisconnectOutput(connectionId: number): void Use a connection id to disconnect the output of an entity from a callback.
EntFireAtName EntFireAtName(config: { name: string, input: string, value?: InputValue, caller?: Entity, activator?: Entity, delay?: number }): void Fire the input on all targets matching the specified names.
EntFireAtTarget EntFireAtTarget(config: { target: Entity, input: string, value?: InputValue, caller?: Entity, activator?: Entity, delay?: number }): void Fire the input on the specified target.
FindEntitiesByClass FindEntitiesByClass(className: string): Entity[] Find entities of the specified class name.
FindEntityByClass FindEntityByClass(className: string): Entity | undefined Find the first entity of the specified class name.
FindEntitiesByName FindEntitiesByName(name: string): Entity[] Find entities matching the specified name.
FindEntityByName FindEntityByName(name: string): Entity | undefined Find the first entity matching the specified name.
GetGameMode GetGameMode(): number Get the current Game Mode.
Todo:  Document return values and their meaning.
GetGameTime GetGameTime(): number Get the game time in seconds.
GetGameType GetGameType(): number Get the current Game Type.
Todo:  Document return values and their meaning.
GetMapName GetMapName(): string Get the name of the current map.
GetPlayerController GetPlayerController(slot: number): CSPlayerController | undefined Get the player controller in the given slot.
GetRoundsPlayed GetRoundsPlayed(): number Get the number of rounds played in the current game.
IsFreezePeriod IsFreezePeriod(): boolean Get if the game is currently in a Freeze period.
IsWarmupPeriod IsWarmupPeriod(): boolean Get if the game is currently in a Warmup period.
Msg Msg(text: any): void Log a message to the console.
ServerCommand ServerCommand(command: string): void Issue a command on the server.
SetNextThink SetNextThink(time: number): void Set when the OnThink callback should next be run. The exact time will be on the tick nearest to the specified time, which may be earlier or later.
SetThink SetThink(callback: () => void): void Called per-think. Control when this is run using SetNextThink.
TraceBox TraceBox(trace: { mins: Vector, maxs: Vector } & BaseTraceConfig): TraceResult Trace an axis aligned bounding box along a line and detect collisions.
TraceBullet TraceBullet(trace: BulletTrace): BulletTraceResult[] Trace as a bullet and detect hits and damage.
TraceLine TraceLine(trace: BaseTraceConfig): TraceResult Trace a point along a line and detect collisions.
TraceSphere TraceSphere(trace: { radius: number } & BaseTraceConfig): TraceResult Trace a sphere along a line and detect collisions.

Hooks

Passing callbacks with fitting signatures into these functions causes them to be called at the appropriate times.

Function Signature Description
OnActivate OnActivate(callback: () => void): void Called when the point_script entity is activated.
OnBeforePlayerDamage OnBeforePlayerDamage(callback: (event: BeforePlayerDamageEvent) => BeforePlayerDamageModify | { abort: true } | void): void Called when a CSPlayerPawn is about to take damage. @param callback: Return `{ damage: N }` to modify the amount of damage. Armor and hitgroup modifications will be applied to this new value. Return `{ abort: true }` to cancel the damage event.
OnBombDefuse OnBombDefuse(callback: (event: { plantedC4: Entity, defuser: CSPlayerPawn }) => void): void Called when a player defuses the c4.
OnBombPlant OnBombPlant(callback: (event: { plantedC4: Entity, planter: CSPlayerPawn }) => void): void Called when a player plants the c4.
OnBulletImpact OnBulletImpact(callback: (event: { weapon: CSWeaponBase, position: Vector }) => void): void Called when a bullet hits a surface. This will trigger for each bullet and for each impact. Penetrations can cause a single bullet to trigger multiple impacts.
OnGrenadeBounce OnGrenadeBounce(callback: (event: { projectile: Entity, bounces: number }) => void): void Called when a grenade bounces off a surface. `bounces` is the number of bounces so far.
OnGrenadeThrow OnGrenadeThrow(callback: (event: { weapon: CSWeaponBase, projectile: Entity }) => void): void Called when a grenade is thrown. `projectile` is the newly created grenade projectile.
OnGunFire OnGunFire(callback: (event: { weapon: CSWeaponBase }) => void): void Called when a gun emits bullets. A shotgun will only trigger this once when emitting multiple bullets at once.
OnGunReload OnGunReload(callback: (event: { weapon: CSWeaponBase }) => void): void Called when a gun is reloaded.
OnKnifeAttack OnKnifeAttack(callback: (event: { weapon: CSWeaponBase, attackType: CSWeaponAttackType }) => void): void Called when a knife attacks, even if it misses.
OnPlayerActivate OnPlayerActivate(callback: (event: { player: CSPlayerController }) => void): void Called when a client finishes loading and is ready to spawn.
OnPlayerChat OnPlayerChat(callback: (event: { player: CSPlayerController | undefined, text: string, team: number }) => void): void Called when a player sends a chat message. `team` will match they player's team if the message was sent to team chat.
OnPlayerConnect OnPlayerConnect(callback: (event: { player: CSPlayerController }) => void): void Called when a client finishes the initial connection handshake with the server.
OnPlayerDamage OnPlayerDamage(callback: (event: PlayerDamageEvent) => void): void Called when a player has taken damage.
OnPlayerDisconnect OnPlayerDisconnect(callback: (event: { playerSlot: number }) => void): void Called when a client disconnects from the server.
OnPlayerJump OnPlayerJump(callback: (event: { player: CSPlayerPawn }) => void): void Called when a player jumps off the ground.
OnPlayerKill OnPlayerKill(callback: (event: { player: CSPlayerPawn, inflictor?: Entity, attacker?: Entity, weapon?: CSWeaponBase }) => void): void Called when a player dies. `inflictor`, `attacker` and `weapon` will match the damage event that caused the kill.
OnPlayerLand OnPlayerLand(callback: (event: { player: CSPlayerPawn }) => void): void Called when a player hits the ground while falling.
OnPlayerPing OnPlayerPing(callback: (event: { player: CSPlayerController, position: Vector }) => void): void Called when a player pings a location.
OnPlayerReset OnPlayerReset(callback: (event: { player: CSPlayerPawn }) => void): void Called when a player respawns, changes team, or is placed back at spawn due to a round restart.
OnRoundEnd OnRoundEnd(callback: (event: { winningTeam: number, reason: CSRoundEndReason }) => void): void Called when a team wins a round.
OnRoundStart OnRoundStart(callback: () => void): void Called when a new round begins.
OnScriptInput OnScriptInput(name: string, callback: (inputData: { caller?: Entity, activator?: Entity }) => void): void Called when input RunScriptInput is triggered on the point_script entity with a parameter value that matches name.
OnScriptReload OnScriptReload<T>(config: { before?: () => T, after?: (memory: T) => void }): void Called in Tools mode when the script is reloaded due to changes. The before callback will be invoked before pre-load teardown. The after callback will be invoked after the new script is evaluated and will be passed the return value of the before callback.

Entity

The base class for all entities.

Methods

Function Signature Description
GetAbsAngles GetAbsAngles(): QAngle The angles (pitch, yaw, and roll) of this entity relative to the world.
GetAbsOrigin GetAbsOrigin(): Vector The position of the origin of this entity relative to the world.
GetAbsVelocity GetAbsVelocity(): Vector The velocity of this entity relative to the world.
GetClassName GetClassName(): string
GetEntityName GetEntityName(): string
GetEyeAngles GetEyeAngles(): QAngle The angles of the eyes of this entity relative to the world.
GetEyePosition GetEyePosition(): Vector The angles of the eyes of this entity relative to the world.
GetGroundEntity GetGroundEntity(): Entity | undefined Get the entity that this entity is resting on. Will be `undefined` if in the air.
GetHealth GetHealth(): number
GetLocalAngles GetLocalAngles(): QAngle The angles (pitch, yaw, and roll) of this entity relative to its parent. Will be relative to the world if no parent.
GetLocalOrigin GetLocalOrigin(): Vector The position of the origin of this entity relative to its parent. Will be relative to the world if no parent.
GetLocalVelocity GetLocalVelocity(): Vector The velocity of this entity relative to its parent. Will be relative to the world if no parent.
GetMaxHealth GetMaxHealth(): number
GetOwner GetOwner(): Entity | undefined
GetParent GetParent(): Entity | undefined
GetTeamNumber GetTeamNumber(): number
IsAlive IsAlive(): boolean
IsValid IsValid(): boolean Returns `false` if the entity has been deleted.
IsWorld IsWorld(): boolean Get if this is the world entity.
Kill Kill(): void
Remove Remove(): void
SetEntityName SetEntityName(name: string): void
SetMaxHealth SetMaxHealth(health: number): void
SetHealth SetHealth(health: number): void
SetOwner SetOwner(owner: Entity | undefined): void
SetParent SetParent(parent: Entity | undefined): void
TakeDamage TakeDamage(takeDamage: EntityDamage): number Apply damage to this entity. Damage value will be modified by armor and hitgroup.
Teleport Teleport(newValues: { position?: Vector, angles?: QAngle, velocity?: Vector }): void Update the physics state of this entity.

BaseModelEntity

extends Entity

Methods

Function Signature Description
Glow Glow(color?: Color): void
SetColor SetColor(color: Color): void
SetModel SetModel(modelName: string): void
SetModelScale SetModelScale(scale: number): void
Unglow Unglow(): void

CSPlayerController

extends Entity

Methods

Function Signature Description
AddScore AddScore(points: number): void Add to the player's score. Negative values are allowed but the score will not go below zero.
GetObserverPawn GetObserverPawn(): CSObserverPawn | undefined
GetPlayerName GetPlayerName(): string
GetPlayerPawn GetPlayerPawn(): CSPlayerPawn | undefined
GetPlayerSlot GetPlayerSlot(): number
GetScore GetScore(): number
GetWeaponDataForLoadoutSlot GetWeaponDataForLoadoutSlot(slot: number, team?: number): CSWeaponData | undefined Leave team as the default to use the player's current team.
IsBot IsBot(): boolean
IsConnected IsConnected(): boolean
IsObserving IsObserving(): boolean
JoinTeam JoinTeam(team: number): void

CSObserverPawn

extends BaseModelEntity

Methods

Function Signature Description
GetObserverMode GetObserverMode(): number
GetOriginalPlayerController GetOriginalPlayerController(): CSPlayerController Gets the controller that this player pawn was originally spawned for.
GetPlayerController GetPlayerController(): CSPlayerController | undefined Gets the controller currently controlling this player pawn.
SetObserverMode SetObserverMode(nMode: number): void

CSPlayerPawn

extends BaseModelEntity

Methods

Function Signature Description
DestroyWeapon DestroyWeapon(target: CSWeaponBase): void
DestroyWeapons DestroyWeapons(): void
DropWeapon DropWeapon(target: CSWeaponBase): void
FindWeapon FindWeapon(name: string): CSWeaponBase | undefined
FindWeaponBySlot FindWeaponBySlot(slot: CSGearSlot): CSWeaponBase | undefined
GetActiveWeapon GetActiveWeapon(): CSWeaponBase | undefined
GetArmor GetArmor(): number
GetOriginalPlayerController GetOriginalPlayerController(): CSPlayerController Gets the controller that this player pawn was originally spawned for.
GetPlayerController GetPlayerController(): CSPlayerController | undefined Gets the controller currently controlling this player pawn.
GiveNamedItem GiveNamedItem(name: string, autoDeploy?: boolean): void
IsCrouched IsCrouched(): boolean
IsCrouching IsCrouching(): boolean
IsNoclipping IsNoclipping(): boolean
SetArmor SetArmor(value: number): void
SwitchToWeapon SwitchToWeapon(target: CSWeaponBase): void

CSWeaponBase

extends BaseModelEntity

Methods

Function Signature Description
GetData GetData(): CSWeaponData
GetOwner GetOwner(): CSPlayerPawn | undefined

CSWeaponData

Methods

Function Signature Description
GetDamage GetDamage(): number Starting damage as the bullet travels
GetGearSlot GetGearSlot(): CSGearSlot
GetName GetName(): string
GetPenetration GetPenetration(): number Power to maintain damage during penetration.
GetPrice GetPrice(): number
GetRange GetRange(): number Maximum distance bullets will travel.
GetRangeModifier GetRangeModifier(): number Exponential damage drop off from traveling through air. nextDamage = currentDamage * rangeModifier ^ (distance / 500).
GetType GetType(): CSWeaponType

PointTemplate

extends Entity

Methods

Function Signature Description
ForceSpawn ForceSpawn(origin?: Vector, angle?: QAngle): Entity[] | undefined

Interfaces

BeforePlayerDamageEvent

Property Type Optional Description
attacker Entity yes The entity credited with causing the damage. For bullets this is the shooter. For grenades this is the thrower.
damage number no The amount of damage being applied, before armor and hitgroup modifications.
damageFlags CSDamageFlags no The flags configuring how to interpret the damage.
damageTypes CSDamageTypes no The type or types of damage.
inflictor Entity yes The entity applying the damage. For bullets this is the owner of the gun. For grenades this is the exploding projectile.
player CSPlayerPawn no The victim that is taking damage.
weapon CSWeaponBase yes The weapon used. For grenades this will not be present because the weapon is often removed before the projectile explodes.

BeforePlayerDamageModify

Property Type Optional Description
damage number yes The amount of damage being applied, before armor and hitgroup modifications.
damageFlags CSDamageFlags yes The flags configuring how to interpret the damage.
damageTypes CSDamageTypes yes The type or types of damage.

BulletTrace

Examples:

  • {damage:30, rangeModifer:.85, penetration:1} // Glock
  • {damage:30, rangeModifer:.45, penetration:1} // Mag-7
  • {damage:36, rangeModifier:.98, penetration:2} // AK47
  • {damage:115, rangeModifier:.99, penetration:2.5} // AWP
Property Type Optional Description
damage number yes The starting damage value of the bullet. This will reduce as it travels through the air and penetrates solids. @default 100
end Vector no
penetration number yes The power to maintain damage during penetration. Will default to 1 if left unspecified. @default 1
rangeModifier number yes The exponential damage drop off constant from traveling through air. @default .85
shooter CSPlayerPawn no The player shooting the bullet. Acts as `ignoreEnt` for traces.
start Vector no

BaseTraceConfig

Property Type Optional Description
end Vector no
ignoreEntity Entity | Entity[] yes Specify entities to not trace against. 0, 1 or 2 entities is equally fast. 3 or more is equally slower
ignorePlayers boolean yes
start Vector no
traceHitboxes boolean yes Trace against hitboxes instead of the larger collision shape for entities with hitboxes (eg. players).

BulletTraceResult

Result entry for Instance.TraceBullet

Property Type Optional Description
damage number no Damage value reduced by travel, before damage modification (body armor, headshots, etc)
hitEntity Entity no The entity hit by the bullet. This is not limited to players.
hitGroup CSHitGroup no
position Vector no

EntityDamage

Property Type Optional Description
attacker Entity yes The entity credited with causing the damage. For bullets this is the shooter. For grenades this is the thrower.
damage number no The amount of damage being applied, before armor and hitgroup modifications.
damageFlags CSDamageFlags yes The flags configuring how to interpret the damage.
damageTypes CSDamageTypes yes The type or types of damage.
inflictor Entity yes The entity applying the damage. For bullets this is the owner of the gun. For grenades this is the exploding projectile.
weapon CSWeaponBase yes The weapon used. For grenades this will not be present because the weapon is often removed before the projectile explodes.

PlayerDamageEvent

Property Type Optional Description
attacker Entity yes The entity credited with causing the damage. For bullets this is the shooter. For grenades this is the thrower.
damage number no The actual health lost after armor and hitgroup modifications.
damageFlags CSDamageFlags no The flags configuring how to interpret the damage.
damageTypes CSDamageTypes no The type or types of damage.
inflictor Entity yes The entity applying the damage. For bullets this is the owner of the gun. For grenades this is the exploding projectile.
player CSPlayerPawn no The victim that has taken damage.
weapon CSWeaponBase yes The weapon used. For grenades this will not be present because the weapon is often removed before the projectile explodes.

TraceResult

Property Type Optional Description
didHit boolean no
end Vector no
fraction number no
hitEntity Entity yes
hitGroup CSHitGroup yes
normal Vector no
startedInSolid boolean no


Enumerations

CSDamageFlags

Name Value Description
NONE 0
SUPPRESS_HEALTH_CHANGES 1 << 0
SUPPRESS_PHYSICS_FORCE 1 << 1
SUPPRESS_EFFECTS 1 << 2
PREVENT_DEATH 1 << 3
FORCE_DEATH 1 << 4
SUPPRESS_DAMAGE_MODIFICATION 1 << 5

CSDamageTypes

Name Value Description
GENERIC 0
CRUSH 1 << 0
BULLET 1 << 1
SLASH 1 << 2
BURN 1 << 3
VEHICLE 1 << 4
FALL 1 << 5
BLAST 1 << 6
CLUB 1 << 7
SHOCK 1 << 8
SONIC 1 << 9
BUCKSHOT 1 << 10
DROWN 1 << 11
POISON 1 << 12
HEADSHOT 1 << 13

CSGearSlot

Name Value Description
INVALID -1
RIFLE 0
PISTOL 1
KNIFE 2
GRENADES 3
C4 4
BOOSTS 5 Gear slot for healthshots.

CSHitGroup

Name Value Description
INVALID -1
GENERIC 0
HEAD 1
CHEST 2
STOMACH 3
LEFTARM 4
RIGHTARM 5
LEFTLEG 6
RIGHTLEG 7
NECK 8

CSLoadoutSlot

Name
INVALID
MELEE
SECONDARY0
SECONDARY1
SECONDARY2
SECONDARY3
SECONDARY4
SMG0
SMG1
SMG2
SMG3
SMG4
RIFLE0
RIFLE1
RIFLE2
RIFLE3
RIFLE4
EQUIPMENT2

CSRoundEndReason

Name Value Description
UNKNOWN -1
IN_PROGRESS
GAME_COMMENCING
DRAW
TARGET_BOMBED
TARGET_SAVED
BOMB_DEFUSED
HOSTAGES_RESCUED
HOSTAGES_NOT_RESCUED
CTS_WIN
TERRORISTS_WIN
CTS_SURRENDER
TERRORISTS_SURRENDER

CSWeaponAttackType

Name Value Description
INVALID -1
PRIMARY 0
SECONDARY 1

CSWeaponType

Name Value Description
KNIFE 0
PISTOL 1
SUBMACHINEGUN 2
RIFLE 3
SHOTGUN 4
SNIPER_RIFLE 5
MACHINEGUN 6
C4 7
TASER 8
GRENADE 9
EQUIPMENT 10
STACKABLEITEM 11 Healthshot
UNKNOWN 12


Type Aliases

cs_script pre-defines a few type aliases.

BeforeDamageResult

Signature: type BeforeDamageResult = { damage?: number, abort?: boolean } | void

Color

Signature: type Color = { r: number, g: number, b: number, a?: number }

Values will be clamped to a maximum of 255.

InputValue

Signature: type InputValue = boolean | number | string | Vector | Color | undefined

QAngle

Signature: type QAngle = { pitch: number, yaw: number, roll: number }

Vector

Signature: type Vector = { x: number, y: number, z: number }