Quick Start
In this chapter, we’ll write a script that prints a line when a round starts or ends, when we get a kill, and when we die. We assume you’ve installed pygsi and added the config file from Installation and CS2 Setup.
from pygsi import GSIServer, PlayerMatchStats, PlayerState, RoundState
gsi = GSIServer(player_ids="76561198000000000") # your SteamID64
@gsi.on_round_start
async def round_start(player_id: str, old: RoundState | None, new: RoundState) -> None:
state = gsi.state
if state is not None and state.map is not None:
print(f"Round {state.map.round} started on {state.map.name}")
@gsi.on_round_end
async def round_end(player_id: str, old: RoundState | None, new: RoundState) -> None:
print(f"Round over, {new.winning_team} won")
@gsi.on_local_player_kill
async def kill(
player_id: str, old: PlayerMatchStats | None, new: PlayerMatchStats
) -> None:
print(f"Kill! That's {new.kills} this match")
@gsi.on_local_player_death
async def death(player_id: str, old: PlayerState | None, new: PlayerState) -> None:
print("We died")
gsi.run()
The script starts by creating a GSIServer, which receives the payloads from CS2, and player_ids tells it whose game to follow. It listens on port 4213 unless we pass port=, and the port has to match the uri in the config file.
Each decorator registers an async handler for one event. Every handler gets the SteamID64 of the player the event is about, then old and new, one slice of the game state before and after the payload. Kill handlers get PlayerMatchStats, round handlers get RoundState, and so on. For a few events, like the round start, old is None when pygsi has nothing to compare with yet. Events has the full list.
In round_start, we read gsi.state, the latest full GameState for the player, which we can do anywhere, not only in a handler (see Game State). The last line, gsi.run(), starts the server and blocks until we stop it with Ctrl+C.
Let’s save it as main.py and run it:
python main.py
Now we start CS2 and join a match. Any mode works, including a practice match with bots. pygsi skips warmup, so nothing prints until the match goes live. If nothing shows up after that, check that CS2 was started after the config file was in place, and that the SteamID64 in the script is yours.
The repository’s example/ folder has a longer script that logs every event.