IGameEventManager: Difference between revisions
Le Glaconus (talk | contribs) mNo edit summary |
|||
(5 intermediate revisions by 3 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. | 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. | ||
Line 4: | Line 6: | ||
=== Subscribing to events === | === Subscribing to events === | ||
You can subscribe at events with the function | 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|highlight=cpp|gameeventmanager->AddListener( this, true );}} | |||
Catching a certain event on the server: | Catching a certain event on the server: | ||
{{code|highlight=cpp|gameeventmanager->AddListener( this, "event_name", true );}} | |||
Catching an event on the client: | Catching an event on the client: | ||
{{code|highlight=cpp|gameeventmanager->AddListener( this, "event_name", false );}} | |||
=== Catching events === | === Catching events === | ||
Catching events is easy. Only adding the function | Catching events is easy. Only adding the function {{code|highlight=cpp|void FireGameEvent( IGameEvent* event )}} is enough. | ||
Example: | Example: | ||
< | <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") ); | |||
}</ | }</source> | ||
=== Adding events === | === Adding events === | ||
Custom events need to be added to the file | 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 | 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 === | ||
< | <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); | ||
} | } | ||
</ | </source> | ||
=== Subscribing To Events === | === Subscribing To Events === | ||
< | <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 ); | ||
} | } | ||
</ | </source> | ||
=== Catching events === | === Catching events === | ||
< | <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: | ||
} | } | ||
} | } | ||
</ | </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);
}
}
}