IGameEventManager: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
== Note on IGameEventManager2 ==
The '''IGameEventManager2'''-class makes it possible to fire & catch events. Events are "messages" sent by the server and are "catchable" on the server & client. An good example is <code>player_death</code>. An event that is fired when a player dies.


With the release of the updated SDK for Ep1 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. '''Beware''' of changes in the API for this version.
== For modcode ==


== Getting an Instance if the IGameEventManager2 ==
=== Subscribing at events ===
You can subscribe at events with the function <code>AddListener</code>. 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:
<code>gameeventmanager->AddListener( this, true );</code>
Catching an certain event on the server:
<code>gameeventmanager->AddListener( this, "event_name", true );</code>
Catching an event on the client:
<code>gameeventmanager->AddListener( this, "event_name", false );</code>
=== Catching events ===
Catching events is easy. Only adding the function <code>void FireGameEvent( IGameEvent* event )</code> is enough.
Example:
<pre>
void FireGameEvent( IGameEvent* event )
{
if ( Q_strcmp(event, "player_connect") == 0 )
  Msg("The player %d has connected!\n", event->GetInt("userid") );
}</pre>
=== Adding events ===
Custom events need to be added to the file <code>resource/ModEvents.res</code> 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 ===
<pre>
<pre>
bool Plugin::Load( CreateInterfaceFn interfaceFactory, CreateInterfaceFn gameServerFactory ) {
bool Plugin::Load( CreateInterfaceFn interfaceFactory, CreateInterfaceFn gameServerFactory ) {
Line 12: Line 42:
</pre>
</pre>


== Subscribing for Events ==
=== Subscribing at events ===
 
<pre>
<pre>
void Plugin::LevelInit( char const* pMapName ) {
void Plugin::LevelInit( char const* pMapName ) {
Line 21: Line 50:
</pre>
</pre>


== Checking for Events ==
=== Catching events ===
 
<pre>
<pre>
class Plugin :
class Plugin :

Revision as of 13:40, 16 August 2006

The IGameEventManager2-class makes it possible to fire & catch events. Events are "messages" sent by the server and are "catchable" on the server & client. An 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, "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