Global Listeners

How to subscribe to CounterStrikeSharp global listeners.

Adding a Listener

Global listeners come in a variety of shapes so there is no shared registration for these, they must be registered in the OnStart of your plugin (or anywhere you want) using different registration methods. You can find the full list of event listeners in the Listeners section as seen below.

using System;
using System.Numerics;
using Plugify;
using static s2sdk.s2sdk;

public unsafe class Sample : Plugin
{
    public void OnPluginStart()
    {
        OnEntitySpawned_Register(OnEntitySpawnedCallback);
    }

    public void OnPluginEnd()
    {
        OnEntitySpawned_Unregister(OnEntitySpawnedCallback);
    }

    private static void OnEntitySpawnedCallback(int entityHandle)
    {
        string className = GetEntityClassname(entityHandle);
        if (className != "smokegrenade_projectile") return;
        // Changes smoke grenade colour to a random colour each time.
        QueueTaskForNextFrame(OnEntitySpawnedCallback_Post, [entityHandle]);
    }
    
    private static void OnEntitySpawnedCallback_Post(object[] data) {
        int entityHandle = data[0] as int;
        Vector3 smokeColor = new Vector3(
            Random.Shared.NextSingle() * 255.0f
            Random.Shared.NextSingle() * 255.0f
            Random.Shared.NextSingle() * 255.0f
        );
        SetEntSchemaVector3D(entityHandle, 'CSmokeGrenadeProjectile', 'm_vSmokeColor', smokeColor, true, 0);
        PrintToServer($"Smoke grenade spawned with color {smokeColor}");
    }
}

Full list of global listeners can be find here