# Events

{% hint style="info" %}
The event add-on is included by default in the Databrain package
{% endhint %}

The Event add-on is an object-based generic event system. It supports class types as parameters.

### Register Listener

Register a listener to the event.

```csharp
databrainEvent.RegisterListener(Action<T> _data);
```

### UnregisterListener

remove listener from event

```csharp
databrainEvent.UnregisterListener(Action<T> _data);
```

### Raise Event

Raise an event.

```csharp
databrainEvent.Raise(T);
```

### Custom event object

Here's an example of a custom event object, including a parameter class

```csharp
public class MyEvent : DatabrainGenericEvent<MyEventData>{}

// Event parameter class
public class MyEventData
{
    public float param1;
    public string param2;
    
    // Constructor
    public MyEventData(){}
}
```

You can now use this event like this:

**EventCaller.cs**

```csharp
// Event caller example
public class EventCaller : MonoBehaviour
{
    public DataLibrary data;
    
    [DataObjectDropdown("data")]
    public MyEvent myEvent;
    
    // Using IMGUI for testing purposes only
    public void OnGUI()
    {
        if (GUILayout.Button("Call Event"))
        {
            // Create parameters
            var _params = new MyEventData();
            _params.param1 = 99.9f;
            _params.param2 = "My Event";
            // Raise event
            myEvent.Raise(_params);
        }
    }
}
```

**EventListener.cs**

```csharp
public class EventListener : MonoBehaviour
{
    public DataLibrary data;
    
    [DataObjectDropdown("data")]
    public MyEvent myEvent;
    
    public void Start()
    {
        // As with all DataObjects we have to make sure the DataLibrary is ready
        // before using it.
        data.RegisterInitializationCallback(Ready);
    }
    
    void Ready()
    {
        // DataLibrary is ready. We can now register to the event
        myEvent.RegisterListener(EventCalled);
    }
    
    void EventCalled(MyEventData _data)
    {
        // Event has been called
        Debug.Log("EVENT CALLED " + _data.param1 + " _ " + _data.param2);
    }
}
```

## Global DatabrainEvent

You can also use the base type - DatabrainEvent for global events which do not require any parameters.

```csharp
public DatabrainEvent myEvent;

myEvent.Raise();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://giantgrey.gitbook.io/databrain/add-ons/events.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
