IGameEventManager: Difference between revisions
No edit summary |
Le Glaconus (talk | contribs) mNo edit summary |
||
(18 intermediate revisions by 9 users not shown) | |||
Line 1: | Line 1: | ||
= | {{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. | ||
bool Plugin::Load( CreateInterfaceFn interfaceFactory, CreateInterfaceFn gameServerFactory ) { | |||
== For modcode == | |||
=== Subscribing to events === | |||
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: | |||
{{code|highlight=cpp|gameeventmanager->AddListener( this, true );}} | |||
Catching a certain event on the server: | |||
{{code|highlight=cpp|gameeventmanager->AddListener( this, "event_name", true );}} | |||
Catching an event on the client: | |||
{{code|highlight=cpp|gameeventmanager->AddListener( this, "event_name", false );}} | |||
=== Catching events === | |||
Catching events is easy. Only adding the function {{code|highlight=cpp|void FireGameEvent( IGameEvent* event )}} is enough. | |||
Example: | |||
<source lang=cpp> | |||
void FireGameEvent( IGameEvent* event ) | |||
{ | |||
if ( Q_strcmp(event->GetName(), "player_connect") == 0 ) | |||
Msg("The player %d has connected!\n", event->GetInt("userid") ); | |||
}</source> | |||
=== Adding events === | |||
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 == | |||
=== Note on IGameEventManager2 === | |||
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 === | |||
<source lang=cpp> | |||
bool Plugin::Load( CreateInterfaceFn interfaceFactory, CreateInterfaceFn gameServerFactory ) | |||
{ | |||
IGameEventManager2* gameeventmanager = | IGameEventManager2* gameeventmanager = | ||
(IGameEventManager2*) interfaceFactory( INTERFACEVERSION_GAMEEVENTSMANAGER2, NULL); | (IGameEventManager2*) interfaceFactory( INTERFACEVERSION_GAMEEVENTSMANAGER2, NULL); | ||
} | } | ||
</ | </source> | ||
< | === Subscribing To Events === | ||
void Plugin::LevelInit( char const* pMapName ) { | <source lang=cpp> | ||
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 ); | ||
} | } | ||
</ | </source> | ||
=== Catching events === | |||
<source lang=cpp> | |||
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); | |||
} | |||
} | |||
} | |||
</source> | |||
== See also == | |||
* [[Networking Events & Messages]] | |||
[[Category:Programming]] | |||
[[Category:Interfaces]] | |||
[[Category:Programming]][[Category:Interfaces]] |
Latest revision as of 14:05, 18 June 2025




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 resource/ModEvents.res
before they can be used in the code.
For plugins
Note on IGameEventManager2
With the release of the 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);
}
}
}