EventHandlers

How to create EventHandlers

In order to be able to use Events you can just create a new Type that inherits from Neuron.Core.Events.Listener and add an Automatic Attribute to the Type

using Neuron.Core.Events;
using Neuron.Core.Meta;

namespace DocsExample;

[Automatic]
public class EventHandlers : Listener { }

An instance of this Type will now be created between the Loading and Enabling of your Plugin/Module.

How can I now hook the Events?

Well it's really simple just create a public void Method and use as arguments the Parameters for the Event you want to use and also mark the Method with the EventHandlerAttribute

using Neuron.Core.Events;
using Neuron.Core.Meta;
using Synapse3.SynapseModule.Events;

namespace DocsExample;

[Automatic]
public class EventHandlers : Listener
{
    [EventHandler]
    //The Method must be public
    public void OnRoundStart(RoundStartEvent ev)
    {
        //The code inside this method will now be excuted when the Round Starts
        //You can use ev to get more information about the specific event
        //However in this case the Round Start event doesn't have any additional informtaion
    }
}

Manually subscribing

In some cases you want to subscribe manually to an event and this is also possible with Synapse3: You just have to get the proper EventService that contains the Event you wan't to hook and call the Subscribe Method of the Event

using Neuron.Core.Events;
using Neuron.Core.Meta;
using Synapse3.SynapseModule.Events;

namespace DocsExample;

[Automatic]
public class EventHandlers : Listener
{
    public EventHandlers(ItemEvents itemEvents)
    {
        //This will subscribe the method
        //This can be used everywhere, the method must not be inside a Listener
        itemEvents.ConsumeItem.Subscribe(OnConsumeItem);
    }

    //Since you are subscribing manually can the method be private
    private void OnConsumeItem(ConsumeItemEvent ev)
    {
        
    }
    
    [EventHandler]
    //But it needs to be public if it is automatically subscribed to
    public void OnRoundStart(RoundStartEvent ev)
    {
        
    }
}

Multiple Events

Many Event Arguments inherits the same common Type which means you can use this Type as argument inside your method and hook multiple Events to the same method

using Neuron.Core.Events;
using Neuron.Core.Meta;
using Synapse3.SynapseModule.Events;

namespace DocsExample;

[Automatic]
public class EventHandlers : Listener
{
    public EventHandlers(ItemEvents itemEvents)
    {
        itemEvents.ConsumeItem.Subscribe(OnItemUse);
        itemEvents.FlipCoin.Subscribe(OnItemUse);
    }

    private void OnItemUse(BasicItemInteractEvent ev)
    {
        
    }
    
    [EventHandler]
    public void OnRoundStart(RoundStartEvent ev)
    {
        
    }
}

Manually Creating a Listener

If you don't wan't to use the Automatic Attribute can you use Synapse3.SynapseModule.Synapse.GetEventHandler<T>() to create and register a Listener

Last updated