This page provides quickstart examples demonstrating how to create a Dota2 client instance, establish a session with the Game Coordinator (GC), and subscribe to SOCache events. It covers the essential initialization sequence and event subscription patterns needed to begin interacting with Dota 2's Game Coordinator.
The Dota2 client is created using the New function, which requires an established steam.Client from the go-steam library and a logrus logger. The client automatically registers itself as a packet handler for Game Coordinator messages with AppID 570 (Dota 2) [client.go:23-66].
Client Initialization Flow
Sources: [client.go:52-66]
The Dota2 struct maintains several key components:
| Component | Type | Purpose |
|---|---|---|
client | *steam.Client | Reference to the underlying Steam client for GC communication [client.go:34] |
cache | *socache.SOCache | Shared object cache for tracking game objects [client.go:35] |
handlers | handlerMap | Message type to handler function mapping [client.go:44] |
state | state.Dota2State | Thread-safe state management including connection status [client.go:42] |
pendReq | map[uint32]map[uint32]responseHandler | Request tracking for request-response patterns [client.go:48] |
Sources: [client.go:32-49]
After creating the client, the application must signal Steam that it is playing Dota 2 and initiate the Game Coordinator handshake.
The SetPlaying method informs Steam whether the client is currently playing Dota 2. This must be set to true before attempting to communicate with the Game Coordinator.
Sources: [client.go:121-123] (Note: SetPlaying logic is typically handled by the underlying steam.Client or specific session management files not provided in the snippet, but dota.emit is used to track state changes [client.go:121-142]).
The SayHello method initiates the Game Coordinator handshake. The GC responds with a CMsgGCClientWelcome message [client.go:86] when a session is successfully established.
The client handles the welcome message via handleClientWelcome [client.go:86], which transitions the internal state to GCConnectionStatus_HAVE_SESSION.
Session Establishment Flow
Sources: [client.go:86-87], [client.go:116-123], [client.go:136-140]
The SOCache (Shared Object Cache) system provides a subscription mechanism for monitoring changes to game objects. Objects are organized by type ID, with each type managed in a separate container [README.md:43-48], [socache/socache.go:24-39].
The SubscribeType method allows subscribing to events for a specific object type (e.g., Lobby, Party) defined in the cso package [socache/socache_sub.go:33-40].
Sources: [README.md:49-64], [socache/socache_sub.go:33-40], [client.go:69-71]
Events for the object type are emitted on the eventCh as CacheEvent structs [socache/socache_sub.go:22-27].
| Event Type | Description |
|---|---|
EventTypeCreate | Emitted when an object is inserted into the cache [socache/socache_sub.go:14] |
EventTypeUpdate | Emitted when an object is updated in the cache [socache/socache_sub.go:16] |
EventTypeDestroy | Emitted when an object is removed from the cache [socache/socache_sub.go:18] |
SOCache Subscription Architecture
Sources: [socache/socache_sub.go:33-40], [client.go:69-71]
Typical Client Lifecycle
Sources: [client.go:52-66], [client.go:74-80], [client.go:86], [socache/socache.go:42-60]
| Method | Receiver | Parameters | Purpose |
|---|---|---|---|
New | Package | client *steam.Client, le logrus.FieldLogger | Creates new Dota2 client instance [client.go:52] |
GetCache | *Dota2 | None | Returns the SOCache instance [client.go:69] |
Close | *Dota2 | None | Cancels ongoing session contexts [client.go:74] |
HandleGCPacket | *Dota2 | packet *gamecoordinator.GCPacket | Dispatches incoming GC messages [client.go:156] |
SubscribeType | *SOCache | id cso.CSOType | Subscribes to cache updates for a type [socache/socache_sub.go:33] |
Sources: [client.go:52-156], [socache/socache_sub.go:33-40]
The ErrNotReady error is returned when the Dota client is not ready to accept requests, typically before a session is established or immediately after losing connection [client.go:26]. Incoming packets are handled by HandleGCPacket, which routes them through the handlerMap or identifies them as unhandled events [client.go:156-177].
Sources: [client.go:26], [client.go:156-177]