From Valve Developer Community
(Redirected from Events)
Jump to: navigation, search

The IGameEventManager2-class makes it possible to fire & catch events. Events are "messages" sent by the server and are "catchable" on the server & client. A good example is player_death. An event that is fired when a player dies.

For modcode

Subscribing to events

You can subscribe at events with the function AddListener to add an instance of IGameEventListener2 to the event, for more information see Networking Events & Messages.

It's possible to catch all events but most of the time, due performance-reasons, only catching certain events would be smarter.

Catching all events on the server: gameeventmanager->AddListener( this, true );

Catching a certain event on the server: gameeventmanager->AddListener( this, "event_name", true );

Catching an event on the client: gameeventmanager->AddListener( this, "event_name", false );

Catching events

Catching events is easy. Only adding the function void FireGameEvent( IGameEvent* event ) is enough. Example:

void FireGameEvent( IGameEvent* event )
if ( Q_strcmp(event->GetName(), "player_connect") == 0 )
   Msg("The player %d has connected!\n", event->GetInt("userid") );

Adding events

Custom events need to be added to the file resource/ModEvents.res before they can be used in the code.

For plugins

Note on IGameEventManager2

With the release of the August 06 SDK, the IGameEventManager was upgraded to version 2. This version allows for registering for only the events that are explicitly desired instead of registering for all events.

Getting an Instance if the IGameEventManager2

bool Plugin::Load( CreateInterfaceFn interfaceFactory, CreateInterfaceFn gameServerFactory ) {
	IGameEventManager2* gameeventmanager = 
		(IGameEventManager2*) interfaceFactory( INTERFACEVERSION_GAMEEVENTSMANAGER2, NULL);

Subscribing To Events

void Plugin::LevelInit( char const* pMapName ) {
	Msg( "Level \"%s\" has been loaded\n", pMapName );
	gameeventmanager->AddListener( this, "player_say", true );

Catching events

class Plugin :
	public IGameEventListener2 {
	virtual void FireGameEvent( IGameEvent* event ) {
		const char* eventName = event->GetName();

		if( FStrEq( eventName, "player_say") ) {
			int userId = event->GetInt( "userid");
			const char* text = event->GetString( "text");
			Msg( "Got player_say event: userid=%i text=%s\n", userId, text);

See Also