Metamod Source Game Event listening
A small explanation on how to listen to game events. This example is made for CS:GO, but will work on other Source based games too (some event names may be different). The example is meant for class based plugins. So if you are creating static functions you'll have to do things a little bit different.
Your Plugin
Get the interface
Make sure you got the interface of 'IGameEventManager2'.Â
Global scope, or class member:
IGameEventManager2 *gameevents = NULL;
In your 'MyPlugin::Load()' method:
GET_V_IFACE_CURRENT(GetEngineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
In our example we the pointer 'gameevents' will be used to access the interface.
Define which events you want to listen too.
Do this after you got your interface. Lets say we want to listen to the events 'player_death' and 'player_hurt'.
bool MyPlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
{
...
GET_V_IFACE_CURRENT(GetEngineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
...
gameevents->AddListener(this, "player_death", true);
gameevents->AddListener(this, "player_hurt", true);
}
Inherit the class 'IGameEventsManager2' in your plugin class.
class MyPlugin : public ISmmPlugin, public IMetamodListener, public IGameEventListener2
{
...
public: //Declare the virtual functions from IGameEventListener2
void FireGameEvent(IGameEvent * event);
int GetEventDebugID();
}
Create the method members for your plugin
void MyPlugin::FireGameEvent(IGameEvent * event)
{
}
in MyPlugin::GetEventDebugID()
{
return 42;
}
Now every time the events gets fired, the function 'FireGameEvent' will be called.
The 'GetEventDebugID()' should return 42. According to one of the maintainers of Metamod.
[23:49] <psychon1c> oh, for events, IEventListener2 has an extra member, starting in swarm i think
[23:49] <MaxLap> good thing i wasn't aiming at being ready for release then!
[23:49] <psychon1c> GetDebugId or GetEventDebugId, something like that
[23:50] <psychon1c> and just return 42 in the impl. there's a define for it, but i don't remember the name
[23:50] <psychon1c> should be in the header
[23:56] <MaxLap> you could just copy paste this conv in a page and then link to it lol
Access data from the event
All events can be found here, and in our example for CSGO here. The info is retrieved by using key values and their correct type.
So now we are currently only listening to two events. player_death and player_hurt. To get information out of the event do the following in your FireGameEvent() function:
void MyPlugin::FireGameEvent(IGameEvent * event)
{
if (event->GetName() == "player_death")
{
//Weapon name that caused the death
const char * weapon = event->GetString("weapon");
//Was it a headshot?
bool headshot = event->GetBool("headshot");
....
}
}