IGameEventManager: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (small fix)
mNo edit summary
 
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
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.
{{Underlinked|date=January 2024}}
 
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 <code>player_death</code>. An event that is fired when a player dies.


== For modcode ==
== For modcode ==


=== Subscribing at events ===
=== Subscribing to 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.
You can subscribe at events with the function {{code|highlight=cpp|AddListener()}} to add an instance of {{mono|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:
Catching all events on the server:
<code>gameeventmanager->AddListener( this, true );</code>
{{code|highlight=cpp|gameeventmanager->AddListener( this, true );}}


Catching an certain event on the server:
Catching a certain event on the server:
<code>gameeventmanager->AddListener( this, "event_name", true );</code>
{{code|highlight=cpp|gameeventmanager->AddListener( this, "event_name", true );}}


Catching an event on the client:
Catching an event on the client:
<code>gameeventmanager->AddListener( this, "event_name", false );</code>
{{code|highlight=cpp|gameeventmanager->AddListener( this, "event_name", false );}}


=== Catching events ===
=== Catching events ===
Catching events is easy. Only adding the function <code>void FireGameEvent( IGameEvent* event )</code> is enough.
Catching events is easy. Only adding the function {{code|highlight=cpp|void FireGameEvent( IGameEvent* event )}} is enough.
Example:
Example:
<pre>
<source lang=cpp>
void FireGameEvent( IGameEvent* event )
void FireGameEvent( IGameEvent* event )
{
{
if ( Q_strcmp(event->GetName(), "player_connect") == 0 )
    if ( Q_strcmp(event->GetName(), "player_connect") == 0 )
  Msg("The player %d has connected!\n", event->GetInt("userid") );
        Msg("The player %d has connected!\n", event->GetInt("userid") );
}</pre>
}</source>


=== Adding events ===
=== Adding events ===
Custom events need to be added to the file <code>resource/ModEvents.res</code> before they can be used in the code.
Custom events need to be added to the file {{path|icon=file|resource/ModEvents|res}} before they can be used in the code.


== For plugins ==
== For plugins ==
Line 32: Line 36:
=== Note on IGameEventManager2 ===
=== 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.
With the release of the {{src06|4}} SDK, {{mono|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 ===
=== Getting an Instance if the IGameEventManager2 ===
<pre>
<source lang=cpp>
bool Plugin::Load( CreateInterfaceFn interfaceFactory, CreateInterfaceFn gameServerFactory ) {
bool Plugin::Load( CreateInterfaceFn interfaceFactory, CreateInterfaceFn gameServerFactory )
{
IGameEventManager2* gameeventmanager =  
IGameEventManager2* gameeventmanager =  
(IGameEventManager2*) interfaceFactory( INTERFACEVERSION_GAMEEVENTSMANAGER2, NULL);
(IGameEventManager2*) interfaceFactory( INTERFACEVERSION_GAMEEVENTSMANAGER2, NULL);
}
}
</pre>
</source>


=== Subscribing at events ===
=== Subscribing To Events ===
<pre>
<source lang=cpp>
void Plugin::LevelInit( char const* pMapName ) {
void Plugin::LevelInit( char const* pMapName )
{
Msg( "Level \"%s\" has been loaded\n", pMapName );
Msg( "Level \"%s\" has been loaded\n", pMapName );
gameeventmanager->AddListener( this, "player_say", true );
gameeventmanager->AddListener( this, "player_say", true );
}
}
</pre>
</source>


=== Catching events ===
=== Catching events ===
<pre>
<source lang=cpp>
class Plugin :
class Plugin :
public IGameEventListener2 {
public IGameEventListener2 {
virtual void FireGameEvent( IGameEvent* event ) {
virtual void FireGameEvent( IGameEvent* event )  
{
const char* eventName = event->GetName();
const char* eventName = event->GetName();


if( FStrEq( eventName, "player_say") ) {
if( FStrEq( eventName, "player_say") )  
{
int userId = event->GetInt( "userid");
int userId = event->GetInt( "userid");
const char* text = event->GetString( "text");
const char* text = event->GetString( "text");
Line 64: Line 72:
}
}
}
}
</pre>
</source>


== See also ==
* [[Networking Events & Messages]]


== See Also ==
[[Category:Programming]]
* {{doxygen|igameevents_8h-source.html igameevents.h on HL2 SDK Doxygen}}
[[Category:Interfaces]]
* [[Networking Events & Messages]]
[[Category:Programming]][[Category:Interfaces]]

Latest revision as of 14:05, 18 June 2025

Underlinked - Logo.png
This article needs more Wikipedia icon links to other articles to help Wikipedia icon integrate it into the encyclopedia. Please help improve this article by adding links Wikipedia icon that are relevant to the context within the existing text.
January 2024

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 Fileresource/ModEvents.res before they can be used in the code.

For plugins

Note on IGameEventManager2

With the release of the Source 2006 Source 2006 SDK, 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