Custom Event Handlers

Custom Event Handlers are a simple and intuitive way of using events in LabAPI.

Creating your Custom Event Handlers

For this example, we will create an empty class called MyCustomEventHandlers.cs.

All your custom event handlers class has to do is to inherit CustomEventHandlers.

MyCustomEventHandlers.cs
public class MyCustomEventHandlers : CustomEventHandlers

And voilà, you can start using any event inside LabAPI by just overriding its target method.

MyCustomEventHandlers.cs
public class MyCustomEventHandlers : CustomEventHandlers
{
    public override void OnRoundEnded(RoundEndedEventArgs args)
    {
        // Implement your custom logic here
    }
}

Registering the event handlers

All your event subscriptions should be done when your plugin is being enabled.

Instead of subscribing each method, you can pass your handler to RegisterEventHandlers inside CustomHandlersManager

MyFirstPlugin.cs
public MyCustomEventHandlers Events { get; } = new ();

public override void Enable()
{
    CustomHandlersManager.RegisterEventHandlers(Events);
}

Unregistering the event handlers

At the same time, all your event unsubscriptions should be done when your plugin is being disabled.

You can use againCustomHandlersManager, ⁣ but this time calling UnregisterEventHandlers.

Last updated

Was this helpful?