Game Events

How to listen to Source 2 style game events.

Events are informational notification messages passed between objects in the server. Many are also passed from the server to the client. They are defined in .res files under the hl2/resource folder and resource folders of specific mods. For a basic listing, see Source2 Game Events.

It is important to note a few concepts about events:

  • They are almost always informational. That is, blocking player_death will not stop a player from dying. It may block a HUD or console message or something else minor.
  • They almost always use userids instead of client slots.
  • Just because it is in a resource file does not mean it is ever called, or works the way you expect it to. Mods are notorious for not properly documenting their event functionality.

An example of finding when a player dies:

public void RegisterEvents()
{
    HookEvent("player_death", Event_PlayerDeath);
}
 
public static void Event_PlayerDeath(string name, nint event, bool dontBroadcast)
{
    int victim_id = GetEventInt(event, "userid");
    int attacker_id = GetEventInt(event, "attacker");
    
    int victim = victim_id + 1;
    int attacker = attacker_id + 1;
    
    /* CODE */
}

Adding an Event Listener

You can bind event listeners in the OnPluginStart (or anywhere you like).

c#
c++
python
go
js
lua
using Plugify;
using static s2sdk.s2sdk;

public unsafe class Sample : Plugin
{
    public void OnPluginStart()
    {
        HookEvent("player_death", (string name, nint event, bool dontBroadcast) =>
        {
            int userid = GetEventInt(event, "userid");
            PrintToServer("event called.\n");
            return ResultType.Continue;
        }, HookMode.Post);
    }
}

Accessing Event Parameters

To access event parameters, you can use the GetEventInt, GetEventFloat, GetEventString, etc. functions.

int userid = GetEventInt(event, "userid");
float damage = GetEventFloat(event, "damage");

To set event parameters, you can use the SetEventInt, SetEventFloat, SetEventString, etc. functions.

SetEventInt(event, "userid", 123);
SetEventFloat(event, "damage", 50.0f);

Preventing Broadcast

You can modify a game event so that it does not get broadcast to clients by calling the SetEventBroadcast function.

SetEventBroadcast(event, false);

Cancelling an Event

In a pre-event hook, you can prevent the event from continuing to other plugins by returning ResultType.Handled or ResultType.Stop.