Difference between revisions of "IGameEventManager"

From Valve Developer Community
Jump to: navigation, search
m
m (Removed "localhost" Doxygen link)
Line 68: Line 68:
  
 
== See Also ==
 
== See Also ==
* {{doxygen|igameevents_8h-source.html igameevents.h on HL2 SDK Doxygen}}
 
 
* [[Networking Events & Messages]]
 
* [[Networking Events & Messages]]
 
[[Category:Programming]][[Category:Interfaces]]
 
[[Category:Programming]][[Category:Interfaces]]

Revision as of 20:22, 30 June 2008

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 at events

You can subscribe at events with the function AddListener. 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 an 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 at 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